简体   繁体   English

在opencv中按选定像素裁剪图像

[英]Cropping image by selected pixels in opencv

I am trying to half the original image by halving height and width of the image and cropping it. 我正在尝试通过将图像的高度和宽度减半并将其裁切来将原始图像减半。 [image to be cropped][1] [要裁剪的图像] [1]

and below is my code but it causing a runtime exception and the .exe file stops working with the below error: 以下是我的代码,但它导致运行时异常,.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.

below is the code: 下面是代码:

#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;
}

The problem is in your roi . 问题出在你的投资roi Since x=width and y=length , you're using a roi out of the image. 由于x=widthy=length ,您将使用图像的roi。 x and y should be the top-left corner of your roi. xy应该是您投资回报率的左上角。 In this case they should both be 0 . 在这种情况下,它们都应为0

You shouldn't use obsolete C api. 您不应该使用过时的 C api。

The get a crop of the top-left part of the image, you can simply: 裁剪图像左上角的部分,您可以简单地:

#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;
}

Producing: 生产:

在此处输入图片说明

Besides using the obsolete API (as pointed out by @Miki), OpenCV is telling you what the problem is (formatted for clarity): 除了使用过时的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

Specifically, this call to cvSetImageROI() is failing: 具体而言,对cvSetImageROI()此调用失败:

cvSetImageROI(img1, roi);

You can determine specifically which of the assertion subconditions are failing by checking each of them via your debugger. 您可以通过调试器检查每个断言子条件的具体出处,从而确定失败的条件。 You could also print the values out and compare them. 您也可以打印出值并进行比较。 Substitute img1 for image and roi for rect , respectively, ie: 分别将img1替换为image并将roi替换为rect ,即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM