Image binarization

Introduction to the

Image Binarization refers to the process of setting the gray value of pixel points on the Image to 0 or 255, that is, the whole Image presents an obvious black and white effect. In digital image processing, binary image plays a very important role. The binarization of image greatly reduces the amount of data in the image, so as to highlight the contour of the target.

The principle of

Image binarization processing is to set the gray value of the points on the image to 0 or 255, that is, the whole image presents an obvious black and white effect. In other words, the binarization images of 256 brightness levels can still reflect the whole and local features of the image by selecting appropriate thresholds. In digital image processing, binary image occupies very important position, especially in practical image processing, to binary image processing and system is composed of many, must carry on the binary image processing and analysis, gray image binarization in the first place, get the binary image, it is helpful for further processing in the image, The set nature of the image is only related to the position of the point with the pixel value of 0 or 255. Multi-level value of the pixel is no longer involved, which makes the processing simple and the amount of data processing and compression small. In order to obtain ideal binary images, closed and connected boundaries are generally used to define non-overlapping regions. All pixels with a gray value greater than or equal to the threshold are deemed to belong to a particular object, with a gray value of 255; otherwise, they are excluded from the object region, with a gray value of 0, indicating the background or exceptional object region. If a particular object has uniform and consistent gray values inside, and it is in a uniform background with other levels of gray values, the threshold method can be used to get the comparative segmentation effect. If the difference between the object and the background is not shown on the gray value (such as texture difference), the difference feature can be converted to the gray difference, and then the threshold selection technology is used to segment the image. The specific result of image segmentation can be observed dynamically by adjusting the threshold value to realize the binarization of image.

API

Global thresholding: threshold

public static double threshold(Mat src, Mat dst, double thresh, double maxval, int type) 

Copy the code
  • Parameter 1: SRC, the multi-channel image to be binarized, can only be CV_8U and CV_32F data types
  • Parameter two: DST, the binarized image with the same size, type and number of channels as the input image
  • Parameter 3: Thresh, the threshold for binarization
  • Parameter 4: maxval, the maximum value of the binarization process. This function is used only in THRESH_BINARY and THRESH_BINARY_INV binarization
  • Parameter 5: type, binary type

Binarization type

// C++: enum ThresholdTypes

public static final int

THRESH_BINARY = 0.

THRESH_BINARY_INV = 1.

THRESH_TRUNC = 2.

THRESH_TOZERO = 3.

THRESH_TOZERO_INV = 4.

THRESH_MASK = 7.

THRESH_OTSU = 8.

THRESH_TRIANGLE = 16;

Copy the code
type value role
THRESH_BINARY 0 If the gray value is greater than the threshold, it is the maximum value, and other values are 0
THRESH_BINARY_INV 1 The gray value greater than the threshold is 0, and other values are the maximum
THRESH_TRUNC 2 The gray value greater than the threshold is the threshold, and other values remain unchanged
THRESH_TOZERO 3 If the gray value is greater than the threshold, the other values are 0
THRESH_TOZERO_INV 4 The gray value greater than the threshold is zero, and other values remain unchanged
THRESH_MASK 7 NA
THRESH_OTSU 8 The Otsu method automatically finds global thresholds
THRESH_TRIANGLE 16 The triangle method automatically finds global thresholds
Binarization type
generation

THRESH_OTSU and THRESH_TRIANGLE

These two marks is to obtain the threshold method, it is not a sign of threshold comparison method, the two marks in front of the can and 5 kinds of marks are used together, such as “THRESH_BINARY | THRESH_OTSU”. Five marks in the calling function needs to be in front of the man-made setting threshold, if the images do not understand the threshold setting is unreasonable, can cause serious impact to the effect after the treatment, these two signs, respectively (OTSU) and the triangular method by the use of dajin (TRIANGLE) combined with image grey value distribution characteristics for binarization threshold, The threshold value is given in the form of the return value of the function. So if the last parameter of a function sets either of these flags, the third parameter thresh is given automatically by the system, but cannot be defaulted when the function is called, except that the program does not use this value. Currently, these two flag bits are only available for CV_8UC1 images.

OTSU method: best for double wave peaks

