Ask Your Question

Find pixel color out of cv::Mat on specific position

asked 2012-08-29 01:35:06 -0600

Stefan gravatar image

well my problem is, I need to find the sub matrix of a cv::Mat image which includes all white pixels. Therefore I want to iterate through all pixels, check if they are white and build a cv::Rect with that information.
I figured out how to iterate through all the pixels but I don't know how to get the pixels color out of it. The cv::Mat was previously converted to greyscale with CV_GRAY2BGR

for(int y = 0; y < outputFrame.rows; y++)
    for(int x = 0; x < outputFrame.cols; x++)
        // I don't know which datatype I should use
        if (<INSERT_DATATYPE_HERE>(x,y) == 255)
           //define area

My final question is, which datatype should I insert in the code on the position INSERT_DATATYPE_HERE and is 255 the right value to compare with than?

Thanks alot

edit retag flag offensive close merge delete


Please share the code for android.

harsh_bangari gravatar imageharsh_bangari ( 2017-06-21 07:47:00 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-08-29 16:38:15 -0600

imran gravatar image

To answer your first question, to get the pixel value (its colour), then you should use the following:

For a grayscale image, you would use this

Scalar colour =<uchar>(Point(x, y));

For a 3-channel colour image, you would use this

Vec3b colour =<Vec3b>(Point(x, y));
if(colour.val[0]==255 && colour.val[1]==255 && colour.val[2]==255)

To answer your second question, like Vladislav said, it would be "uchar" if it is a grayscale and "Vec3b" if it is a colour image, see below for example:

For grayscale you would use it like this,<uchar>(Point(x,y)) = 255;

For a 3-channel colour image it would be,<Vec3b>(Point(x, y))[0] = 255;<Vec3b>(Point(x, y))[1] = 255;<Vec3b>(Point(x, y))[2] = 255;
edit flag offensive delete link more


i tested out your Code and it doesnt work correctly for me i had to use<vec3b>(x,y) instead. Nevetheless thanks alot for your help

arminkazim7 gravatar imagearminkazim7 ( 2016-07-18 08:54:45 -0600 )edit

answered 2012-08-29 01:59:39 -0600

Vladislav Vinogradov gravatar image

If your image is grayscale, than type will be unsigned char.

If it is bgr -> cv::Vec3b (cv::Vec<unsigned char, 3>)

And y must be the first index -><unsigned char>(y, x)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2012-08-29 01:35:06 -0600

Seen: 65,363 times

Last updated: Aug 29 '12