Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Your indices are wrong, the correct way would be:

unsigned char blue = bgr.data[3*bgr.cols*y + 3*x]*0.114;
unsigned char green = bgr.data[3*bgr.cols*y + 3*x + 1]*0.587;
unsigned char red = bgr.data[3*bgr.cols*y + 3*x+2]*0.299;

However, due to the individual casts you'll lose some precision, so either use float instead of unsigned char for your three lines or write it as a one-liner:

gray.at<uchar>(y,x) = bgr.at<Vec3b>(y,x)[0] * 0.114 + bgr.at<Vec3b>(y,x)[1]*0.587 + bgr.at<Vec3b>(y,x)[2]*0.299

Also note that your way of accessing the data-array only works for continuous data, this is normally true, but you never know.

Btw.: If you use the templated-matrices (i.e. Mat3b and Mat1b) you can omit the '.at<uchar>' and '.at<Vec3b>'