OTSU method is also called maximum variance method and maximum threshold method (OTSU). Its basic idea is to use a threshold value to divide the data in the image into two types, one kind of image pixel gray is less than the threshold, the other kind of image pixel gray is greater than or equal to the threshold. If these two classes of pixel gray variance, the greater the instructions for the threshold value is the best threshold (variance is grayscale distribution uniformity of a measure, the greater the variance between between background and foreground, illustrate the difference between the two parts of the image, the greater the part when prospects is divided into background or part of the background wrong into outlook will lead to two parts difference is smaller. Therefore, segmentation that maximizes the variance between classes means the lowest probability of misclassification.) . The threshold can be used to divide the image into foreground and background. And what we’re interested in is usually the foreground.

TRIANGLE method: most suitable for a single wave peak, originally used for medical segmentation of cells, etc

The triangulation method was first seen in Zack’s paper “Automatic Measurement of Sister Chromatid Exchange Frequency”, mainly used in the study of chromosomes. This method uses histogram data to find the optimal threshold based on pure geometric method. Its establishment condition is that it is assumed that the maximum wave peak of the histogram is near the brightest side, and then the maximum straight-line distance is obtained through the triangle. The gray level of the histogram corresponding to the maximum straight-line distance is the segmentation threshold, as shown below:


Construct a straight line on the histogram from the peak BMX to the darkest corresponding histogram bmin(p=0)%, calculate the vertical distance from each corresponding histogram B to the line from bmin until bmax is known, where the histogram position corresponding to the maximum distance is the threshold T corresponding to image binarization.

Sometimes the corresponding position of the maximum wave peak is not on the brightest side of the histogram, but on the dark side. In this case, it is necessary to reverse the histogram and obtain the value after the inversion. The threshold value T is obtained by subtracts 255. The histogram of the expansion is shown below:


Algorithm steps

  1. Image grayscale
  2. Calculate the gray histogram of the image
  3. Find the sides of the histogram
  4. Find the histogram maximum
  5. Check if the maximum wave peak is on the bright side, otherwise flip
  6. Calculate the threshold value to the threshold T, or 255-t if flipped

Local thresholding: adaptiveThreshold

The local adaptive threshold is to determine the binarization threshold of the pixel position according to the pixel value distribution of the neighborhood block of the pixel. The advantage of this approach is that the binarization threshold at each pixel position is not fixed, but determined by the distribution of pixels in its surrounding neighborhood. The binarization threshold of the image region with higher brightness is usually higher, while the binarization threshold of the image region with lower brightness is correspondingly smaller. Local image regions with different brightness, contrast and texture will have corresponding local binarization thresholds.

public static void adaptiveThreshold(Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C) 

Copy the code
  • Parameter 1: SRC, the image to be binarized, the image can only be CV_8UC1 data type

  • Parameter two: DST, the binarized image has the same size and type as the input image

  • Parameter 3: maxValue, the maximum value of binarization

  • Parameter 4: adaptiveMethod, which is an adaptive threshold algorithm, including the mean method ADAPTIVE_THRESH_MEAN_C and the Gaussian method ADAPTIVE_THRESH_GAUSSIAN_C.

  • ThresholdType: select the flag of the image binarization method, which can only be THRESH_BINARY and THRESH_BINARY_INV

  • Parameter 6: blockSize: adaptively determine the pixel neighborhood size of the threshold, which is generally an odd number of 3, 5, and 7

  • Parameter 7: C, the constant subtracted from the average or weighted average, can be positive or negative

Adaptive threshold algorithm

// C++: enum AdaptiveThresholdTypes

public static final int

ADAPTIVE_THRESH_MEAN_C = 0.

ADAPTIVE_THRESH_GAUSSIAN_C = 1;

Copy the code
type value role
ADAPTIVE_THRESH_MEAN_C 0 Let’s take the mean of the block and subtract C
ADAPTIVE_THRESH_GAUSSIAN_C 1 Take the gaussian weighted sum of the blocks and subtract C
Adaptive threshold

operation

private fun threshold(type: Int) {

val ret = Mat()

Imgproc.threshold(mGray, ret, 127.toDouble(), 255.toDouble(), type)

showMat(ret)

title = getTypeName(type)

}

Copy the code
private fun adaptiveMean(a) {

val ret = Mat()

Imgproc.adaptiveThreshold(

mGray,

ret,

255.toDouble(),

Imgproc.ADAPTIVE_THRESH_MEAN_C,

Imgproc.THRESH_BINARY,

55.

0.0

)

showMat(ret)

title = "ADAPTIVE_THRESH_MEAN_C"

}

Copy the code

The results of

The results

The source code

Github.com/onlyloveyd/…


This article is formatted using MDNICE