Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cropping IplImage most effectively

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?

like image 223
memyself Avatar asked Sep 25 '12 15:09

memyself


1 Answers

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

like image 188
karlphillip Avatar answered Oct 14 '22 09:10

karlphillip