MAtlib 的 cvAdaptiveThreshold 局部二值化

 

 1 MAtlib如何调用opencv函数

    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库

     非常棒的一个类似makefile的make文件

4  当成功生成文件:cvAdaptiveThreshold.mexw64

5  使用函数:

 singeChannelimg=imread(singeChanneimagepath);
 bw=cvAdaptiveThreshold(singeChannelimg,25,5);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值