C ++,OpenCV:Assertion在调整大小失败 [英] C++, OpenCV : Assertion failed in Resize

查看:423
本文介绍了C ++,OpenCV:Assertion在调整大小失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个C ++初学者,我目前面临一个问题,我不知何故不能解决,即使代码是很简单。
我一直在网上搜索答案,但没有适用于我的问题。

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.

我目前使用OpenCV 2.4.8在VS2013下编写基本的SVM与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.

现在,我试图:打开不同大小的图片,将它们调整到一定的较小的大小,并将以前的代码应用到现在调整大小的数据集。很简单。

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.

这是代码的开头:

#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(){

输入参数为:

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

,所以我将所有文件调整为200x200格式。

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

对于标记为{1}的类的每个文件,它们全部命名为Tree01,Tree02等。 - 我打开并调整大小。

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

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

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错误:断言失败> 0>在cv :: resize,文件C:\builds\2- 4-PackSlave-win32-vc12-shared\opencv\modules\imgproc\serc\imgwarp.cpp,第1824行

是问题吗?
我认为也许OpenCV没有正确打开文件;但在这种情况下,一切都可能以前工作?
仍然想知道。

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.

推荐答案

调整大小的唯一原因是缺少图片。即使你检查了一些图像是否正确读取并不意味着所有的图像 - 其中一些可能会丢失。从磁盘读取文件是程序的一个常见的失败点,因为你永远不能确定读取是否成功。因此,每次你读一个图片,你真的应该验证它不是空的:

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

这篇关于C ++,OpenCV:Assertion在调整大小失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