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.
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!
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.
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 ;).
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With