1
votes

OK, i know this question may not be new and I've already gone through a few posts covering the same issue but it hasn't really helped. I am new to opencv and I am trying to load an image (in a folder that's different from the one where the executable file's stored) using imread and display it using imshow. Its the part of a much bigger code but I've shown the part that covers the issue as a separate code here:

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

The "file.txt" is a text file containing the full path of the image i want to load and display. I am reading it into a character array, converting it to a string and passing it to the imshow/imread functions. I am not getting any errors while compiling, however, i am getting an error while i run the code:

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)

I tried debugging the code, even re-compiled opencv; but i am getting the same issue again & again. I need help !!!

Hope i've explained my issue properly. Thanks in advance !!!

P.S: The text file actually contains a number before every image path; and i need to remove the number before i can feed the path to the imshow/imread functions; that's the reason i am trying to read the text file and store in a character array (so that i can get rid of the first 2 characters first).

3

3 Answers

2
votes

The error message tells you that the image a 0 rows and/or 0 columns. This is usually caused by an incorrect path to image, or by an an image type that is not handled by your installation of OpenCV.

To debug it, you need to print out the argument of imread() and compare it with the catual location of the file on your system.

1
votes

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.

-1
votes

There are some typos in your code. Also, it appears as if you haven't completely grasped the concepts of file handling and string allocations using c++; I would advise you to read up on those.. I have re-written your code as follows,

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

Hope this helps.