Emgu image threshold and emgu image threshold

Source: Internet
Author: User

Emgu image threshold and emgu image threshold

Address: http://www.cnblogs.com/CoverCat/p/5043833.html

Reprinted for future reference

Visual Studio Community 2015 project and code: http://pan.baidu.com/s/1o7lxYSM

Content

The following content will be mentioned in this article:

  • Global threshold
  • Adaptive Threshold
  • Otsu's binarization

In image processing, we want to ignore some grayscale details and only keep the contour of the subject. This is achieved by performing threshold-based processing on grayscale images.

"The basic idea is to give an array and a threshold value, and then perform some processing based on whether the value of each element in the array is lower than or higher than the threshold value." -- learning OpenCV (Chinese Version) ", the" array "here is the image data,

"Processing" refers to classification. There are only two types, which are divided into different classes according to different values.

Preparations

  • Create a project -- refer to Emgu (1) -- Emgu introduces how to create a WinForm project named "Threshold"
  • REFERENCE The namespace in Form1.cs:
 using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.CvEnum;
  • Interface: Add a TableLayoutPanel container with one row and two columns in Form1, and then add two Emgu containers. CV. UI. imageBox control (refer to Emgu Learning (2)-image reading, display, and saving). After adding the image, the interface is as follows:

  

  • Set the SizeMode attribute of imageBox1 and imageBox2 to StretchImage

Global threshold

Global Threshold refers to the use of a Threshold value for filtering and classification of the entire image data. OpenCV provides the cvThreshold () method for Threshold-based operations, and the corresponding method name in Emgu is Threshold.

The Threshold method accepts a parameter of the ThresholdType type, which has the following enumerated values:

Binary = 0,
BinaryInv = 1, Trunc = 2, ToZero = 3, ToZeroInv = 4, Mask = 7, // I will not introduce Otsu = 8 here. // I will introduce it later.
  • Binary (Binary threshold)-- Binary threshold refers to setting the pixels greater than the threshold value to the maximum value, while those smaller than the threshold value to 0, that is:

Value = value> threshold? Max_value: 0

  

  • BinaryInv (reverse binary threshold)-- Opposite to the binary threshold, the reverse binary threshold is set to 0 when the pixel value is greater than the threshold value, and the reverse is set to the maximum value, that is:

Value = value> threshold? 0: max_value

  

  • Trunc (truncation threshold)-- Truncation threshold indicates that when the pixel value is greater than the threshold value, the pixel value is set to the threshold value. Otherwise, the pixel value itself is retained, that is:

Value = value> threshold? Threshold: value

  

  • ToZero)-- Hyper-threshold zeroing means that when the pixel value is greater than the threshold value, the original value of the pixel is retained. Otherwise, the value is set to 0, that is:

Value = value> threshold? Value: 0

  

  • ToZeroINV (return to zero below the threshold)-- Opposite to superthreshold zeroing, if the value of the pixel is greater than the threshold value, the pixel value is set to 0. Otherwise, the original value of the pixel is retained, that is:

Value = value> threshold? 0: value

  

From the above description, we can see that binary/anti-binary threshold requires a max_value (maximum value) parameter. At the same time, there are only two types of image data after binary/anti-binary threshold processing

Possible values: 0 and max_value, which are called binarization images.

"In digital image processing, binary images play a very important role. First, binarization is conducive to further image processing, making the image simple and reducing the amount of data, rounds that highlight the target of interest

Profile. Secondly, we need to process and analyze binary images. First, we need to binarization gray images to obtain binary images. All pixels whose gray scale is greater than or equal to the threshold value are determined to belong to a specific object. The gray scale value is 255,

Otherwise, these pixels are excluded from the object area. The gray value is 0, indicating the background or exceptional object area ." -- Baidu encyclopedia

The following code shows how to use binary threshold processing. If you want to use binary threshold processing instead of binary, max_value does not need to be set.

 

{Using (var image = new Image <Bgr, Byte> (Properties. resources. chess3) {var grayImage = image. convert <Gray, Byte> (); // Convert to grayscale var threshImage = grayImage. copyBlank (); CvInvoke. threshold (grayImage, threshImage, 150, // Threshold value 255, // maximum value ThresholdType. binary); // sets the Binary threshold value to imageBox1.Image = grayImage; imageBox2.Image = threshImage ;}}

 

The effect is as follows: Binary-> BinaryInv-> Trunc-> ToZero-> ToZeroInv

Adaptive Threshold

The global threshold value is the threshold value used for the entire image, which is not applicable to all situations. The adaptive threshold value uses different thresholds for different regions of the image, and the threshold value is calculated for this region. OpenCV provides cvAdaptiveThreshold ()

This function provides two methods for calculating the threshold value, CV_ADAPTIVE_THRESH_MEAN_C and CV_ADAPTIVE_THRESH_GAUSSIAN_C.

"In both cases, the adaptive threshold T (x, y) varies with each pixel. Calculate the weighted average of B x B around the pixel and subtract a constant to obtain the adaptive threshold. The block_size parameter of B is specified, and the constant has param1.

. If the CV_ADAPTIVE_THRESH_MEAN_C method is used, the average weighting of all pixels in the region is calculated. If CV_ADAPTIVE_THRESH_GAUSSIAN_C is used, the pixels around (x, y) in the region

The Gaussian Functions are weighted based on their distance from the center ." -- Learning OpenCV (Chinese Version)

In Emgu, The CVInvoke class provides the AdaptiveThreshold static method for adaptive threshold processing. The prototype of this method is:

Public static void AdaptiveThreshold (IInputArray src, // original image IOutputArray dst, // result image double maxValue, // The maximum value CvEnum used for Binary/anti-binary threshold processing. adaptiveThresholdType adaptiveType, // Adaptive Threshold calculation method: MeanC or GaussianCCvEnum. thresholdType thresholdType, // threshold method, which must be one of Binary and anti-Binary thresholds (Binary/BinaryInv) int blockSize. // The calculated region matrix size is 3, 5, 7, 9... double param1) // constant

The Image class also provides a ThresholdAdaptive method that encapsulates the AdaptiveThreshold method. The following code uses the ThresholdAdaptive method of several Image classes:

        private void Form1_Load(object sender, EventArgs e)        {            var grayImage = new Image<Gray, Byte>(Properties.Resources.chess3);            //CvInvoke.AdaptiveThreshold(grayImage, threshImage, 255, AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, 12, 5);            var threshImage = grayImage.ThresholdAdaptive(                new Gray(255),                 AdaptiveThresholdType.MeanC,                ThresholdType.Binary,                 9,                 new Gray(5));            imageBox1.Image = grayImage;            imageBox2.Image = threshImage;        }

Execution result: MeanC-> GaussianC

Otsu binarization

In the global threshold code, we use 150 as the binary threshold value, but this threshold value is randomly selected. I am not sure whether this threshold value is a suitable threshold value. If the Otsu binarization method is used

The histogram of the image calculates a threshold value. The Otsu method uses the Threshold method mentioned in the global Threshold. The Code is as follows:

            var grayImage = new Image<Gray, Byte>(Properties.Resources.chess3);            var threshImage = grayImage.CopyBlank();            CvInvoke.Threshold(                grayImage,                 threshImage,                0,                255,                ThresholdType.Otsu);            imageBox1.Image = grayImage;            imageBox2.Image = threshImage;

Running effect:

  

 

Related Article

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.