Ask Your Question
0

OpenCV cannot load images

asked 2015-10-19 12:40:00 -0600

Icoria gravatar image

Hi, I'm using the code from "https://help.ubuntu.com/community/OpenCV" in the C++ section, and I'm having trouble with the following code:

#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main()
{

    Mat img = imread("/home/courtneym/Pictures/index.jpeg",CV_LOAD_IMAGE_COLOR);
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

I keep getting this error when I try to run it:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/courtneym/opencv/modules/highgui/src/window.cpp, line 276 terminate called after throwing an instance of 'cv::Exception' what(): /home/courtneym/opencv/modules/highgui/src/window.cpp:276: error: (-215) size.width>0 && size.height>0 in function imshow

Aborted (core dumped)

edit retag flag offensive close merge delete

Comments

2

Use the search bar of the forum to find several different threads with solutions to this problem. First of all, I'd check if the image is indeed index.jpeg and not index.jpg

LorenaGdL gravatar imageLorenaGdL ( 2015-10-19 12:58:17 -0600 )edit
3

that ubuntu page should be erased.

berak gravatar imageberak ( 2015-10-19 13:01:37 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2015-10-20 04:39:54 -0600

updated 2015-10-20 15:18:33 -0600

LorenaGdL gravatar image

Please add the following code

Mat img = imread("/home/courtneym/Pictures/index.jpeg",CV_LOAD_IMAGE_COLOR);
if(img.empty()){
    std::cout << "Path incorrectly or filename wrong, image not read!" << std::endl;
    return -1;
}
imshow("opencvtest",img);
waitKey(0);

And see if the error appears or not!

edit flag offensive delete link more

Comments

These are the errors I got:

link text

Icoria gravatar imageIcoria ( 2015-10-20 12:48:02 -0600 )edit
1

Check again with the edited code (you can also write using namespace std; and use previous code)

LorenaGdL gravatar imageLorenaGdL ( 2015-10-20 15:19:26 -0600 )edit
1

Ow man, not to be rude or so, but if you have problems with fixing namespace errors, then why are you attempting computer vision programming? Seems to me you need to start with a basic C++ course to get a grasp of programming concepts...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-21 02:56:51 -0600 )edit

For your information, this was someone /else's/ code, and I am borrowing it. I tried using various openCV codes for my program and none of them work, no matter what imports or whatever are in there. Don't lecture me about needing basic C++ courses, I'm fully aware of "using namespsace std" and that did not fix the problem. None of the solutions that you guys have offered so far are working, so don't act so high and mighty.

Icoria gravatar imageIcoria ( 2015-10-21 11:56:24 -0600 )edit

One more thing, I used the correct path, and the correct filename.

Icoria gravatar imageIcoria ( 2015-10-21 12:02:08 -0600 )edit

So you're fully aware but still don't fix it so the program can compile? Weird, to say the least. IMHO you're the only one acting high, because here we run into dozens of people that are not well versed in C++ programming and that do need indications such as using namespace std and take basic programming lessons before trying to build your desired time-machine. I told before and I'll tell once again: there are plenty of threads in the forum which show tips to deal with problems about imread not loading images. Take a look at them and try the solutions, and if nothing works, come back and ask again.

LorenaGdL gravatar imageLorenaGdL ( 2015-10-21 12:11:54 -0600 )edit

I did add using namespace std; but it didn't change anything

Icoria gravatar imageIcoria ( 2015-10-21 12:16:14 -0600 )edit

also I've been checking other threads but I've not yet found one that works for my issue

Icoria gravatar imageIcoria ( 2015-10-21 12:17:37 -0600 )edit

Suggestions:

· Only keep minimum necessary code (and in any case, delete those old C headers):

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main()
{
    Mat img = imread("/home/courtneym/Pictures/index.jpeg",CV_LOAD_IMAGE_COLOR);
    if(img.empty()){
        std::cout << "Incorrect path or wrong filename, image not read!" << std::endl;
        return -1;
    }
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

· Though it's normally recommended the way around, try to put your image in the code directory (here it seems? /home/courtneym) and use just relative path (Mat img = imread("index.jpeg",CV_LOAD_IMAGE_COLOR);)

LorenaGdL gravatar imageLorenaGdL ( 2015-10-21 12:57:27 -0600 )edit

I did that:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main()
{

    Mat img = imread("index.jpeg",CV_LOAD_IMAGE_COLOR);

    if(img.empty())
    {
        cout << "Path incorrectly or filename wrong, image not read!" << endl;
        return -1;
    }
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

but now it's saying:


    g++ -Wall -c "opencvtest.cpp" (in directory: /home/courtneym)
    opencvtest.cpp: In function ‘int main()’:
    opencvtest.cpp:15:3: error: ‘cout’ was not declared in this scope
       cout<< "Path incorrectly or filename wrong, image not read!" << endl;
       ^
    Compilation failed.

Icoria gravatar imageIcoria ( 2015-10-21 13:39:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-19 12:38:11 -0600

Seen: 4,797 times

Last updated: Oct 20 '15