load image with openCV Mat c++

前端 未结 5 778
梦谈多话
梦谈多话 2020-11-30 15:26

I want to load an image using Mat in openCV

My code is:

Mat I = imread(\"C:/images/apple.jpg\", 0);
namedWindow( \"Display window\", CV_WINDOW_AUTOSI         


        
相关标签:
5条回答
  • 2020-11-30 15:35

    Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.

    0 讨论(0)
  • 2020-11-30 15:45

    Have you checked that I exists after imread? Perhaps the file read failed

    After reading a file do if ( I.empty() ) to check if it failed

    0 讨论(0)
  • 2020-11-30 15:47

    I don't know why you don't have include problem because normally it .hpp file so you're suppose to do

    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2\core\eigen.hpp>
    

    But your code seems good but add a cv::waitKey(0); after your imshow.

    0 讨论(0)
  • 2020-11-30 15:55

    I've talked about this so many times before, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:

    Mat I = imread("C:\\images\\apple.jpg", 0);
    if (I.empty())
    {
        std::cout << "!!! Failed imread(): image not found" << std::endl;
        // don't let the execution continue, else imshow() will crash.
    }
    
    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", I ); 
    waitKey(0);
    

    Note that Windows' path uses backslash \ instead of the standard / used on *nix systems. You need to escape the backslash when passing the filename: C:\\images\\apple.jpg

    Calling waitKey() is mandatory if you use imshow().

    EDIT:

    If it's cv::imread() that is throwing the exception the only solution I know to work is downloading OpenCV sources and building it on the machine, since re-installing OpenCV doesn't fix the issue.

    0 讨论(0)
  • 2020-11-30 15:57

    As pointed out by @karlphillip, however trivial it may sound but this statement " You need to escape the backslash when passing the filename: C:\images\apple.jpg" is really important.

    0 讨论(0)
提交回复
热议问题