Lion King
Lion King

Reputation: 146

cannot access values of Mat returned from OpenCV functions

I'm using cv::imread to load a image and do some processes with that image, but I don't know why I can't read values of the returned Mat from imread function. I used Mat.at method:

Mat iplimage = imread("Photo.jpg",1); //input
    for(int i=0;i<iplimage.rows;i++){
        for(int j=0;j<iplimage.cols;j++){
            cout<<(int)iplimage.at<int>(i,j)<<" ";
        }
        cout<<endl;
    }

But it appeared an error:

OpenCV Error: Assertion failed ( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((Sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) -1))*4) & 15) == elemSize1()) is unknown function, file: "c:\opencv2.2\include\opencv2\core\mat.hpp", line 517

But it is ok if I use the direct access way:

 Mat iplimage = imread("Photo.jpg",1); //input
     for(int i=0;i<iplimage.rows;i++){
         for(int j=0;j<iplimage.cols;j++){
        cout<<(int)iplimage.data[i*iplimage.cols + j]<<" ";                 
         }
    cout<<endl;
     }

Could anyone tell me how can I use the Mat.at method to access the above Mat? Thanks for your help!

Upvotes: 0

Views: 2453

Answers (4)

Yu Tao
Yu Tao

Reputation: 798

Mat iplimage = imread("Photo.jpg",1) this read in a 3 channel colour image. You can use Mat iplimage = imread("Photo.jpg",0) to read in the image as greyscale so that your iplimage.at(i,j) would work. Please note that you should use .at if your image is 8bit instead of .at.

If your image is not 8bit, you should use iplimage = imread("Photo.jpg",CV_LOAD_IMAGE_ANYDEPTH)

Upvotes: 0

Lion King
Lion King

Reputation: 146

I found the solution. It is because I used : inputImage.at<int>(i,j) or inputImage.at<float>(1,2) instead of, (int)inputImage.at<uchar>(1,2) or (float)inputImage.at<uchar>(1,2) Sorry for my carelessness!

Upvotes: 0

Mzk
Mzk

Reputation: 1120

you are trying to load with 3 channel of image it will be fine if you change to this Mat iplimage = imread("Photo.jpg",0); //input

Upvotes: 0

fireant
fireant

Reputation: 14538

See this answer. In your case, the returned Mat is 3 dimensional, hence iplimage.at<int> fails the assertion, you just need to access the intensities in each channel like the way the mentioned answer explain.

Upvotes: 0

Related Questions