Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openCv crop image

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.

like image 360
Johann Avatar asked Jan 16 '13 18:01

Johann


People also ask

How do I crop an image in OpenCV?

There is no specific function for cropping using OpenCV, NumPy array slicing is what does the job. Every image that is read in, gets stored in a 2D array (for each color channel). Simply specify the height and width (in pixels) of the area to be cropped. And it's done!

How do I crop a rectangle in OpenCV?

To crop an image to a certain area with OpenCV, use NumPy slicing img[y:y+height, x:x+width] with the (x, y) starting point on the upper left and (x+width, y+height) ending point on the lower right. Those two points unambiguously define the rectangle to be cropped.


3 Answers

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 ;).

like image 176
Smash Avatar answered Nov 06 '22 00:11

Smash


Image cropping using OpenCV

Mat image=imread("image.png",1);

int startX=200,startY=200,width=100,height=100

Mat ROI(image, Rect(startX,startY,width,height));

Mat croppedImage;

// Copy the data into new matrix
ROI.copyTo(croppedImage);

imwrite("newImage.png",croppedImage);
like image 38
N.Singh Avatar answered Nov 06 '22 01:11

N.Singh


Try this.It's work.

                     IplImage *source_image;
                     IplImage *cropped_Image1;

                     cout << "Width:" << source_image->width << " pixels" << endl;
                     cout << "Height:" << source_image->height << " pixels" << endl;
                     int width = source_image->width;
                     int lenght = source_image->height;

                     cv::Rect roi;
                     roi.x = 1200; //1200     // 950
                     roi.y = 355; //350      //150 
                     roi.width = 2340; //2360          //2750
                     roi.height = 1425;  //1235 /2500         //2810   //2465 fully braille sheet


                     cropped_Image1 = cvCreateImage(cvSize(roi.width, roi.height), source_image->depth, source_image->nChannels);
                     cvSetImageROI(source_image, roi);
                     cvCopy(source_image, cropped_Image1);
                     cvResetImageROI(source_image);
                     cvShowImage("Cropped Image", cropped_Image1);
                     cvSaveImage("1_cropped.jpg", cropped_Image1);
like image 26
Vimukthi Gunasekara Avatar answered Nov 06 '22 02:11

Vimukthi Gunasekara