C#,图像二值化(14)——全局阈值的最佳迭代算法(Iterate Thresholding)及源代码

C#,图像二值化(14)——全局阈值的最佳迭代算法(Iterate Thresholding)及源代码_第1张图片

1、图像二值化

图像二值化是将彩色图像转换为黑白图像。大多数计算机视觉应用程序将图片转换为二进制表示。图像越是未经处理,计算机就越容易解释其基本特征。

二值化过程

在计算机存储器中,所有文件通常以灰度级的形式存储,灰度级具有从0到255的最大256个不同灰度值。每个灰度值生成灰度调色板的不同颜色。如果需要文档图像中的某些信息,则需要进行多次操作。为了减少提取图像部分所需的时间,二进制图像更有用。

二值化是将任何灰度图像(多色调图像)转换为黑白图像(双色调图像)的方法。要执行二值化处理,首先找到灰度的阈值,并检查像素是否具有特定的灰度值。

如果像素的灰度值大于阈值,则将这些像素转换为白色。类似地,如果像素的灰度值小于阈值,则这些像素被转换为黑色。

下面讨论了两种类型的二值化方法——

(1) 基于全局或单个阈值的二值化

(2)基于区域的二值化

1.基于全局或单个阈值的二值化:通常,找到整个图像的全局阈值,并使用单个阈值对图像进行二值化。但是在这种单阈值方法中,图像的局部方差通常丢失或被抑制,这可能具有一些重要的信息或内容。

2.基于区域的二值化:还设计了另一种用于二值化的方法,其中阈值根据区域来确定。实际上,图像被划分为几个区域或窗口。每个区域或窗口计算或决定自己的局部阈值,然后根据Saha,借助其局部阈值将其区域转换为双色调区域。

在实际场景中,二值化过程失败,因为可能由于图像采集过程效率低、原始源质量差或原始源上的照明不均匀而导致退化。

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、灰度图像二值化,全局算法,迭代最佳阈值算法及源代码

C#,图像二值化(14)——全局阈值的最佳迭代算法(Iterate Thresholding)及源代码_第2张图片

  二值算法综述请阅读:

C#,图像二值化(01)——二值化算法综述与二十三种算法目录https://blog.csdn.net/beijinghorn/article/details/128425225?spm=1001.2014.3001.5502

支持函数请阅读:

C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码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 灰度图像二值化 全局算法 迭代最佳阈值

        /// 
        /// 迭代最佳阈值
        /// 
        /// 
        /// 
        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、灰度图像二值化,全局算法,迭代最佳阈值算法计算效果

你可能感兴趣的:(C#算法演义,Algorithm,Recipes,C#实用代码,Coding,Recipes,算法,计算机视觉,图像处理)