6

I wonder what is the most effective way of cropping an IplImage in opencv. I currently do the following, but it seems too complicated and I'm sure there's a better way of doing things.

    // set ROI on original image, create 'tmp' image and copy data over.
    cvSetImageROI(orig_image, cvRect(55, 170, 530, 230));

    IplImage *tmp = cvCreateImage(cvGetSize(orig_image),
                               orig_image->depth,
                               orig_image->nChannels);

    cvCopy(m_depth_run_avg, tmp, NULL);
    cvResetImageROI(orig_image);

    // copy temporary image back to original image.
    IplImage *orig_image= cvCreateImage(cvGetSize(tmp),
                           tmp->depth,
                           tmp->nChannels);
    cvCopy(tmp, orig_image, NULL);

is there a better way to crop a image?

solvingPuzzles
  • 8,541
  • 16
  • 69
  • 112
memyself
  • 11,907
  • 14
  • 61
  • 102

2 Answers2

5

Yes, there is. You seem to be recreating the original image at the end. That is not necessary, as the following code demonstrates:

IplImage* orig = cvLoadImage("test.jpg");
if (!orig)
{
    return -1;
}
printf("Orig dimensions: %dx%d\n", orig->width, orig->height);

cvSetImageROI(orig, cvRect(0, 250, 350, 350));

IplImage *tmp = cvCreateImage(cvGetSize(orig),
                               orig->depth,
                               orig->nChannels);

cvCopy(orig, tmp, NULL);
cvResetImageROI(orig);

orig = cvCloneImage(tmp);
printf("Orig dimensions after crop: %dx%d\n", orig->width, orig->height);

cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
cvShowImage( "result", orig);
cvWaitKey( 0 );
cvDestroyWindow( "result" );

Unfortunately, it's imperative that you create a temporary image to store the result of cvCopy().

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 1
    Make sure `cvLoadImage()` has sucessfuly loaded an image. I hard coded the file I needed to load in the application. You most certainly don't have a **test.jpg** laying around in the directory of the application. I guarantee that this code works. If it's crashing on your machine, you need to really, really, really pay attention on what you are doing differently (or not doing). – karlphillip Sep 25 '12 at 17:38
  • I have some kind of memory leak problem with that code when I'm using it to crop video frames. – Kamil Jul 29 '15 at 07:30
  • [That's probably due to `cvCloneImage()` or `cvLoadImage()`](http://www.cprogramdevelop.com/4885055/). Did you forgot to `cvReleaseImage()`? – karlphillip Jul 29 '15 at 12:41
0

you can easily crop the image by using c++. Make sure this code is successfully.try it

                       IplImage *source_image = cvLoadImage("paper.jpg", 1);


                     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


                     IplImage *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);