openCv crop image

Johann picture Johann · Jan 16, 2013 · Viewed 35.1k times · Source

I running into problems with my openCv IplImage cropping. Assuming both tmp and img are IplImage* . Using the code:

printf("Orig dimensions: %dx%d\n", img->width, img->height);
cvSetImageROI(img, cvRect(0, 0,500,500));
tmp = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopy(img, tmp, NULL);
cvResetImageROI(img);
img = cvCloneImage(tmp);
printf("Orig dimensions after crop: %dx%d\n", tmp->width, tmp->height);

when I use the the cvRect above I'll get an image cropped with a size of 500 x500 as expected, however when i use the rect (400,400,500,500) I'll get an image with the size 500 X 320.

Answer

Smash picture Smash · Jan 16, 2013

cvRect is defined as ( int x, int y, int width, int height ), not (int left, int top, int right, int bottom). So you are selecting a 500x500 region starting at the point (x,y) = (400,400). I am guessing your image height is 720 ;).