1 MAtlib如何调用opencv函数
注意安装Matlab的视觉包,很简单
windows 只需要在matlab 运行visionSupportPackages指令 然后next 注意其中有一步选择 interface opencv
2 直接上代码 cvAdaptiveThreshold
// Interface: cvAdaptiveThreshold
// Author : zhang
// Date : 2016-08-15
// CVAPI(void) cvAdaptiveThreshold( const CvArr* src, CvArr* dst, double max_value,
// int adaptive_method CV_DEFAULT(CV_ADAPTIVE_THRESH_MEAN_C),
// int threshold_type CV_DEFAULT(CV_THRESH_BINARY),
// int block_size CV_DEFAULT(3),
// double param1 CV_DEFAULT(5));
//bw=cvAdaptiveThreshold(im,25,5);
#include "opencvmex.hpp"
using namespace cv;
void checkInputs(int nrhs, const mxArray *prhs[])
{
if ((nrhs != 3))
{
mexErrMsgTxt("Incorrect number of inputs. Function expects 3 inputs.\n Usage: [CvArr* dst] = cvAdaptiveThreshold(const CvArr* src,block_size,param1);\n");
return ;
}
}
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
checkInputs(nrhs, prhs);
cv::Ptr<cv::Mat> gray_mat = ocvMxArrayToImage_uint8(prhs[0], true);
int blockSize = *(mxGetPr(prhs[1]));
int constValue = *(mxGetPr(prhs[2]));
IplImage pImg= IplImage(*gray_mat);
IplImage *outputImg = cvCreateImage(cvGetSize(&pImg), IPL_DEPTH_8U, 1);
cvAdaptiveThreshold(&pImg, outputImg, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, blockSize, constValue);
cv::Mat out_mat(outputImg);
int rows = out_mat.rows;
int cols = out_mat.cols;
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
double *imgMat;
imgMat = mxGetPr(plhs[0]);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
*(imgMat + i + j * rows) = (double)out_mat.at<uchar>(i, j);
cvReleaseImage(&outputImg);
return;
}
3 使用:因为使用了opencv所以需要配置相应的opencv库
4 当成功生成文件:cvAdaptiveThreshold.mexw64
5 使用函数:
singeChannelimg=imread(singeChanneimagepath);
bw=cvAdaptiveThreshold(singeChannelimg,25,5);