Skip to content

Instantly share code, notes, and snippets.

@SubhiH
Created December 21, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SubhiH/0467753e84961094e342489e08003ebe to your computer and use it in GitHub Desktop.
Save SubhiH/0467753e84961094e342489e08003ebe to your computer and use it in GitHub Desktop.
Convert RGB image to gray scale by looping through pixels (two for loops) using OpenCV
void ImageOperator::to_gray_m3(const cv::Mat &input, cv::Mat &output) {
unsigned char *data_in = (unsigned char*)(input.data);
unsigned char *data_out = (unsigned char*)(output.data);
int index = 0;
for (int row = 0; row < input.rows; ++row) {
for (int col = 0; col < input.cols*input.channels(); col+=input.channels()) {
data_out[index]= 0.11*data_in[row*input.step+col]+
0.59*data_in[row*input.step+col+1]+
0.3*data_in[row*input.step+col+2];
index++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment