Ask Your Question

cz_asm7's profile - activity

2017-12-04 14:26:03 -0500 received badge  Popular Question (source)
2013-03-10 09:04:16 -0500 asked a question converting 3-channel BGR pic to grayscale BY HAND


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 =[3*bgr.cols*y + x]*0.114f; //0.114
         unsigned char green =[3*bgr.cols*y + x+sizeof(char)]*0.587f;
         unsigned char red =[3*bgr.cols*y + x+2*sizeof(char)]*0.299f;
[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