1

好的,我知道这个问题可能不是新问题,而且我已经浏览了几篇涉及同一问题的帖子,但并没有真正帮助。我是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 个字符)。

4

3 回答 3

2

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

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

于 2013-10-31T08:44:18.487 回答
1

your for loop here is broken:

for(i=0;ch != '\n';i++)
{
    ch = fgetc(imgListFile);
    // you have to check the value of ch *after* reading
    if ( ch == '\n' )
        break; // else, you still append the '\n' to your filename
    shr[i] = ch;    
}
// z terminate
shr[i] = 0;

so, - you get empty images because of broken path.

于 2013-10-31T09:12:37.537 回答
-1

您的代码中有一些拼写错误。此外,您似乎还没有完全掌握使用 c++ 进行文件处理和字符串分配的概念;我建议您阅读这些内容。我已将您的代码重写如下,

int main()
{
    disp("file.txt");
    return EXIT_SUCCESS;
} 

void disp(char* filename)
{
    ifstream myReadFile;
    char shr[500];
    myReadFile.open(filename);

    if(myReadFile.is_open())
            while(!myReadFile.eof())
                    myReadFile >> shr;

    string str(shr);
    Mat image=imread(str.c_str(),1);    
    namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
    imshow( "Display Image",image);
    cvWaitKey(0);
}

希望这可以帮助。

于 2013-10-31T09:23:56.320 回答