Ask Your Question

Iterating and printing all pixel values from a Mat image

asked 2017-03-09 04:01:05 -0600

mirnyy gravatar image

updated 2017-03-09 04:01:20 -0600


i would like to iterate and print out all pixel values of my
Mat image = imread(path, CV32F)-Object.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-09 04:15:31 -0600

berak gravatar image

updated 2017-03-09 05:41:03 -0600

first, please use correct flags for imread()

i guess, you wanted the following:

Mat image = imread(path, IMREAD_GRAYSCALE); //8bit uchar [0..255]  (CV_8U)
Mat float_img;
image.convertTo(float_img, CV_32F);
//image.convertTo(float_img, CV_32F, 1.0/255); // if you want it in [0..1] range

then, iterating over pixels is an absolute anti-pattern in opencv, please try to avoid that by all means.

you can simply print a whole Mat by:

cout << image << endl;
edit flag offensive delete link more


Thanks for the hint! The thing is that i would like to know if the image is containing a certain pixel value.
And what kind of type does IMREAD_GRAYSCALE return? Which range and which data type do the elements have?

mirnyy gravatar imagemirnyy ( 2017-03-09 04:49:03 -0600 )edit

^ see update.

berak gravatar imageberak ( 2017-03-09 05:40:27 -0600 )edit

Thank you.. but i can't check with it if the image is containing a certain pixel value or not..

mirnyy gravatar imagemirnyy ( 2017-03-09 05:59:41 -0600 )edit

see tutorials

but maybe you have to tell us, what you're trying to achieve ? 99%, there's already a builtin function for that.

berak gravatar imageberak ( 2017-03-09 06:05:44 -0600 )edit

Question Tools

1 follower


Asked: 2017-03-09 04:01:05 -0600

Seen: 4,009 times

Last updated: Mar 09 '17