CoInitializeEx Error When Calling OpenCV Method cvLoadImage ()

I am trying to integrate some OpenCV features into my application. I currently have a code configured using DirectShow to receive a video from my camera, which is then displayed in the MFC window. This code cannot be changed or deleted.

The code works fine, but regardless of location, I put the following line of code:

IplImage *img = cvLoadImage("C:/well.jpg"); 

The webcam cannot initialize correctly and breaks the program.

I get FAILED HRESULT at:

 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) 

More specifically, at some point in my code, I Call CDialog :: doModal (), which then lands on CoInitializeEx (), and thus the program crashes.

Does anyone know what is going on here?

+4
source share
3 answers

CoInitialize will fail if the stream was previously initialized as another apartment, that is, if there was a previous CoInitializeEx (NULL, COINIT_MULTITHREADED)

I would suggest that OpenCV calls CoInitializeEx (NULL, COINIT_MULTITHREADED), resulting in your subsequent calls to CoInitializeEx fail. You can confirm this by checking the CoInitializeEx return - in this case it will be RPC_E_CHANGED_MODE.

There is no direct solution, the easiest way is to move OpenCV calls to a separate thread.

+4
source

In addition to what Michael said, checking also for external dependent DLLs, if CoInitialize is missing, will also fail.

+1
source

I had a similar problem. In my MFC application, the AfxOleInit call ended with RPC_E_CHANGED_MODE .

I cannot ignore the failure (I need COM inside the application), and I cannot move the OpenCV call to another thread (as Michael rightly suggests).

I found the stream "wxwidgets and opencv 1.1 ole initialization error" that solves my problem: I do not need video capture support from OpenCV and therefore I can remove #define HAVE_VIDEOINPUT 1 as suggested in http://tech.dir.groups.yahoo. com / group / OpenCV / message / 60060

go to _highgui.h , comment line 96 (" #define HAVE_VIDEOINPUT 1 ") and recompile

It works with OpenCV_1.1pre1a.

0
source

All Articles