Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, OpenCV : Assertion failed in Resize

Tags:

c++

opencv

resize

As a C++ beginner, I am currently facing a problem I somehow can't solve, even if the code is pretty simple. I've been searching for answers all over the Internet, but none was applicable for my problem.

I am currently coding basic SVMs with C++, under VS2013, using OpenCV 2.4.8. I was able to work on images of same size, specifying fixed height, width at the beginning of my code.

Now, I'm trying to : open images of different sizes, resize them to a certain lower size, and apply the previous code to the now-resized dataset. Simple as that.

Here's the beginning of my code :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>

using namespace cv;
using namespace std;

int main(){

Input parameters are :

int Nb_Data_Class_1 = 10;
int Nb_Data_Class_0 = 5;
int Height_Zone = 200;
int Width_Zone = 200;

so I will resize all my files to 200x200 format.

string Path = "C:\\Users\\....";
string Format = ".jpg";

int Nb_Files = Nb_Data_Class_1 + Nb_Data_Class_0;
Mat TrainingMat(Nb_Files, Zone_Image, CV_32FC1);
Mat TrainingLabels(Nb_Files, 1, CV_32FC1);

For every file of the class labelled {1} - they are all named Tree01, Tree02, etc. - I open, and resize.

for (int i = 0; i < Nb_Data_Class_1; ++i)
{
    stringstream ss;
    ss << Path << "\\Tree0" << i + 1 << Format;
    Mat Image = cv::imread(ss.str(), 0);
    resize(Image, Image, Size(Width_Zone, Height_Zone));}

Things worked perfectly without the last line. I had a Mat array, filled with 0-t0-255 numbers. Now, I get the following error :

OpenCV Error: Assertion failed <ssize.area<> >0> in cv::resize, 
    file C:\builds\2-4-PackSlave-win32-vc12-shared\opencv\modules\imgproc\serc\imgwarp.cpp, line 1824

What could be the problem ? I thought that maybe OpenCV wasn't properly opening the files ; but, in that case, how everything could have been previously working ? Still wondering.

Any help would be much appreciated ! Thanks in advance.

like image 993
Parveez Avatar asked Feb 05 '14 15:02

Parveez


2 Answers

The only reason for resize to crush is absence of Image. Even if you checked that some of the images were read properly it doesn't mean that all of them were - some of them may be missing. Reading files from disk is a very common point of failure for programs because you never can be sure if the read was successfully or not. As a result every time you read an image you really really should verify that it is not empty:

if (Image.cols == 0) {
     cout << "Error reading file " << ss << endl;
     return -1;
}
like image 192
Michael Burdinov Avatar answered Oct 23 '22 09:10

Michael Burdinov


Not going to solve the problem in this case, but this assertion can also be caused by trying to resize a Mat with a signed type like CV_8SC3. For example:

Mat wrong = Mat::zeros(4, 4, CV_8SC3); // <- Notice 'S'
Mat right = Mat::zeros(4, 4, CV_8UC3); // <- Notice 'U'

imshow("OK", right);
imshow("ASSERTS", wrong);

Note that checking wrong.cols != 0 will not prevent this from crashing.

like image 27
Rick Smith Avatar answered Oct 23 '22 10:10

Rick Smith