最有效地裁剪IplImage

时间:2012-09-25 15:27:02

标签: c image-processing opencv

我想知道在opencv中裁剪IplImage最有效的方法是什么。我目前做了以下工作,但看起来太复杂了,我确信有更好的办法。

    // 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);

有没有更好的方法裁剪图片?

2 个答案:

答案 0 :(得分:5)

,有。您似乎最终会重新创建原始图像。这不是必需的,如下面的代码所示:

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" );

不幸的是,您必须创建一个临时图像来存储cvCopy()的结果。

答案 1 :(得分:0)

您可以使用c ++轻松裁剪图像。确保此代码成功。

                       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);
相关问题