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就是获取库下指定的文件。否则,编译通过无法链接,报出无法解析的外部符号


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

meadl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值