当前位置:主页 > 软件编程 > C代码 >

利用OpenCV实现局部动态阈值分割

时间:2021-03-10 09:27:22 | 栏目:C代码 | 点击:

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

#include "dialog.h"
#include <QApplication>
#include "cv.h"
#include "highgui.h"
#include <QDebug>


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()<<img->nChannels;
  double a = 0;
  double b = 0;
  for(int i=0;i<img->height;i++){
    for(int j=0;j<img->width;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;
}

您可能感兴趣的文章:

相关文章