简体   繁体   English

C ++,OpenCV:Resize中的断言失败

[英]C++, OpenCV : Assertion failed in Resize

As a C++ beginner, I am currently facing a problem I somehow can't solve, even if the code is pretty simple. 作为一名C ++初学者,我目前面临一个问题,即使代码非常简单,我也无法解决。 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. 我目前正在使用OpenCV 2.4.8在VS2013下使用C ++编写基本SVM。 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. 所以我将所有文件的大小调整为200x200格式。

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. 对于标记为{1}的类的每个文件 - 它们都被命名为Tree01,Tree02等。 - 我打开并调整大小。

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. 我有一个Mat数组,填充0-t0-255数字。 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 ; 我想也许OpenCV没有正确打开文件; but, in that case, how everything could have been previously working ? 但是,在那种情况下,以前的一切都可以如何运作? Still wondering. 仍在疑惑。

Any help would be much appreciated ! 任何帮助将非常感激 ! Thanks in advance. 提前致谢。

The only reason for resize to crush is absence of Image. 调整大小的唯一原因是缺少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;
}

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 . 在这种情况下不会解决问题,但是这个断言也可能是由于尝试使用像CV_8SC3这样的带符号类型来调整Mat大小。 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. 请注意,检查wrong.cols != 0不会阻止此崩溃。

Your line: ss << Path << "\\Tree0" << i + 1 << Format; 你的行:ss << Path <<“\\ Tree0”<< i + 1 << Format;

will produce (where i=0): "C:\\Users\\....\\Tree01.jpg". 将产生(其中i = 0):“C:\\ Users \\ .... \\ Tree01.jpg”。

Solution

Change "string Path = "C:\\Users\\....";" 更改“string Path =”C:\\ Users \\ ....“;” line to: string Path = "C:\\Users"; line to:string Path =“C:\\ Users”;

and change "ss << Path << "\\Tree0" << i + 1 << Format;" 并改变“ss << Path <<”\\ Tree0“<< i + 1 << Format;” line to: ss << Path << "Tree0" << i + 1 << Format; line to:ss << Path <<“Tree0”<< i + 1 << Format;

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

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