1
votes

In matlab when I use imread function the pixel values of an image are stored in a 3D matrix (of uint8). The values of the matrix are between 0 and 255. But in OpenCV the imread function stores the values in a cv::Mat. When I try to see the values of the pixels I see float values and when I try to convert to integer I have big values.

How can I see the cv::Mat components (RGB) with values between 0 and 255 like in Matlab?

Thanks in advance!

5

5 Answers

1
votes

cv::Mat can be used with any type of pixel, if you use imread it will create a cv::Mat of the correct type.

Floating point images are unusual - are you sure the source data is floating point or are you just printing the values wrong?

You can convert a floating point image into 8bit ( CV8UC3) with cvtcolor()

1
votes
int red = (int)((uchar*)pImg->imageData)[y*((int)pImg->widthStep)+x*3+C];

Those are for IplImage. Don't know if works for Mat. C=(0 or 1 or 2) is for color channel.

0
votes

An example of the results

i have this:

Canny(test,edges,0.9*av[0],2*av[0],3,true);
    for(int i=0;i<edges.rows;i++){
        for(int j=0;j<edges.cols;j++){
            cv::Vec2w& elem = edges.at<cv::Vec2w>(i,j);
            std::cout << elem[0] << " , "<<elem[1] << std::endl;
        }
    }

the edges (cv::Mat) variable storage the result of cv::Canny function (a binary image) .. when i try to see the values of pixels using cv::Vec2w i have this for result: 6408 , 2058 1817 , 7433 1540 , 282 5386 , 1024 15 , 4867 768 , 275 1285 , 512 2 , 0 0 , 0 1 , 256 with cv::Vec2

0 , 0 0 , 0 0 , 0 0 , 0 0 , -256 255 , -256 0 , 0 0 , 0

with cv::Vec2i

0 , 0 0 , 0 -16777216 , -16776961 0 , 0 0 , -256 -1 , 65535 0 , -16777216 65535 , 0 0 , 0

and so on...

But for example if i write this image (imwrite("image.pgm",edges) ) and then reading with armadillo (the image es a single channel binary image) i have for the result an matrix (nx1) with values between 0 and 255 .. i know .. the format of both libraries is different, but .. i supose: binary image always have values 0 and 255 in one channel ....

0
votes

I normally store images in type IplImage* rather than cv::Mat. If I say IplImage* frame = (get image somehow...), then I can say frame->imageData to see the values you are interested in. The values are arranged for an RGB image in a single array [r1, b1, g1, r2, b2, g2, r3, b3, g3, ..., r(height*width), b(height*width), g(height*width)], and I believe they are arranged row-wise.

0
votes

What you need is edges.at<cv::Vec2b>(i,j), not cv::Vec2w or cv::Vec2i.