1

I am new to OpenCV image processing. My task is simple. I have to get 1/4th of an image (lower 1/4). The size of the image is 320 x 240. I used ROI Rect in a Mat object to get it.

Mat img_roi;
img_roi= image(cv::Rect(0,180,320,240));

I get the following error:

First-chance exception at 0x7669c41f in first.exe: Microsoft C++ exception: cv::Exception >at memory location 0x0041ec30.. Unhandled exception at 0x7669c41f in first.exe: Microsoft C++ exception: cv::Exception at >memory location 0x0041ec30..

If I specify Rect(0,0,320,60) in the above code, I am getting the result (which is upper half) and the code works for some other values, for eg Rect (0,0,320,240), i.e., a full image. But not with lower quarter values (0,180,320,240).

I should also tell that I am getting the result by using Iplimage. Only Mat object creates the problem.

I would be really thankful if any of you can guide me in solving this.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Karthik_elan
  • 323
  • 1
  • 6
  • 13

2 Answers2

2

Lower 1/4 of your image is Rect(0,180,320,60). It is not Rect(0,180, 320, 240) nor it is Rect(0,0,320,60).

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Michael Burdinov
  • 4,348
  • 1
  • 17
  • 28
  • Thank you very much Michael Burdinov. It worked. Importantly, I understood my mistake. I wonder, how it worked with Iplimage ? – Karthik_elan Jan 23 '14 at 11:05
  • You are welcome. As for why it worked with IplImage, I think that the exception you got was thrown by OpenCV itself, i.e. Mat constructor failed at sanity check. I am not sure that the same sanity check is performed by IplImage. Access to un-allocated memory is always bad, but not always creates exception. – Michael Burdinov Jan 23 '14 at 13:31
2

The arguments of cv::Rect are:

cvRect(int x, int y, int width, int height);

Here the x and Y represents the top-left corner whereas width and height represents the number of columns and rows you want to have in your image.

In your case: rows = 320, cols =240

so to get the lower-left corner use:

Rect( 0, image.rows/2, image.cols/2, image.rows/2 )

and if you want to have lower-right image then use:

Rect( image.cols/2, image.rows/2, image.cols/2, image.rows/2 )

skm
  • 5,015
  • 8
  • 43
  • 104