天天看点

Matlab、C++混合编程调用OpenCV

https://blog.csdn.net/xidiancoder/article/details/51138388

http://blog.sina.com.cn/s/blog_646e75120100ge8i.html

1.看第二个链接了解函数void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])的基本用法;

2.编写一个点cpp文件,比如videoreadrtsp.cpp,

#include<opencv2/highgui/highgui.hpp>

#include<iostream>

#include "mex.h"

using namespace std;

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

// Check if the input argument is legal

if (nrhs != 1 || !mxIsChar(prhs[0]))

{

mexErrMsgTxt("url should be given.\n");

}

int nStringLen;

nStringLen = mxGetNumberOfElements(prhs[0]);

string path;

path.resize(nStringLen + 1); //new char[nStringLen + 1];

mxGetString(prhs[0], &path[0], nStringLen + 1);

cv::VideoCapture capture(path);

if (!capture.isOpened()){

mexErrMsgTxt("video is not existed");

}

double rate = capture.get(CV_CAP_PROP_FPS);

bool stop(false);

cv::Mat frame;

capture.read(frame);

int row;

int col;

int i,j;

row=frame.rows;

  col=frame.cols;

double *a;

plhs[0]= mxCreateDoubleMatrix(row, col, mxREAL);

a=mxGetPr(plhs[0]);

while (!stop){

for(i=0;i<row;i++){

for(j=0;j<col;j++){

a[j*row+i+1]=(float)frame.at<cv::Vec3b>(i,j)[0];

}

}

if (!capture.read(frame)){

cout << "frame not is existed" << endl;

break;

}  

}

}

这是一个读rtsp的函数,还不好需要改进。

3.按照第一链接添加环境变量

4.MATLAB编译

a.mex -setup

b. mexmvideoreadrtsp.cpp -ID:\opencv\build\include -LD:\opencv\build\x64\vc12\lib -lopencv_core249 -lopencv_imgproc249 -lopencv_highgui249.lib

说明: -Ipathname是Adds 

pathname

 to the list of folders to search for 

#include

 files. ,就是增加搜索include文件的路径,I和pathname之间不要有空格;

—L指定链接库路径,也是你的opencv库,大写L,小写l就是获取库下指定的文件。否则,编译通过无法链接,报出无法解析的外部符号

继续阅读