C#,图像二值化(03)——全局阈值的基本算法及其源程序

C#,图像二值化(03)——全局阈值的基本算法及其源程序_第1张图片

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). 
 

以下文字来自于百度百科,凑数而已,可以掠过。

1、图像二值化简介
 

将256个亮度等级的灰度图像通过适当的阈值选取而获得仍然可以反映图像整体和局部特征的二值化图像。在数字图像处理中,二值图像占有非常重要的地位,首先,图像的二值化有利于图像的进一步处理,使图像变得简单,而且数据量减小,能凸显出感兴趣的目标的轮廓。其次,要进行二值图像的处理与分析,首先要把灰度图像二值化,得到二值化图像。
所有灰度大于或等于阈值的像素被判定为属于特定物体,其灰度值为255表示,否则这些像素点被排除在物体区域以外,灰度值为0,表示背景或者例外的物体区域。

2、图像二值化原理

图像的二值化处理就是将图像上的点的灰度值为0或255,也就是将整个图像呈现出明显的黑白效果。即将256个亮度等级的灰度图像通过适当的阈值选取而获得仍然可以反映图像整体和局部特征的二值化图像。在数字图像处理中,二值图像占有非常重要的地位,特别是在实用的图像处理中,以二值图像处理实现而构成的系统是很多的,要进行二值图像的处理与分析,首先要把灰度图像二值化,得到二值化图像,这样子有利于在对图像做进一步处理时,图像的集合性质只与像素值为0或255的点的位置有关,不再涉及像素的多级值,使处理变得简单,而且数据的处理和压缩量小。为了得到理想的二值图像,一般采用封闭、连通的边界定义不交叠的区域。所有灰度大于或等于阈值的像素被判定为属于特定物体,其灰度值为255表示,否则这些像素点被排除在物体区域以外,灰度值为0,表示背景或者例外的物体区域。 
如果某特定物体在内部有均匀一致的灰度值,并且其处在一个具有其他等级灰度值的均匀背景下,使用阈值法就可以得到比较的分割效果。如果物体同背景的差别表现不在灰度值上(比如纹理不同),可以将这个差别特征转换为灰度的差别,然后利用阈值选取技术来分割该图像。动态调节阈值实现图像的二值化可动态观察其分割图像的具体结果。

3、二值化的全局阈值算法源代码

using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace Legalsoft.Truffer.ImageTools
{
    public static partial class BinarizationHelper
    {
        #region 灰度图像二值化 全局算法 基本阈值 算法

        /// 
        /// 最基本的二值算法
        /// 
        /// 灰度化的图片数据
        /// 灰度阈值
        public static void Threshold_Algorithm(byte[,] data, int threshold = 127)
        {
            int height = data.GetLength(0);
            int width = data.GetLength(1);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    data[y, x] = (byte)((data[y, x] > threshold) ? 255 : 0);
                }
            }
        }

        #endregion
    }
}

选择不同的阈值,获得的二值图是完全不同的。

 4、调用方法及显示图片的代码


private void button2_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();

    FileInfo[] fileInfos = new DirectoryInfo(Path.Combine(Application.StartupPath, @"photos")).GetFiles();

    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");

    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");

    int idx = 1;
    foreach (FileInfo file in fileInfos)
    {
        if (file.Extension != ".png") continue;

        Image img = Image.FromFile(file.FullName);
        Bitmap bmp = new Bitmap(img);
        byte[] data = BitmapHelper.BitmapToArray(bmp, out int stride);
        byte[,] gray = BinarizationHelper.ColorToGrayArray(data, img.Width, img.Height, stride, 3);

        sb.AppendLine("");

        {
            sb.AppendLine("");
            Bitmap gm = BinarizationHelper.GrayBufferToBitmap(gray, stride);
            string fm = Path.Combine(Application.StartupPath, "photos", "page", String.Format("{0:D3}", idx) + "-001.jpg");
            gm.Save(fm);
            sb.AppendLine("");
        }

        for (int k = 32, num = 2; k <= 160; k += 32, num++)
        {
            byte[,] temp = (byte[,])gray.Clone();
            BinarizationHelper.Threshold_Algorithm(temp, k);
            string fm = Path.Combine(Application.StartupPath, "photos", "page", String.Format("{0:D3}-{1:D3}", idx, num) + ".jpg");
            Bitmap gm = BinarizationHelper.GrayBufferToBitmap(temp, stride);
            gm.Save(fm);
            sb.AppendLine("");
        }

        sb.AppendLine("");
        idx++;
    }

    sb.AppendLine("
原图灰度图阈值
32
阈值
64
阈值
96
阈值
128
阈值
160
"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("
"); sb.AppendLine(""); sb.AppendLine(""); File.WriteAllText(@"photos\page\01.html", sb.ToString(), Encoding.UTF8); }

其中必要的支持函数请阅读:

C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码icon-default.png?t=M85Bhttps://blog.csdn.net/beijinghorn/article/details/128425984?spm=1001.2014.3001.5501

POWER BY 315SOFT.COM & TRUFFER.CN 

C#,图像二值化(03)——全局阈值的基本算法及其源程序_第2张图片

你可能感兴趣的:(C#算法演义,Algorithm,Recipes,C#实用代码,Coding,Recipes,c#,算法,开发语言)