Search code examples
c++matlabopencvmex

How to send Mat H=findHomography return to Matlab


I try to use the OpenCV Function findHomography. I use Mex OpenCV. My Problem is how can I return the Mat H Array to MATLAB. I hope anybody can help me :-)

Example code is:

enter code here

#include "opencvmex.hpp"
#include "math.h"
#include "mex.h"
#include "matrix.h"

using namespace std;
using namespace cv;

void mexFunction(int nlhs, mxArray *plhs[], 
            int nrhs, const mxArray *prhs[]){


    //create "CV_32FC2" Array       
    Mat srcPoints(4,1,CV_32FC2);

    srcPoints.at<int>(0)=221;
    srcPoints.at<int>(1)=227;              
    srcPoints.at<int>(2)=237;
    srcPoints.at<int>(3)=255;

    Mat dstPoints(4,1,CV_32FC2);

    dstPoints.at<int>(0)=120;
    dstPoints.at<int>(1)=67;
    dstPoints.at<int>(2)=91;
    dstPoints.at<int>(3)=113;


    // findHomography

    Mat H;

    int method=0;
    double ransacReprojThreshold=3;
    int maxIters = 2000;
    double confidence = 0.995;        

     H = findHomography(srcPoints,dstPoints);
  }

Solution

  • You can use ocvMxArrayFromMat_{DataType}, in your case probably best to use:

    plhs[0]=ocvMxArrayFromMat_double(H);
    

    Here is the documentation of the OpenCV interface, so you can look at the available fucntions.