Using Converter class to use cv::undistort in Matlab

OpenCV function

void undistort(const Mat& src, Mat& dst, const Mat& cameraMatrix, const Mat& distCoeffs, const Mat& newCameraMatrix=Mat())

Matlab

dst=mexUndistort(src,cameraMatrix,distCoeffs)

MEX function source

#include "mex.h"
#include "matcv.h" 
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, mxArray *prhs[])
{
    if(nrhs!=3)
    {
         mexErrMsgTxt("Need exactly 3 inputs src,  cameraMatrix,  and  distCoeffs.n");
    }
    
    cv::Mat Inputs[3];
    cv::Mat Output;
    
    for(int i=0;i<nrhs;i++)
    {
        Inputs[i]=Converter(prhs[i]);
    }
    
    cv::undistort(Inputs[0],Output,Inputs[1],Inputs[2]);
    
    plhs[0]=Converter(Output);
    
    return;
}


To compile you need opencv and boost. Mex command I used with Matlab R2011a and VC9 goes like this:

mex mexUndistort.cpp matcv.cpp -IC:OpenCV2.3buildinclude -ID:NinadToolsboost_1_47_0 -LC:OpenCV2.3buildx64vc9lib -lopencv_core230 -lopencv_imgproc230   -v COMPFLAGS=”/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /DMATLAB_MEX_FILE /nologo /MD”

Leave a Reply

Your email address will not be published. Required fields are marked *