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