OPenCV 2.0 and OF ?

Hi,

I want to set the last opencv version 2.0 because it gives a lot of new opportunities (So many new functions !).

So, someone has already experimented this ?

Do you think it will be easy to do ?

I have some problems for the moment with some dll…

as with this function :

  
cvCvtColor( mom.getCvImage(), cvImage, CV_RGB2GRAY );  

Is it just very incompatible or I just do something wrong ?

Thanks for your advices…

Nico

Hi,

I managed to compile and use OpenCV 2.0 and ofxOpenCv working on os x snow leopard.
It’s a bit tricky under Linux cause of a bug (see http://sourceforge.net/tracker/index.ph-…-tid=376677). To solve the problem you need to change #include order for ofxOpenCv.h to be included before ofMain.h…

Bests,
F.

I’m working with Nico on this problem, and I think I have kind of isolated where the problem comes from.

Actually it has nothing to do with libraries or dlls. OpenCV 2.0 comes with precompiled libraries for MinGW on windows that are perfectly working. Also I’m pretty sure the version of ofxOpencv coming with 0.6 is 100% compatible with OpenCV 2.0 …

I’ve made some test and it seems that the problem comes from how OF is handling the main loop but I have no idea why it is doing that and how to solve it!

Here’s some code to illustrate what’s working and what is not:

testApp.h :

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "cv.h"  
#include "highgui.h"  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
  
		testApp();  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void resized(int w, int h);  
  
        IplImage* pImg;  
        IplImage* gray;  
};  
  
#endif  
  

testApp.cpp :

  
#include "testApp.h"  
#include "stdio.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    gray = cvCreateImage( cvSize(512,512), IPL_DEPTH_8U, 1 );  
  
    if((pImg = cvLoadImage("lena.jpg", 1)) != 0){  
  
        cvNamedWindow( "Image", 1 );  
        cvNamedWindow( "Image2", 1 );  
  
        cvCvtColor( pImg, gray, CV_RGB2GRAY );  
  
        cvThreshold( gray, gray, 40, 255, CV_THRESH_BINARY_INV );  
  
        cvShowImage( "Image", pImg );  
        cvShowImage( "Image2", gray );  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
    // cvCvtColor( pImg, gray, CV_RGB2GRAY );  
    // cvThreshold( gray, gray, 40, 255, CV_THRESH_BINARY_INV );  
}  
  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    // cvCvtColor( pImg, gray, CV_RGB2GRAY ); // doesn't work  
    // cvThreshold( gray, gray, 40, 255, CV_THRESH_BINARY_INV ); // doesn't work  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){  
        cvCvtColor( pImg, gray, CV_RGB2GRAY );  
        cvThreshold( gray, gray, mouseX, 255, CV_THRESH_BINARY_INV );  
        cvShowImage( "Image", pImg );  
        cvShowImage( "Image2", gray );  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
        cvCvtColor( pImg, gray, CV_RGB2GRAY );  
        cvThreshold( gray, gray, mouseX, 255, CV_THRESH_BINARY_INV );  
        cvShowImage( "Image", pImg );  
        cvShowImage( "Image2", gray );  
}  
  

All the uncommented code is working perfectly but the commented code you can see inside testApp::update() makes the application crash without any message. At first I thought that something was happening after the setup that blocks any opencv calls but then I discovered that you could execute the same code inside mousePressed and keyPressed without any problem.

All this make me thing that there is a problem maybe on how ofAppGlutWindow is calling the update and draw methods, maybe something to do with the appPtr, or even with glutMainLoop() … this is really weird! I don’t understand! I can’t find any leads on google and it seems that nobody is talking about the same problem … so I’m kind of stuck here, without any clue!

Any help or suggestions would really save us!

(Also I would love to hear is there is any reason to stay with the old version of OpenCV? I searched the forum about that but I did not find anything)

hmm - that is stange.

I wonder if it could maybe be openmp related. it might be doing some multi-processor / threaded type stuff internally and you only see the issue if you are calling the function faster than it can finish the previous call. a good way to test this is try setting the OF frame rate to 2 or 3 fps and see if you have the issue then.

( ofSetFrameRate(3) in setup )

also make sure just to have the problematic functions in just update ( don’t do update and draw for now ) .

Theo

Hey! Thanks Theo!

I already tried that … without succes!

So remember, it’s working perfectly when you call them inside keyPressed or inside mousePressed, so I agree with you it could be related with some multithreaded stuffs but here’s something else, that is very strange also!

  
  
void testApp::update(){  
    if(doOnce){  
        cvCvtColor( pImg, gray, CV_RGB2GRAY );  
        cvThreshold( gray, gray, 40, 255, CV_THRESH_BINARY_INV );  
        doOnce = false;  
    }  
}  
  

Now if doOnce is initialized at true in the setup, the code is executed as expected only once and without making the application crash. Now the weirdest thing is that if you initialized it at false and then change it to true somewhere else like that it crash :

  
  
void testApp::keyPressed  (int key){  
    doOnce = true;  
}  
  

