C#, image binarization (14) - the best iterative algorithm for global threshold (Iterate Thresholding) and source code

1. Image binarization

Image binarization is the conversion of a color image into a black and white image. Most computer vision applications convert pictures into binary representations. The more raw an image is, the easier it is for a computer to interpret its underlying features.

Binarization process

In computer memory, all files are usually stored in grayscale with a maximum of 256 different grayscale values ​​from 0 to 255. Each grayscale value produces a different color of the grayscale palette. If you need some information from the document image, you need to do it multiple times. To reduce the time required to extract parts of an image, binary images are more useful.

Binarization is the method of converting any grayscale image (multitone image) into a black and white image (duotone image). To perform binarization, first find the threshold of the grayscale and check if the pixel has a specific grayscale value.

Convert pixels to white if their grayscale value is greater than the threshold. Similarly, pixels are converted to black if their gray value is less than a threshold.

Two types of binarization methods are discussed below −

(1) Binarization based on global or individual thresholds

(2) Region-based binarization

1. Global or single threshold based binarization: Typically, a global threshold is found for the entire image and a single threshold is used to binarize the image. But in this single-threshold method, the local variance of the image is usually lost or suppressed, which may have some important information or content.

2. Region-based binarization: Another method for binarization is also devised, where the threshold is determined according to the region. In practice, the image is divided into several areas or windows. Each region or window calculates or decides its own local threshold, and then converts its region into a duotone region with the help of its local threshold, according to Saha.

In real scenarios, the binarization process fails because of degradations that may be caused by inefficient image acquisition processes, poor quality of the original source, or uneven illumination on the original source.

Why do We Need Binarization?
 
Auto encoders are not able to recognize the images because of the noise in the images, otherwise referred to as “image processing.” For avoiding the background noise generated in images we will use a Binarization technique commonly empoloyed with artificial intelligence.
 

A Breakdown of Binarization
 
A color image consists of 3 channels (Red, Green and Blue) with values ranging from 0 to 255. One of the key features of binarization is converting grey scale images into black and white (0 and 1). What’s more, binarization provides sharper and clearer contours of various objects present in the image. This feature extraction improves the learning of AI models.

In the process of image binarization a threshold value is chosen, and all pixels with values above this threshold are classified as white, and all other pixels as black. The problem then is how to select the correct threshold (otherwise referred to as a thresholding method). 

One can see that binarization takes an image with foreground/background and returns the binary image. It discards the background noise and gives the contour of the image in the foreground. 

Steps involved in Image Binarization
 
The ‘imager ‘ package uses the K-means method to automatically identify the threshold for an image and this method is equivalent to globally optimal version of popular Otsu’s method. It is very important to know that an incorrect threshold value can result in distorted binary images, where parts of the object could be missing.

Manual Threshold Identification
 
Given an image, how do we calculate the threshold value?
The image below illustrates a histogram-based method which uses pixel values.

2. Grayscale image binarization, global algorithm, iterative optimal threshold algorithm and source code

  For an overview of binary algorithms, please read:

C#, image binarization (01) - a review of binarization algorithms and a catalog of twenty-three algorithms https://blog.csdn.net/beijinghorn/article/details/128425225?spm=1001.2014.3001.5502

For supporting functions, please read:

C#, image binarization (02) - C# source code of some basic image processing functions for image binarization https://blog.csdn.net/beijinghorn/article/details/128425984?spm=1001.2014 . 3001.5502

using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Imaging;
 
namespace Legalsoft.Truffer.ImageTools
{
    public static partial class BinarizationHelper
    {
        #region 灰度图像二值化 全局算法 迭代最佳阈值

        /// <summary>
        /// 迭代最佳阈值
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static int Best_Iteratived_Threshold(int[] histogram)
        {
            int MinValue = Histogram_Left(histogram);
            int MaxValue = Histogram_Right(histogram);

            int Threshold = MinValue;
            int NewThreshold = (MaxValue + MinValue) / 2;
            int Iter = 0;
            double MeanValueOne, MeanValueTwo, SumOne, SumTwo, SumIntegralOne, SumIntegralTwo;
            while (Threshold != NewThreshold)
            {
                SumOne = 0;
                SumIntegralOne = 0;
                SumTwo = 0;
                SumIntegralTwo = 0;
                Threshold = NewThreshold;
                for (int i = MinValue; i <= Threshold; i++)
                {
                    SumIntegralOne += histogram[i] * (double)i;
                    SumOne += histogram[i];
                }
                MeanValueOne = SumIntegralOne / SumOne;
                for (int i = Threshold + 1; i <= MaxValue; i++)
                {
                    SumIntegralTwo += histogram[i] * (double)i;
                    SumTwo += histogram[i];
                }
                MeanValueTwo = SumIntegralTwo / SumTwo;
                NewThreshold = (int)((MeanValueOne + MeanValueTwo) / 2.0);

                Iter++;
                if (Iter >= 1000)
                {
                    return -1;
                }
            }
            return Threshold;
        }

        public static void Best_Iteratived_Algorithm(byte[,] data)
        {
            int[] histogram = Gray_Histogram(data);
            int threshold = Best_Iteratived_Threshold(histogram);
            Threshold_Algorithm(data, threshold);
        }

        #endregion
    }
}
 

3. Grayscale image binarization, global algorithm, iterative optimal threshold algorithm calculation effect

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/128522073