Reputation: 457
I am trying to half the original image by halving height and width of the image and cropping it. [image to be cropped][1]
and below is my code but it causing a runtime exception and the .exe file stops working with the below error:
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;
}
Upvotes: 0
Views: 2941
Reputation: 41765
The problem is in your roi
. Since x=width
and y=length
, you're using a roi out of the image. x
and y
should be the top-left corner of your roi. In this case they should both be 0
.
You shouldn't use obsolete 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:
Upvotes: 2
Reputation: 1924
Besides using the obsolete API (as pointed out by @Miki), OpenCV is telling you what the problem is (formatted for clarity):
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(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, i.e.:
cout << roi.width << endl; // should be >= 0
cout << roi.height << endl; // should be >= 0
// etc
Upvotes: 0