[Reproduced + original] emgu CV on C # (4) -- emgu CV on binarization

Source: Internet
Author: User
Tags scale image

This article focuses on the binarization principle and mathematical implementation, and uses the emgucv Method for programming.

I. theoretical overview (reprinted. If you understand image processing, you can skip it and use it for science purposes only, or write articles to gather words)

1. Overview

Image binarization is a basic technology in image processing and A preprocessing process of many image processing technologies.

Image pre-processing requires image pre-processing before image binarization, including color image gray and enhancement. Because the selection threshold must refer to the histogram, after processing the image, we can obtain the histogram of the image to help select the threshold. The process is as follows:

Image reading → grayscale image → Image Enhancement → Image Histogram → binarization processing

2. Mathematical Principles (for details, see Study on binarization methods of grayscale images.)

Image binarization is a common method for studying gray-scale images using gray-scale transformations, that is, a threshold value is set to divide the pixels of a grayscale image into two parts: a pixel group greater than the threshold value and a pixel group smaller than the threshold value. For example, if the gray-scale image function is used to output a binary image

Threshold is a scale that separates the target from the background. to select an appropriate threshold, you must save the image information as much as possible and minimize the interference between the background and noise, this is the principle of selecting a threshold.

The binarization processing of the image is to set the gray scale of the points on the image to 0 or 255, that is, to make the entire image show a clear black and white effect. About 256 gray-scale images with brightness levels will be obtained by selecting appropriate thresholds and a binarization image that still reflects the overall and local features of the image.

There are many methods for binarization grayscale images, including global threshold and local threshold.

Global Threshold Method

The global threshold method only uses one global threshold during binarization. It compares the gray-scale values of each pixel of the image. If the gray-scale value is greater than the gray-scale value, the image is taken as the foreground color (white). Otherwise, the image is taken as the background color. A threshold value is determined based on the histogram of the text image or the gray space distribution to convert the gray text image to the binary image. The global threshold method can be divided into the point-based threshold method and the region-based threshold method. The results of the Threshold Segmentation Method depend heavily on the selection of the threshold. Therefore, the key to this method is how to select the appropriate threshold.

Typical global threshold methods include the Otsu method and the maximum entropy method. The global threshold algorithm is simple and has good effects on images with obvious separation of the target and background, and the histogram distribution in dual peaks, however, for images with uneven illumination and high noise interference, the histogram distribution is not shown in dual peaks, and the binarization effect is obviously deteriorated. If the gray-scale difference between the target and the background is not obvious, a large black area may appear, or even the information of the entire image may be lost. Only one-dimensional gray-scale histogram distribution is used, without combining the spatial information of the image, the processing effect is not good. When the image is broken or the background is noisy, the expected effect cannot be obtained.

In order to meet the automatic and real-time requirements of image processing application systems, it is best for computers to automatically select an image binarization threshold. The following lists the automatic selection algorithms for several thresholds:

(1) average gray value method: the average value of the gray value of all pixels in the image is the threshold value.

(2) the Dajin method, also known as the maximum inter-class variance method, is an automatic threshold selection based on the statistical characteristics of the entire image. The principle is to divide the image histogram into two types by a certain gray value, calculate the pixel points and the average gray value of the two types respectively, and then calculate the inter-Class Variance between them. When the variance between the two classes is the largest, the gray scale value is used as the threshold for image binarization. The Dajin method is widely used. no matter whether the histogram of the image has obvious double peaks, it can produce satisfactory results and has been applied and developed in many fields. However, this method still has some shortcomings, mainly manifested in: If the gray difference between the target and the background is not obvious, a large black area may appear, or even the information of the entire image may be lost; only one-dimensional gray-scale histogram distribution is used, without combining the spatial information of the image, the processing effect is not good; when the image is broken or the background has a certain degree of noise, the expected effect cannot be achieved.

(3) edge operator method: Use Laplace, Robert, and sober operators to perform gray-level enhancement or reduction transformation on pixels. For pixels in the area where the gray scale is evenly distributed, these operators weaken the gray scale. For pixels near the edge, these operators increase the gray scale.

Local Threshold Method

The pixel threshold is determined by the current pixel gray value and the local gray level feature of the pixel's surrounding points. For example, the original image can be divided into some non-Intersecting small blocks, and the gray-scale mean of each block image is used as the threshold value of this part of the image, and the above overall threshold method is used locally.

