MatlabR2015b & VSProfessional2013 混合编码连接以及一个问题(opencv)

参考链接,感谢!

http://blog.csdn.net/shaoxiaohu1/article/details/37744309

http://blog.csdn.net/zouxy09/article/details/20553007

http://blog.csdn.net/sinat_29547669/article/details/46757485

 

首先,创建一个接口函数,用matlab代码连接opencv代码,这里我用网上一个例子:(RGB2Gray.cpp)

 

#include "mex.h"

#include "highgui.h"
#include "opencv2/opencv.hpp"

using namespace cv;

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{ 
    if (nrhs != 1 || mxGetClassID(prhs[0]) != mxUINT8_CLASS)
    {
        mexErrMsgTxt("The number of input should be 1, and type first input must be uchar.");
    }
	if (nlhs != 1)
	{
		mexErrMsgTxt("The number of output should 1");
	}
	
    uchar * pData;
    int width, height;

	pData = (uchar *) mxGetPr(prhs[0]);

	height = mxGetM(prhs[0]);
	width  = mxGetN(prhs[0]);

	// 复制数据
	Mat srcImg(height, width, CV_8U);
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			srcImg.at(i, j) = pData[height*j + i];
		}
	}
	
	// 因为C与Matlab的存储方式不同,C是按行存储,Matlab是按列存储
	// 所以将图片转置
	Mat dstImg = srcImg.t();

	/* output data */
	mwSize dims[2] = {height, width};
	plhs[0] = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL);
	memcpy(mxGetPr(plhs[0]), dstImg.data, mxGetNumberOfElements(plhs[0])*sizeof(uchar));
}


然后设定编译器:

 

 

mex -setup


选择: mex -setup C++ 

 

 

选择该路径,敲下命令:

 

mex -g RGB2Gray.cpp -I'D:\Program Files (x86)\opencv\build\include' -I'D:\Program Files (x86)\opencv\build\include\opencv' 
 x64\vc12\lib' 
-lopencv_core248 -lopencv_highgui248 -lopencv_video248 -lopencv_imgproc248

 

 

以上的路径都给为你自己的!


注意:这里如果你的计算机是64位的,就要用x64的编译器,否则就会出现下面的错误,笔者在这里想了很久,后面翻帖子才找到的...

 

测试:找个图片放在该根目录(test.jpg)

 

 >> img = RGB2Gray(‘test.jpg’);
 >> imshow(uint8(img));

 

 

 

 

 

报错:

Error using mex
   正在创建库 RGB2Gray.lib 和对象 RGB2Gray.exp
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPEAX@Z),该符号在函数
"public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl cv::_InputArray::_InputArray(class cv::Mat const &)"
(??0_InputArray@cv@@QEAA@AEBVMat@1@@Z),该符号在函数 "void __cdecl RGB2Gray(char *,struct mxArray_tag * * const)"
(?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl cv::_OutputArray::_OutputArray(class cv::Mat &)"
(??0_OutputArray@cv@@QEAA@AEAVMat@1@@Z),该符号在函数 "void __cdecl RGB2Gray(char *,struct mxArray_tag * * const)"
(?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "public: void __cdecl cv::Mat::copyTo(class cv::_OutputArray const &)const "
(?copyTo@Mat@cv@@QEBAXAEBV_OutputArray@2@@Z),该符号在函数 "void __cdecl RGB2Gray(char *,struct mxArray_tag * * const)"
(?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "public: void __cdecl cv::Mat::deallocate(void)"
(?deallocate@Mat@cv@@QEAAXXZ),该符号在函数 "public: void __cdecl cv::Mat::release(void)" (?release@Mat@cv@@QEAAXXZ) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "int __cdecl cv::_interlockedExchangeAdd(int *,int)"
(?_interlockedExchangeAdd@cv@@YAHPEAHH@Z),该符号在函数 "public: void __cdecl cv::Mat::release(void)"
(?release@Mat@cv@@QEAAXXZ) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::cvtColor(class cv::_InputArray const &,class
cv::_OutputArray const &,int,int)" (?cvtColor@cv@@YAXAEBV_InputArray@1@AEBV_OutputArray@1@HH@Z),该符号在函数 "void __cdecl
RGB2Gray(char *,struct mxArray_tag * * const)" (?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "class cv::Mat __cdecl cv::imread(class std::basic_string std::char_traits,class std::allocator > const &,int)"
(?imread@cv@@YA?AVMat@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z),该符号在函数 "void __cdecl
RGB2Gray(char *,struct mxArray_tag * * const)" (?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
RGB2Gray.mexw64 : fatal error LNK1120: 8 个无法解析的外部命令

你可能感兴趣的:(后端技术,matlab,visual,studio,混合编吗)