Also,
This is working perfectly too (with ofSetFrameRate set to 30 here this function execute at the same speed then update):

  
  
void testApp::mouseDragged(int x, int y, int button){  
    cvCvtColor( pImg, gray, CV_RGB2GRAY );  
    cvThreshold( gray, gray, 40, 255, CV_THRESH_BINARY_INV );  
}  
  

So I guess it’s more complicated then just accessing opencv faster than the opencv thread (if there is any separated thread).

Thank you for your help!

the first call to update is done outside of glut main loop since glut calls first draw, but it’s really weird anyway. what platform are you in? i’ve used opencv2.0 in linux and apart from the compiling problem it has been working without problem for me. also i needed to add -fopenmp in the linking flags to make it link

Hello Arturo!
Thanks for the quick reply!

I’m using Code::Blocks in Window Xp. I think Nico is having the same problem using Windows Vista. No linking problems for me, I just had to change two lines in cxoperation header to make it work with CB (as they say it on opencv wiki).

Do you think it can have anything to do with Glut Main lopp itself?

it seems to be a problem with glut main loop, but it’s really weird as there’s no thread in glut and the keys and mouse callbacks happen more or less the same as update/draw, in the same thread and just before each update/draw call. also openmp runs an operation divided in several threads not several operations so it shouldn’t be the problem.

can you try to debug it and post the stacktrace?

Yes it is what I thought… anyway the stacktrace is not that interesting, it just say that it reach the cv call and then crash!

Here it is:

  
  
#0 00F3E5D6	icvBGRx2Gray_8u_CnC1R() (F:\OpenCV\bin\libcv200.dll:??)  
#1 010D9367	cvCvtColor() (F:\OpenCV\bin\libcv200.dll:??)  
#2 00040000	??() (??:??)  
#3 00000001	??() (??:??)  
#4 00000003	??() (??:??)  
#5 00000002	??() (??:??)  
#6 0023FD40	??() (??:??)  
#7 77C35C94	msvcrt!_except_handler2() (E:\WINDOWS\system32\msvcrt.dll:??)  

btw, from this calls:

  
     cvShowImage( "Image2", gray );  

it seems you’re linking highgui too, right? someone has posted in the linux thread that he’s having crashes when linking highgui in a OF app, can you try not including libhighgui in your linker settings, in the stacktrace the error seems to be originated even before that call, don’t know what is this:

  
msvcrt!_except_handler2()  

but it shouldn’t be there.

Well I don’t have any problems with highgui, I can create windows and show images inside the setup without any crashes.

This make me keep thinking that it is a glut issue instead of a opencv issue!

This is really weird!

I can upload a example for windows if it’s any help!

I will also try tomorrow to do it with Visual Studio and see if it’s related in anyway to MinGW…

Anyway, thanks again for your time, we are really lost with this problem!

S.

Wow this is crazy!
Are we the only guys having problems with opencv 2.0 on windows?

Well I really think it could be a good thing to add to openframeworks, it seems a lot faster then the first version and got a lot of new exiting features such as blob tracking, advanced background subtractions (code books, …), detection of hands, skin segmentations, face tracking, … and much more!

I would love to help tracking down all bugs and problems so we can have a new ofxOpencv that works with both versions. For now I can just try on windows and as we are really stuck with this glut thing I can’t really go further without helps of gurus like you!

If you are having problems too, please share it here!

And if you feel like you can resolve this glut issue, it would be verrry kind of you if you could give it a shot!

Thanks a lot!

Simon.

Hi

A good thing to do would be compile OpenCV as non-dll ( ie static library ) and with debugging symbols - then when your app crashed you will be able to jump to exactly which line of code it is.

My gut feeling says it is something to do with openmp / threading of some sorts - but it could just be an OpenCV bug - you might also try grabbing the latest version directly from cvs or svn.

Theo

Thank you theo,
I will try that, but I’m not really good at building libraries with codeblocks! Maybe I could try directly without libraries and directly with the sources…

simongeilfus - when you say the version of ofxopencv in 0.6 is opencv2.0 compatible, I assume that means it uses the Mat objects right? As opposed to IplImage and CvMat.

I have just started a rather large project using opencv2.0 and openframeworks and having that Mat abstraction built in to ofxopencv would make things much cleaner.

Dare I ask… is there a roadmap/timeline for of0.6?

Thanks,

M.

No, actually i wasn’t speaking about this new Mat object, I was just saying that the current version ofxOpenCv should works with the new opencv… so yes it’s still using IplImages and cvmats but well as soon as I get it to works on windows, I will have a look at this new type!

Anyway what platform are you using? Do you make it works with codeblocks on windows?

No I’m afraid not. I’m on osx 10.6.2, using the latest svn update of opencv2.0 compiled as a private framework and OF 0.6 pre.

@mhax: Thanks for the details! :slight_smile:

Simon.

Forget my last posts, I was using the new .a library files with the old dlls, so yes,
Theo, you were right from the beginning!

Using opencv without openmp does the trick!

Thank you for you help! If anyone is interested I can post the library compiled for Codeblocks and MinGW here.