6

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("My Window", CV_WINDOW_AUTOSIZE);
cv::imshow("My Window", img1);

Visual Studio returnes an unhandled exception and the Console returns:

OpenCV Error: bad flag (parameter or structure field) 
(Unrecognized or unsupported array type) in unknown function, 
file ..\..\..\..\ocv\opencv\src\cxcore\cxarray.cpp, line 2376

The image is not displayed. Furthermore the window "My Window" has a strange caption: "ÌÌÌÌMy Window", which is not dependent on the name.

The "old" C API using commands like cvLoadImage, cvNamedWindow or cvShowImage works without any problem for the same image file. I tried a lot of different stuff without success.

I appreciate any help here.

Konrad

1
  • 1
    It looks like the imread call is failing; you can check that by testing for img1.data == NULL. Unfortunately, I just tried it and I'm getting the same problem here. Commented Apr 12, 2010 at 16:01

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, that's a useful approach - works fine and helps me a lot.
Glad to help. Could I bother you to click the up-arrow and checkmark next to this post? :)
6

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.

3 Comments

Oh, of course, that would cause problems. Nice catch! Remember to link the non-debug versions for your release mode build, though.
appears not to work with latest stable or dev: 2.3.1 or 2.3.2d
i also faced up that problem and have been streesed, Thank you very much for that
3

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.

Comments

2

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.

Comments

2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.