gpt4 book ai didi

opencv - imshow 断言错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:23 26 4
gpt4 key购买 nike

好吧,我知道这个问题可能不是新问题,我已经看过几篇关于同一问题的帖子,但它并没有真正帮助。我是 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;但我一次又一次遇到同样的问题。我需要帮助!!!

希望我已经正确解释了我的问题。提前致谢!!!

P.S:文本文件实际上在每个图像路径之前包含一个数字;我需要先删除数字,然后才能将路径提供给 imshow/imread 函数;这就是我尝试读取文本文件并存储在字符数组中的原因(这样我就可以先去掉前 2 个字符)。

最佳答案

错误消息告诉您图像有 0 行和/或 0 列。这通常是由于图像路径不正确或图像类型未由您安装的 OpenCV 处理造成的。

要调试它,您需要打印出 imread() 的参数并将其与系统上文件的实际位置进行比较。

关于opencv - imshow 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19701730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com