Adaptive threshold segmentation

1. principle
The simplest threshold segmentation is to set the threshold manually to binarize the image. The pixel value greater than the set threshold is set to 255, and the pixel value less than the set threshold is set to 0. This method is generally called global threshold segmentation. Next, I want to introduce a local threshold segmentation algorithm. Its principle is very simple. Generally speaking, each part of the image will be processed to get one The threshold value is used to segment the region. Similarly, each region has a different threshold value to deal with. How to get the threshold value is the key of the whole algorithm.
The API interface of OpenCV is used for explanation. There are interface functions in opencv library
void adaptiveThreshold(InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C)
src is the input image,
dst is the output image,
maxValue is the set pixel maximum value,
The adaptive method is the filtering method used, and opencv gives two methods, which are adaptive? Thresh? Mean? C and adaptive? Thresh? Gaussian? C,
thresholdType is the threshold type, threshold and threshold,
blockSize refers to the size of the neighborhood, that is, the size of the kernel, which is usually odd,
C is the offset of the threshold.
The threshold value is the mean value of the neighborhood minus C when using the adaptive method, and the threshold value is the Gaussian mean value of the neighborhood minus C when using the adaptive method.

2. Code display
Self adaptive thresholding segmentation method with median filtering

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

enum FliterMethod {
 meanFliter,
 gaaussianFliter,
 medianFliter
};
void AdaptiveThreshold(Mat &src, Mat &dst,double Maxval, int Subsize,double C ,FliterMethod method )
{
 if (src.channels() > 1)
  cvtColor(src,src,CV_RGB2GRAY);
 Mat smooth;
 switch (method)
 {
 case meanFliter:
  blur(src, smooth, Size(Subsize, Subsize));
  break;
 case gaaussianFliter:
  GaussianBlur(src, smooth, Size(Subsize, Subsize), 0, 0);
  break;
 case medianFliter:
  medianBlur(src, smooth, Subsize);
  break;
 default:
  break;
 }
 smooth = smooth - C;
 src.copyTo(dst);
 int r = dst.rows;
 int c = dst.cols;
 for (int i = 0; i < r; i++)
 {
  for (int j = 0; j < c; j++)
  {
   if (dst.at<uchar>(i,j) > smooth.at<uchar>(i,j))
    dst.at<uchar>(i,j) = Maxval;
   else
    dst.at<uchar>(i,j) = 0;
  }
 }
}

In order to see the effect of real-time processing, the computer camera is called. The code is as follows

int Capture() {
 VideoCapture cap;
 cap.open(0);
 Mat frame;
 //Mat src;
 if (!cap.isOpened())
  return -1;
 while (1)
 {
  cap.read(frame);
  if (frame.empty())
   break;
  //GaussianBlur(frame, src, Size(5, 5), 3, 3);
  Mat src,tmp;
  AdaptiveThreshold(frame, src, 255, 7, 5, meanFliter);
  adaptiveThreshold(frame, tmp, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 7, 10);
  //imshow(" ", frame);
  imshow("AdaptiveThreshold",src);
  imshow("opencv", tmp);
  waitKey(10);
 }
 cap.release();
 destroyAllWindows();
}

Here is the test code

int main()
{
 Mat img = imread("C:/Users/94077/Desktop/QQ Screenshot 20200307162029.bmp");
 Mat src,tmp;
 AdaptiveThreshold(img, src, 255, 7, 10, gaaussianFliter);
 adaptiveThreshold(img, tmp, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY,7, 10);
 imshow("function", src);
 imshow("opencv", tmp);
 waitKey(0);
 //Capture();
 return 0}

3. Result display
By comparing several processed pictures here, it is found that the processing effect of opencv library function is better, but for different pictures, the effect of median filter may be better than the other two at some times.

Published 2 original articles, won praise 1 and visited 30
Private letter follow

Tags: OpenCV less

Posted on Sat, 07 Mar 2020 04:42:47 -0500 by edwin_h