openCv crop image
37,275
Solution 1
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 ;).
Solution 2
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);
Solution 3
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);
Author by
Johann
Updated on July 09, 2022Comments
-
Johann almost 2 years
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.
-
Ryan Kennedy about 11 yearsAn answer worth accepting; came at the top of my Google search. Sometimes SO is much faster than the official documentation.
-
Denys Vitali almost 6 yearsThe question is about C++