【问题标题】:Cropping image by selected pixels in opencv通过opencv中的选定像素裁剪图像
【发布时间】:2018-01-15 14:35:27
【问题描述】:

我试图通过将图像的高度和宽度减半并裁剪它来将原始图像减半。 [要裁剪的图像][1]

以下是我的代码,但它会导致运行时异常,并且 .exe 文件停止工作并出现以下错误:

OpenCV Error: Assertion failed (rect.width >= 0 && rect.height >= 0 && rect.x < image->width && rect.y < image->height && rect.x + rect.width >= (int)(rect.width > 0) && rect.y + rect.height >= (int)(rect.height > 0)) in cvSetImageROI, file C:\Development\opencv\sources\modules\core\src\array.cpp, line 3006

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information.

下面是代码:

#include <iostream>
#include <string>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
   IplImage *img1 = cvLoadImage("image/testcase.jpg");
   cvNamedWindow("Image1:",1);
   cvShowImage("Image1:",img1);
   cout << "Width:" <<  img1->width <<" pixels"<< endl;
   cout << "Height:" <<  img1->height <<" pixels"<< endl;
   int width = img1->width ;
   int lenght = img1->height;

   // cropping the image

   Rect roi;
   roi.x = width;
   roi.y = lenght;
   roi.width = (roi.x)/2;
   roi.height = (roi.y)/2;

   Mat image_test;
   image_test = imread("image/testcase");
   // Must have dimensions of output image

   IplImage* cropped = cvCreateImage(cvSize(roi.width,roi.height), img1->depth, img1->nChannels );

   cvSetImageROI(img1, roi);
   cvCopy(img1, cropped);
   cvResetImageROI(img1);
   cvNamedWindow( "Cropped Image", 1 );
   cvShowImage( "Cropped Image", cropped );
   cvSaveImage ("savedImage/cropped.jpg" , cropped);
   waitKey(0);
   return 0;
}

【问题讨论】:

  • 您使用 过时 C api 的任何特殊原因?

标签: c++ opencv image-processing


【解决方案1】:

问题在于您的roi。由于x=widthy=length,您正在使用图像之外的投资回报率。 xy 应该是你的投资回报率的左上角。在这种情况下,它们都应该是0

您不应使用过时的 C api。

获取图像左上角的裁剪,您可以简单地:

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Define the roi
    Rect roi(0, 0, img.cols / 2, img.rows / 2);

    // Crop
    Mat3b crop = img(roi);

    // Show result
    imshow("Original", img);
    imshow("Crop", crop);
    waitKey();

    return 0;
}

制作:

【讨论】:

  • 谢谢。解决问题。
【解决方案2】:

除了使用过时的 API(如 @Miki 所指出的那样),OpenCV 还告诉您问题所在(为清晰起见而格式化):

OpenCV Error: Assertion failed 

(rect.width >= 0 
&& rect.height >= 0 
&& rect.x < image->width 
&& rect.y < image->height 
&& rect.x + rect.width >= (int)(rect.width > 0) 
&& rect.y + rect.height >= (int)(rect.height > 0))

in cvSetImageROI, file C:\Development\opencv\sources\modules\core\src\array.cpp, line 3006

具体来说,对cvSetImageROI() 的调用失败:

cvSetImageROI(img1, roi);

您可以通过调试器检查每个断言子条件,具体确定哪些断言子条件失败。您还可以将这些值打印出来并进行比较。分别用img1 代替imageroi 代替rect,即:

cout << roi.width << endl; // should be >= 0
cout << roi.height << endl; // should be >= 0
// etc

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多