converting 3-channel BGR pic to grayscale BY HAND
Hello,
I'm having this problem - I have to write a function which converts bgr picture to grayscale one-channel picture. Only things I can use are cv:Mat variables: rows, cols, data and step. I've been trying to solve it this way:
void bgr2gray( const Mat& bgr, Mat& gray )
{
gray = Mat::zeros(bgr.rows, bgr.cols, CV_8UC1 );
for (int y = 0; y < bgr.rows; y++) {
for (int x = 0; x < bgr.cols; x++) {
unsigned char blue = bgr.data[3*bgr.cols*y + x]*0.114f; //0.114
unsigned char green = bgr.data[3*bgr.cols*y + x+sizeof(char)]*0.587f;
unsigned char red = bgr.data[3*bgr.cols*y + x+2*sizeof(char)]*0.299f;
gray.data[gray.cols*y+x] = red + green + blue;
}
}
}
But still this gives me wrong (not whole) picture with many white vertical lines. Do you know whre I'm mistaken in my thinking?
Thanks for the answer
cz_asm7