Ask Your Question

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

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

Stefan gravatar image Stefan
46 1 5

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

delete close flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted

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

imran gravatar image imran
413 3 5 17

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;
link delete flag offensive edit

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

Vladislav Vinogradov gravatar image Vladislav Vinogradov flag of Russian Federation
4209 12 52

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)

link delete flag offensive edit

Login/Signup to Answer

Question tools

1 follower

subscribe to rss feed


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

Seen: 4,883 times

Last updated: Aug 29 '12