C++,OpenCV:调整大小时断言失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21581858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:43:06  来源:igfitidea点击:

C++, OpenCV : Assertion failed in Resize

c++opencvresize

提问by Parveez

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 answersall over the Internet, but none was applicable for my problem.

作为一名 C++ 初学者,我目前面临着一个不知何故无法解决的问题,即使代码非常简单。我一直在互联网上寻找答案,但没有一个适用于我的问题。

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.

我目前正在使用 OpenCV 2.4.8 在 VS2013 下用 C++ 编写基本的 SVM。我能够处理相同大小的图像,在代码的开头指定固定的高度和宽度。

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. Now, I get the following error :

没有最后一行,事情就完美了。我有一个 Mat 数组,填充了 0-t0-255 个数字。现在,我收到以下错误:

OpenCV Error: Assertion failed <ssize.area<> >0> in cv::resize, 
    file C:\builds-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.

可能是什么问题呢 ?我想可能是 OpenCV 没有正确打开文件;但是,在这种情况下,以前怎么可能一切正常?还在疑惑。

Any help would be much appreciated ! Thanks in advance.

任何帮助将非常感激 !提前致谢。

采纳答案by Michael Burdinov

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

回答by Rick Smith

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

在这种情况下不会解决问题,但此断言也可能是由于尝试Mat使用诸如CV_8SC3. 例如:

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 != 0will not prevent this from crashing.

请注意,检查wrong.cols != 0不会阻止它崩溃。

回答by Shevon Silva

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\....";" line to: string Path = "C:\Users";

更改 "string Path = "C:\Users\....";" 行至:字符串路径 = "C:\Users";

and change "ss << Path << "\Tree0" << i + 1 << Format;" line to: ss << Path << "Tree0" << i + 1 << Format;

并更改 "ss << Path << "\Tree0" << i + 1 << Format;" 行到: ss << Path << "Tree0" << i + 1 << Format;