6

我想知道在 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);

有没有更好的方法来裁剪图像?

4

2 回答 2

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

于 2012-09-25T16:27:26.467 回答
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);
于 2018-02-06T09:22:01.417 回答