Unknown array type in function cvarrToMat (OpenCV, example from ofDemystified book)

#1

I’m trying to run the VideoMorphing example from the book “Mastering openFrameworks: Creative Coding Demystified”. github example

With some minor syntax updates it looks like this:

void ofApp::setup()
{
    ofImage imageOf1, imageOf2;            //Load openFrameworks' images
    imageOf1.load("1.png");
    imageOf2.load("2.png");
        
    color1.setFromPixels( imageOf1 );    //Convert to ofxCv images
    color2.setFromPixels( imageOf2 );
    
    float decimate = 0.3;              //Decimate images to 30%
    ofxCvColorImage imageDecimated1;
    imageDecimated1.allocate( color1.width * decimate,
                             color1.height * decimate );
    //High-quality resize
    imageDecimated1.scaleIntoMe( color1, CV_INTER_AREA );
    gray1 = imageDecimated1;
    
    ofxCvColorImage imageDecimated2;
    imageDecimated2.allocate( color2.width * decimate,
                             color2.height * decimate );
    //High-quality resize
    imageDecimated2.scaleIntoMe( color2, CV_INTER_AREA );
    gray2 = imageDecimated2;
    
    Mat img1 = cvarrToMat(gray1.getCvImage(),true);
    Mat img2 = cvarrToMat(gray2.getCvImage(),true);
    
    Mat flow;                        //Image for flow
    //Computing optical flow
    cvCalcOpticalFlowFarneback(gray1.getCvImage(), gray2.getCvImage(), &flow, 0.7, 3, 11, 5, 5, 1.1, 0);
 //.. stopping here since the error occurs at the line above
}

It compiles, but the last line throws this OpenCV Error:

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /Users/travis/build/openframeworks/apothecary/apothecary/build/opencv/modules/core/src/matrix.cpp, line 893
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/travis/build/openframeworks/apothecary/apothecary/build/opencv/modules/core/src/matrix.cpp:893: error: (-5) Unknown array type in function cvarrToMat

I made sure the images are actually getting loaded. Not sure what to do here. Any suggestions?

#2

To answer my own question in case someone else struggles: the problem is mixing c and c++ bindings.

I used cvCalcOpticalFlowFarneback() because it’s the only thing exposed by ofxOpenCV in of0.10, but adding

#include "opencv2/opencv.hpp" to ofxOpenCV.h lets you use calcOpticalFlowFarneback() instead and everything works as it should.