好的,我知道这个问题可能不是新问题,而且我已经浏览了几篇涉及同一问题的帖子,但并没有真正帮助。我是opencv的新手,我正在尝试使用imread加载图像(在与存储可执行文件的文件夹不同的文件夹中)并使用imshow显示它。它是更大代码的一部分,但我在这里将涵盖该问题的部分显示为单独的代码:
#include <stdio.h>
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv/highgui.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include <fstream>
#include <sstream>
#include <cctype>
#include <iterator>
#include <cstring>
#include <highgui.h>
#include <string.h>
int disp(char * filename);
using namespace std;
using namespace cv;
int main()
{
disp("file.txt");
}
int disp(char * filename)
{
FILE * fp;
char shr[50];
char ch;
if( !(fp = fopen(filename, "rt")) )
{
fprintf(stderr, "Can\'t open file %s\n", filename);
return 0;
}
for(i=0;ch != '\n';i++)
{
ch = fgetc(imgListFile);
shr[i] = ch;
}
string str(shr);
Mat image=imread(str.c_str(),1);
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image",image);
cvWaitKey(0);
}
“file.txt”是一个文本文件,其中包含我要加载和显示的图像的完整路径。我正在将它读入字符数组,将其转换为字符串并将其传递给 imshow/imread 函数。编译时我没有收到任何错误,但是,我在运行代码时遇到了错误:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/ubuntu/Desktop/OpenCV/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
terminate called after throwing an instance of 'cv::Exception'
what(): /home/ubuntu/Desktop/OpenCV/opencv-2.4.6.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
Aborted (core dumped)
我尝试调试代码,甚至重新编译了opencv;但我一次又一次地遇到同样的问题。我需要帮助 !!!
希望我已经正确解释了我的问题。提前致谢 !!!
PS:文本文件实际上每个图片路径前都包含一个数字;我需要删除数字,然后才能将路径提供给 imshow/imread 函数;这就是我试图读取文本文件并存储在字符数组中的原因(这样我就可以首先摆脱前 2 个字符)。