我对mex文件没有什么经验,所以如果这是一个微不足道的问题,请原谅我。我写了一个简单的cpp脚本,它加载两个图像并显示它们,使用opencv库。我试图mex脚本,从matlab中使用它。这是我的脚本
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <map>
#include <math.h>
#include <iostream>
#include <mex.h>
#include "mc_convert.h"
using namespace cv;
using namespace std;
int run(Mat source, Mat target)
{
Mat cimg;
vector<Point> target_samples;
vector<Point> source_samples_chamfered;
vector<Point> source_samples_actual;
// Mat source = imread(argv[1], 0);
// Mat target = imread(argv[2], 0);
double centroid_x_actual, centroid_y_actual;
double centroid_x_chamfered, centroid_y_chamfered;
// Threshold both template and reference image
threshold( target, target, 10, 255, 0 );
cvtColor(target, cimg, COLOR_GRAY2BGR);
threshold( source, source, 10, 255, 0 );
imshow("target", target);
imshow("source", source);
return 0;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
CvMat* source = mxArr_to_new_CvMat (prhs[0]);
CvMat* target = mxArr_to_new_CvMat (prhs[1]);
run(source, target);
// plhs[0] = CvMat_to_new_mxArr (psrc);
cvReleaseMat (&source);
cvReleaseMat (&target);
}
对于在CvMat和MxArray之间转换类型,我使用此链接。当我尝试使用mex编译脚本时,得到以下错误:
mex -cxx -largeArrayDims -I "./" -I/usr/local/include/opencv2 -I/usr/local/include chamfer.cpp -L/usr/local/lib/ -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
chamfer.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
chamfer.cpp:162:46: error: ‘mxArr_to_new_CvMat’ was not declared in this scope
mex: compile of ' "chamfer.cpp"' failed.
"mxArr_to_new_CvMat"在一个名为mc_convert.cpp的文件中声明,该文件是类型转换库的一部分。我已经把所有的头和外部库的cpp文件在同一文件夹中包含chamfer.cpp。请帮助。蒂娅!
在mc_convert.h
中,只有定义了预处理器符号(宏)HAS_OPENCV
才声明mxArr_to_new_CvMat()
。而且,事实上,在doc.htm中的"C/c++ Matlab转换器"下载它说你应该声明这个宏。尝试将-DHAS_OPENCV
添加到您的mex命令中。