Ofx does not recognize native openCV functions

I am using ofxOpenCv and my update function has the following code:

void ofApp::update(){
	video.update();		//Decode the new frame if needed

	if ( video.isFrameNew() ) {
        //Convert to ofxCv images
        ofPixels & pixels = video.getPixels();
        currentColor.setFromPixels( pixels );
        gray = currentColor;
        Mat img1 = Mat(gray.getHeight(), gray.getWidth(), CV_8UC1, gray.getCvImage(), 0);
        Mat img2 = Mat(gray.getHeight(), gray.getWidth(), CV_8UC1, gray.getCvImage(), 0);
        Mat flow;                        //Image for flow
        calcOpticalFlowFarneback( img1, img2, flow, 0.7, 3, 11, 5, 5, 1.1, 0 );
        Mat imageCV = Mat(gray.getHeight(), gray.getWidth(), CV_8UC1, gray.getCvImage(), 0);
        vector<Point2f> corners;
        goodFeaturesToTrack(imageCV, corners, 200, 0.01, 4); //param

When I try to compile it throws an error saying:
use of undeclared identifier goodfeaturestotrack
use of undeclared identifier calcOpticalFlowFarneback

I am not sure why this stopped working. I have called the using namespace cv; command and the image processing function of openCV should be visible. The good features to track function is declared inside here: ofxOpenCv/libs/opencv/include/opencv2/imgproc.hpp so I don’t know why it can’t see that and the optical flow one. This example used to work on ofx 0.0.98. It’s something that changed with 0.10. (Error both on Mac Mojave and Linux Mint 19.01

Any ideas greatly appreciated :slight_smile:


Hi Theo, :slight_smile:

does it work with
put in ofApp.cpp

#include "imgproc.hpp"

hope this helps.

Roy to the rescue!!! Thanks :slight_smile:

I don’t know why these openCV examples I have for class start failing now and they need specific inclusion of the underlying openCV hpp files.
In the past when I included ofxOpenCV and did the namespace thing I could access the underlying/native openCV stuff without issue (on 0.9.8).

Could it be that someone by mistake removed a reference to the native openCV files? What’s baffling is that I could call the cvCalcOpticalFlowFarneback (the C implementation) but not the C++ one (calcOpticalFlowFarneback). This is the reason I think someone might have removed an include by mistake.

this is probably just something that changed in opencv itself not that we changed in OF

Hi, I personally prefer to use ofxCv rather than ofxOpenCv.
besides that,
ofxOpenCv is only including the C header #include "cv.h", while ofxCv includes the C++ one #include "opencv2/opencv.hpp"

Thanks Roy. That fixed all the problems in linux too :). Works smoothly now. I sort of suspected something like that because when I included the ofxCv it worked fine… so @kylemcdonald probably includes the C++ version of openCV.

Any idea why it needs this line from now on? Should I investigate, add where it’s missing and do a pull request? @arturo you don’t think it’s an ofxOpenCv issue, rather than an opencv one?

Thanks guys for help… :slight_smile:

Hi Theo,
it is in fact an opencv issue. OF 0.9.8 used opencv 2.4.8 where the cv.h file included the C++ headers, as you can see here, while OF 0.10 uses opencv 3.1 which does not include those headers as you can see here THe fix would be to add the C++ headers to ofxOpenCv by adding #include "opencv2/opencv.hpp".

yeah, that’s what i meant if you want to send a PR with that change that would be great

sure. I’ll do it later today

@arturo @marinero
Just made the PR, here


I LOVE open source software + the ofx community!

Thanks both :slight_smile: