OpenCV 2.0 C++ API using imshow: returns unhandled exception and “bad-flag”

后端 未结 6 1021
生来不讨喜
生来不讨喜 2020-12-07 02:08

I\'m trying to use the new OpenCV 2.0 API in MS Visual C++ 2008 and wrote this simple program:

cv::Mat img1 = cv::imread(\"image.jpg\",1);
cv::namedWindow(\"         


        
相关标签:
6条回答
  • 2020-12-07 02:46

    Firstly, you'd better compile your own version OpenCV.

    I had the same error with the build (I got from Sourceforge), and solved by compiling my own version in debug and release versions.

    And make sure you change the original system env variable PATH to the new build folder build/bin, then you could build and run the imshow() in Debug mode.

    0 讨论(0)
  • 2020-12-07 02:57

    I believe this might be related to unicode.

    Try the macro _TEXT()

    For example:

    cv::Mat img1 = cv::imread(_TEXT("image.jpg"),1);
    

    Unicode in Visual C++ 2

    0 讨论(0)
  • 2020-12-07 02:58

    I had the same problem described above which turns out to be caused by the settings of the linker.

    I found the answer in another thread, OpenCV 2.3 and Visual Studio 2010.

    To repeat it here:

    Properties of your project (right click on it)

    • C/C++
      • General
        • include directory add the < your directory >\OpenCV2.3\include\opencv2, < your directory >\OpenCV2.3\include\opencv and < your directory >\OpenCV2.3\include
    • Linker

      • General
        • List item
      • Input
        • Add all the libs like opencv_core230d.lib opencv_highgui230d.lib and so on...

    Once I've done the above, I can run imshow and imread + all other cpp functions seamlessly! OP's problem has probably already been resolved, but hopefully this will be useful to other people who are led here looking for the same solution.

    0 讨论(0)
  • 2020-12-07 02:59

    As I just commented, imread isn't working for me either. A little googling shows other people having the same problem; I guess it's a bug in the library code. For now, here's a hacky workaround:

    IplImage* img = cvLoadImage("lena.jpg");
    cv::Mat lena(img);
    cvReleaseImage(&img);
    

    This way, you can at least use the C++ API for the rest of your stuff.

    0 讨论(0)
  • There's help for this issue.

    The solution is, that the usual proposed opencv library files in the linker are not working properly. Instead try to use the debug library files by this:

    In Visual C++:

    go to Project->Properties (or Alt-F7) Configuration Properties->Linker->Input->Additional Dependencies

    replace the usual " cv210.lib cxcore210.lib highgui210.lib" by " cv210d.lib cxcore210d.lib highgui210d.lib" - which are the debugging libraries.

    The OpenCv 2.0 API commands should work now.

    0 讨论(0)
  • 2020-12-07 03:09

    Are you sure you added the whole path starting from /home/.... I had the same problem as you but when I added the whole path, things work out pretty well. The whole path had to be added despite the fact the path exists in the include files.

    imread in openCV unlike Matlab does not return an error when file/folder is not found - instead it returns a null matrix, which in turn is reflected as an error during imshow. Also, imread does not look for image files in the included folders or the workspace. So, specify the entire path whenever possible.

    Please take a note of this for future references.

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