0

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!

Lion King
  • 126
  • 6

4 Answers4

0

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.

Community
  • 1
  • 1
fireant
  • 14,080
  • 4
  • 39
  • 48
  • I don't want to use cv::Split function, because I need to obtain the grayscale image. If Split it, then I need to convert into gray image manually. I think that it is better if I use a OpenCV function for such kinds of work. And, I also tried with imread("Photo.jpg",0), but the same problem still happen. Could you help me to fix this??? – Lion King May 07 '12 at 04:03
  • I'm using 0pencv2.2, is that the problem's cause? – Lion King May 07 '12 at 04:04
0

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

Mzk
  • 1,102
  • 3
  • 17
  • 40
0

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!

Lion King
  • 126
  • 6
0

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)

Yu Tao
  • 758
  • 5
  • 9