天天看點

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就是擷取庫下指定的檔案。否則,編譯通過無法連結,報出無法解析的外部符号

繼續閱讀