For images with clear targets and backgrounds, the global threshold method can achieve better results. However, if the background of the image is uneven or the gray-scale change rate of the target is relatively large, the global method is no longer applicable. The local threshold method determines the threshold value of a pixel based on the gray value of the pixel and the local gray value of the pixel's neighbor. When the illumination is uneven, there is sudden noise, or the background gray scale changes greatly, the local threshold determination technology must automatically determine different thresholds based on the coordinate location relationship of pixels, implement Dynamic adaptive binarization processing. The local threshold is usually used to divide the image into several sub-images, and the overall threshold is used in each sub-image area, thus, the local threshold method of the entire image can be formed (the corresponding threshold is determined based on each sub-image, and the specific threshold determination method is similar to the global threshold ). After this method is used, the split image has a gray-scale discontinuous distribution at the boundary of different sub-images. Therefore, the smoothing technique must be used to eliminate the gray-scale non-continuity.

The local threshold method is generally used to identify images with severe interference and poor quality. It is more widely used than the overall threshold method, but it also has shortcomings and problems, for example, the Implementation speed is slow, the connectivity between strokes of characters is not guaranteed, and artifacts are prone to occur (that is, the stroke results are obtained by noise interference in the back scene. Typical local binarization algorithms include the Bernsen method, multi-threshold gradient intensity method, texture-based image method, and maximum variance method.

3. Advantages

Binary Images play a very important role in digital image processing. Especially in practical image processing, there are many systems that are implemented by binary image processing, to process and analyze a binary image, first binarization the gray image to obtain a binary image, which facilitates further processing of the image, the set nature of the image is only related to the position of the point where the pixel value is 0 or 255. It does not involve the multi-level value of the pixel, making the processing simple, and the amount of data processing and compression is small. Binary Images are widely used in image analysis. Binary Images refer to images with only two gray levels. Binary Images have low storage space and fast processing speed, this feature allows you to conveniently perform Boolean logical operations on images. More importantly, based on a binary image, we can further process the image to obtain some geometric or other features of the image.

Ii. Program Implementation

For the opencv programming method, see "binarization and grayscale of images" and "[opencv Getting Started Guide] Chapter 4 binarization of images".

For the emgucv programming method, see "C # opencv study NOTE 4: image binarization".

1. Key functions

According to the method provided in "C # opencv learning NOTE 4: image binarization", emgucv can be binarization using two functions.

Cvthreshold and thresholdbinary.

Cvthreshold Method 

Function: Performs Edge Detection on the Image Using the canny method.

Function prototype:

C #
public static double cvThreshold(IntPtr src,IntPtr dst,double threshold,double maxValue,THRESH thresholdType)

Function Description:

The first parameter indicates the input image, which must be a single channel grayscale image.

The second parameter indicates the output edge image, which is a single-channel black-and-white image.

The third parameter indicates the threshold.

The fourth parameter indicates the maximum value.

The fifth parameter represents the calculation method.

The calculation method can be found in emgu. cv. cvenum. thresh.

  Value Description
  Cv_thresh_binary 0 Value = value> threshold? Max_value: 0
  Cv_thresh_binary_inv 1 Value = value> threshold? 0: max_value
  Cv_thresh_trunc 2 Value = value> threshold? Threshold: Value
  Cv_thresh_tozero 3 Value = value> threshold? Value: 0
  Cv_thresh_tozero_inv 4 Value = value> threshold? 0: Value
  Cv_thresh_mask 7  
  Cv_thresh_otsu 8 Use Otsu algorithm to choose the optimal threshold value; Combine the flag with one of the above cv_thresh _ * Values

Add the fourth picturebox to the form and set sizemodel to zoom

Add the following program function directly after the equalization function. The threshold is 30, so easy.

// Binarization image <gray, byte> threshimg = new image <gray, byte> (histmi. width, histmi. height); cvinvoke. cvthreshold (histimage, threshimg, 30,255, emgu. CV. cvenum. thresh. cv_thresh_binary); picturebox4.image = threshimg. tobitmap ();

Thresholdbinary Function

Explanation of the methods provided by emgucv: threshold the image such that: DST (x, y) = max_value, if SRC (x, y)> threshold; 0, otherwise. In fact, it is the same as the first operation method in the cvthreshold function, that is, cv_thresh_binary.

Image<Gray, Byte> threshimg= grayimg.ThresholdBinary(new Gray(128), new Gray(255));

Iii. Result Analysis

Compare it with the algorithm in article [opencv Getting Started Guide] Article 4 binarization of images and the processing results of different method slices.

In the original article, the threshold value is 111 to achieve binarization.

Using emgucv programming, set the threshold to 111 to obtain the binarization.

Because the original image cannot be obtained, it is only obtained through the image. I wonder whether there is any image loss in the process.

Some details are different through visual comparison.

Adjust the threshold value based on application requirements to obtain the optimal effect.

Reprinted please indicate the source, original address: http://www.cnblogs.com/MobileBo/p/3918119.html

Iv. Regret

Threshold adaptive adjustment currently has time to learn. Take a look at the time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.