zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码

c#图像处理 处理 函数 基本 一些 图像 源代码
2023-09-11 14:15:48 时间

上一篇:

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

1、Image binarization

The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.

Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.

Binarization can be used in recognising text and symbols, e.g. document processing. Identifying objects with distinctive silhouettes, e.g. components on a conveyor in a manufacturing plant, and determining the orientation of objects are some other examples of binarization applications. Binarization generally involves two steps including the determination of a gray threshold according to some objective criteria and assigning each pixel to one class of background or foreground. If the intensity of the pixel is greater than the determined threshold then it belongs to the foreground class and otherwise to the background. The main problem in binarization is the choice of thresholding technique.

2、Grayscale image

A grayscale image is the one that has the same value for each channel. Grayscale images have only colours of gray shades, Gray shades are not only black and white. If the image has 8-bit depth. It can have 255 shades of gray between dark and white. Sometimes grayscale images are also called black and white images.
 

3、Some Support C# Sourcecodes POWER BY TRUFFER.CN

本页面的C#代码包括但不限于:图像Bitmap转byte[]数组;灰度直方图计算;灰度最大值、灰度最小值计算;直方图归一化计算等等。

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 基础函数

        /// <summary>
        /// 图片转存为byte数组
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static byte[] BitmapToArray(Bitmap bitmap, out int img_stride)
        {
            try
            {
                // 定义锁定bitmap的rect的指定范围区域
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                // 加锁区域像素
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                // 位图的首地址
                var ptr = bitmapData.Scan0;
                // stride:扫描行
                img_stride = bitmapData.Stride;
                int len = bitmapData.Stride * bitmap.Height;
                byte[] img_bytes = new byte[len];
                // 锁定区域的像素值copy到byte数组中
                Marshal.Copy(ptr, img_bytes, 0, len);
                // 解锁
                bitmap.UnlockBits(bitmapData);

                return img_bytes;
            }
            catch (Exception ex)
            {
                throw new Exception("BitmapHelper.BitmapToArray ERROR:" + ex.Message);
            }
        }

        /// <summary>
        /// 极大极小值限定(0-255)
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static double Clamp(double v)
        {
            v = (v > 255.0) ? 255.0 : ((v < 0.0) ? 0.0 : v);
            return v;
        }

        /// <summary>
        /// 灰度计算(简单)
        /// </summary>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static byte GreyByte(byte r, byte g, byte b)
        {
            return (byte)Clamp(r * 0.299 + g * 0.587 + b * 0.114);
        }

        /// <summary>
        /// 彩色图转灰度图
        /// </summary>
        /// <param name="data"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="stride"></param>
        /// <param name="channels"></param>
        public static byte[,] ColorToGrayArray(byte[] data, int width, int height, int stride, int channels = 3)
        {
            byte[,] grayArray = new byte[height, width];
            for (int bz = 0, y = 0; y < height; y++)
            {
                for (int zz = bz, x = 0; x < width; x++)
                {
                    byte g = GreyByte(data[zz + 2], data[zz + 1], data[zz + 0]);
                    grayArray[y, x] = g;
                    zz += channels;
                }
                bz += stride;
            }
            return grayArray;
        }

        /// <summary>
        /// 最小与最大灰度
        /// </summary>
        /// <param name="data"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static void Minium_Maxium(byte[,] data, out int min, out int max)
        {
            int height = data.GetLength(0);
            int width = data.GetLength(1);
            max = 0;
            min = 255;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    byte bt = data[y, x];
                    if (bt < min)
                    {
                        min = bt;
                    }
                    if (bt > max)
                    {
                        max = bt;
                    }
                }
            }
        }

        /// <summary>
        /// 灰度直方图统计
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int[] Gray_Histogram(byte[,] data)
        {
            int height = data.GetLength(0);
            int width = data.GetLength(1);
            int[] hv = new int[256];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    hv[data[y, x]]++;
                }
            }
            return hv;
        }

        /// <summary>
        /// 左端第一个非零
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static int Histogram_Left(int[] histogram)
        {
            for (int i = 0; i < histogram.Length; i++)
            {
                if (histogram[i] > 0)
                {
                    return i;
                }
            }
            return 0;
        }

        /// <summary>
        /// 右端第一个非零
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static int Histogram_Right(int[] histogram)
        {
            for (int i = histogram.Length - 1; i >= 0; i--)
            {
                if (histogram[i] > 0)
                {
                    return i;
                }
            }
            return 0;
        }

        /// <summary>
        /// 直方图和值
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static int Histogram_Sum(int[] histogram)
        {
            return histogram.Sum();
        }

        /// <summary>
        /// 加权和值
        /// </summary>
        /// <param name="histogram"></param>
        /// <param name="right"></param>
        /// <param name="grade"></param>
        public static double Histogram_Sum(int[] histogram, int grade = 0, int right = 255)
        {
            double sum = 0.0;
            for (int i = 0; i < histogram.Length && i <= right; i++)
            {
                double v = (double)histogram[i];
                if (grade >= 1) v *= (double)i;
                if (grade >= 2) v *= (double)i;
                if (grade >= 3) v *= (double)i;
                sum += v;
            }
            return sum;
        }

        /// <summary>
        /// 直方图数据转为浮点数数组
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static double[] Histogram_To_Float(int[] histogram)
        {
            double[] hv = new double[histogram.Length];
            for (int i = 0; i < histogram.Length; i++)
            {
                hv[i] = histogram[i];
            }
            return hv;
        }

        /// <summary>
        /// 直方图归一化
        /// </summary>
        /// <param name="histogram"></param>
        public static double[] Histogram_Normalize(int[] histogram)
        {
            double sum = 1.0 / histogram.Sum();
            double[] hn = new double[256];
            for (int i = 0; i < histogram.Length; i++)
            {
                hn[i] = (double)histogram[i] * sum;
            }
            return hn;
        }

        /// <summary>
        /// 灰度图的直方图均衡化处理
        /// </summary>
        /// <param name="data"></param>
        public static void Gray_Equalization(byte[,] data)
        {
            int height = data.GetLength(0);
            int width = data.GetLength(1);

            int[] histogram = Gray_Histogram(data);
            double[] density = Histogram_Normalize(histogram);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    byte c = data[y, x];
                    double dsum = 0.0;
                    for (int k = 0; k <= c; k++)
                    {
                        dsum += density[k];
                    }
                    data[y, x] = (byte)Math.Round(255.0 * dsum);
                }
            }
        }

        #endregion
    }
}

 这些函数将被后续发布的二十三种二值化算法所使用。

The thresholding algorithms can be categorized into different classes: 

Histogram shape-based methods 
. Clustering-based methods 
. Entropy-based methods
. Object attribute-based methods
. Spatial methods and local methods are based on the local characteristics of each pixel.
 

The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.

Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.
 

下一篇:

C#,图像二值化(03)——全局阈值的基本算法及其源程序icon-default.png?t=MBR7https://blog.csdn.net/beijinghorn/article/details/128426857