OpenCV实现局部动态阈值分割

参考Halcon dyn_threshold算子的思路,利用OpenCV实现局部动态阈值分割。

#include "dialog.h"
#include 
#include "cv.h"
#include "highgui.h"
#include 


int main(int argc, char *argv[])
{
    IplImage *img = cvLoadImage("D:/cs/1227/f9.bmp",0);
    IplImage *out = cvCreateImage(cvGetSize(img),img->depth,1);
    cvSmooth(img,out,CV_BLUR,50);
    IplImage *res = cvCreateImage(cvGetSize(img),img->depth,1);
    qDebug()<nChannels;
    double a = 0;
    double b = 0;
    for(int i=0;iheight;i++){
        for(int j=0;jwidth;j++){
            CvScalar color = cvGet2D(img,i,j);
            CvScalar color1 = cvGet2D(out,i,j);
            a = color.val[0];
            b = color1.val[0];
            //a = exp(a);
            CvScalar c = cvScalar(b-a+128);
            cvSet2D(res,i,j,c);
        }
    }
    cvThreshold(res,res,150,255,CV_THRESH_BINARY_INV);
    cvNamedWindow("Image",0);
    cvShowImage("Image",res);
    cvSaveImage("D:/cs/cs.bmp",out);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Image");
    return 0;
}

你可能感兴趣的:(opencv)