Ask Your Question

How can I read the pixels value in opencv?

asked 2014-05-08 23:22:25 -0600

prince0fpersia gravatar image

I am trying this code to calculate my binary image pixels value by this code :

int main()

Mat img;

img=imread("/home/2b.jpeg", CV_LOAD_IMAGE_UNCHANGED);
namedWindow("win", WINDOW_AUTOSIZE);
imshow("win", img);

for(int i=0; i< img.rows ;i++)
    for(int j=0; j< img.cols ; j++)



    return 0;

But i get 3 types of value: 0, -1 and some big different numbers like (24342234 , 1324244242, etc)

What is the problem? I drew one black line in paint programs and save the image, or download binary image from internet but i get same results!

I thought when i use binary images i must get 0 for white pixels and 255 for black ones.

edit retag flag offensive close merge delete


how did you type the program? when i typed line by line it comes in a single line in post?

jamesnzt gravatar imagejamesnzt ( 2014-07-25 04:39:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-09 01:47:55 -0600

Siegfried gravatar image

You use the wrong datatype. The bitdepth of an image with pixels in the range from 0 to 255 is 8bit. So you have to "cast" to uchar.


A good tutorial on how to access pixels in OpenCV can be found here.

edit flag offensive delete link more

Question Tools


Asked: 2014-05-08 23:22:25 -0600

Seen: 235 times

Last updated: May 09 '14