0

I use visual studio 2012 with opencv 3.0. I already checked several post about this issue but still I get assertion failed error -215 when I run this code:

IplImage * imageOriginal = cvLoadImage("road1.jpg");

int width = 0, height = 0;    
width = imageOriginal->width;
height = imageOriginal->height;
IplImage* img = cvCreateImage(cvGetSize(imageOriginal), imageOriginal->depth , 3);    

CvRect cropRect = cvRect(0, 0, width -10, height -10 ); // ROI in source image 

cvSetImageROI(imageOriginal, cropRect);

try {
  cvCopy(imageOriginal, img, NULL); // Copies only crop region
}
catch (cv::Exception& e) {
  cout << e.what() << endl;
}
cvResetImageROI(imageOriginal);
cvShowImage( "Original ROI", img);
waitKey();

If I leave the rectangle to be full size CvRect cropRect = cvRect(0, 0, width, height); I get no error. What is the problem?

Elod
  • 499
  • 9
  • 25
  • 1
    Are you sure that your width and height are not less than 10. If they are, the effective height and width for the cropped region will be less than or equal to 0, which is maybe why you see the assertion failure. – The Vivandiere Sep 29 '14 at 16:50
  • Have you checked for nullptr in all your loads / creates? if width is 0 you get -10 in your current code. – Surt Sep 29 '14 at 16:50
  • Why dont you play with cvRect in general, vary the third and fourth param, and see at what point you get an error. Remove all the other code. Try cvRect(0, 0, 100, 100 ); then cvRect(0, 0, 200, 200 ); so on – The Vivandiere Sep 29 '14 at 17:14
  • If I put an integer less than the size of the image (648x482) still I get an error and I definitely load an image, no NULL pointer exaption. – Elod Sep 29 '14 at 17:16
  • 1
    I'm not sure, isn't it incorrect to allocate memory to image of the original size and copy only a cropped part? Why not convert to the new `Mat`? – a-Jays Sep 30 '14 at 04:42
  • Thanks, this was the solution, unfortunately I cant use `Mat` cuz the algorithm is going to be used in another software tool which only supports the Iplimage data type. – Elod Sep 30 '14 at 08:09

1 Answers1

0

your code is from opencv1.0. (that was a long time ago)

please, for opencv3.0, use the c++ api instead:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main() {
    Mat img = imread("road1.jpg");
    Mat roi = img(Rect(0,0,img.cols-10,img.rows-10));
    imshow("hi",roi);
    return waitKey();
}
berak
  • 39,159
  • 9
  • 91
  • 89
  • I must use Iplimage as data structure, cuz it will be used somewhere else this code. – Elod Sep 29 '14 at 17:42
  • you *must not* . (there will be no discussion) – berak Sep 29 '14 at 18:15
  • Ok, to be more precise: I have to make a module for RTMaps, in which software tool ONLY Iplimage is used. So, I must use Iplimage in order to keep my code clean and to use in RTMaps. Sorry if I hurt your feelings @berak. Anyway thanks for ur help, but I cant us it. – Elod Sep 29 '14 at 18:38
  • see, the whole thing bit you, where you had to pre-allocate img(and you made it too big) – berak Sep 29 '14 at 18:54
  • even if you have to use legacy code, try not to add technical debth. – berak Sep 29 '14 at 18:55