C#,图像二值化(22)——局部阈值的伯恩森算法(Bernsen Thresholding)及源程序

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1、局部阈值的伯恩森算法Bernsen Thresholding

Bernsen方法是为图像分割开发的局部自适应二值化方法之一。在这项研究中实现了Bernsen的局部自适应二值化方法并对不同灰度图像进行了测试。

Bernsen’s method is one of locally adaptive binarization methods developed for image segmentation. In this study, Bernsen’s locally adaptive binarization method is implemented and then tested for different grayscale images. In this tutorial, we will use “luispedro” image, below is the command to load it.

该方法使用用户提供的对比度阈值。如果局部对比度max-min高于或等于对比度阈值则将阈值设置为局部中间值局部窗口中最小和最大灰度值的平均值。如果局部对比度低于对比度阈值则认为邻域仅由一个类组成并且根据中肠的值将像素设置为对象或背景。

The method uses a user-provided contrast threshold. If the local contrast (max-min) is above or equal to the contrast threshold, the threshold is set at the local midgrey value (the mean of the minimum and maximum grey values in the local window). If the local contrast is below the contrast threshold the neighbourhood is considered to consist only of one class and the pixel is set to object or background depending on the value of the midgrey.

在数字图像处理中二值化二级阈值是一种常用的图像分割技术。这是将灰度图像转换为二进制图像的过程。此外二值化方法分为全局二值化和局部自适应二值化两组。多年来已经提出了许多二值化技术。Bernsen方法是为图像分割开发的局部自适应二值化方法之一。在这项研究中实现了Bernsen的局部自适应二值化方法并对不同灰度图像进行了测试。

In digital image processing, binarization (two-level thresholding) is a commonly used technique for image segmentation. It is the process of converting a gray scale image to a binary image. Furthermore, binarization methods are divided into two groups as global binarization and locally adaptive binarization. A number of binarization techniques have been proposed over the years. Bernsen’s method is one of locally adaptive binarization methods developed for image segmentation. In this study, Bernsen’s locally adaptive binarization method is implemented and then tested for different gray scale images.

由于适用于数字图像处理的许多领域二值化是一种众所周知的图像分割方法Sahoo等人1988。为了将对象与背景分离这是一种有效的技术。多年来已经提出了许多二值化应用例如用于提取印刷字符、徽标、图形内容或乐谱的文档图像分析、用于查找线条、图例或字符的位置的地图处理、用于检测目标的场景处理、材料的质量检查、视频图像的边缘场提取和时空分割Sezgin和Sankur2004。近年来文档图像分析领域受到了广泛关注并已成为数字图像处理的重要组成部分之一。有许多研究人员正在寻求设计从地图、杂志、报纸、工程图纸、表格和邮件等广泛文档中提取信息的系统。在大多数这些系统中二值化作为第一步应用Trier和Taxt1995a。文档图像二值化的目的是从图像中提取文本去除噪声并减小图像大小。该过程是在去除无用信息的情况下进行的以提高图像中有用信息的可见性Bataineh等人2011。在Kefali等人2010的工作中二值化的目的是减少不期望数据的存在并保存文档图像中的期望数据。此操作通过将图像的所有灰度级转换为黑白两级来完成。二值化技术分为两组。这些是全局二值化和局部自适应二值化Singh等人2011。全局二值化方法计算整个图像的单个阈值。具有比阈值暗的灰度级的像素被标记为黑色前景。在相反的情况下其他像素被标记为白色背景Trier和Jain1995。文献中存在的一些全局二值化方法包括Abutaleb方法Abutaleb1989、Kapur等人的方法Kapur等人1985、Kittler和Illingworth方法Kittler and Illingworth1986和Otsu方法Otsu1979。此外局部自适应二值化方法基于包含在像素的邻域中的信息来计算每个像素的阈值。其中一些方法在整个图像上计算阈值曲面。在输入图像中如果像素xy具有比在xy处评估的阈值表面更高的灰度级则将像素x、y标记为背景否则将其标记为前景Trier和Jain1995。在文献中一些局部自适应二值化方法是Bernsen方法Bernsen1986、Chow和Kaneko方法Chow和Kaneko1972Nakagawa和Rosenfeld1979、Eikvil等人的方法Eikvil et al.1991、Mardia和Hainsworth方法Mardia和Heinsworth1988、Niblack方法Niblack1986和Taxt等人的方法Yanowitz和Bruckstein的方法Yanowitz and Bruckstein1989、White和Rohrer的动态阈值算法White and Rohrer1983、White和Rohreer的综合函数算法Whiteand Rohreer1983Parker的方法Parker1991以及Trier和Taxt的方法Trier and Taxt1995b。

Binarization is a well-known method for image segmentation due to applicability to many fields in digital image processing (Sahoo et al., 1988). In order to separate objects from background, it is an effective technique. A number of binarization applications have been proposed over the years such as document image analysis for extracting printed characters, logos, graphical content or musical scores, map processing for finding place of lines, legends or characters, scene processing for detecting a target, quality inspection of materials, extraction of edge field and spatio-temporal segmentation of video images (Sezgin and Sankur, 2004). In recent years, the field of document image analysis has received significant attention and has become one of the important parts of digital image processing. There are various researchers which are seeking to design systems for extracting information from extensive documents as maps, magazines, newspapers, engineering drawings, forms and mails. In most of these systems, binarization is applied as the first step (Trier and Taxt, 1995a). The aim of the binarization of document images is extracting text from images, removing noise and reducing image size. This process is performed with removing useless information in order to increase visibility of useful information in an image (Bataineh et al., 2011). In the work of Kefali et al. (2010), it is asserted that the purpose of binarization is to decrease the existence of undesirable data and conserve the desired data in document images. This operation is done by converting all gray levels of images into two levels as black and white. Binarization techniques are divided into two groups. These are global binarization and locally adaptive binarization (Singh et al., 2011). Global binarization methods compute a single threshold value for the entire image. Pixels having a gray level darker than the threshold value are marked as black (foreground). In the contrary case, the other pixels are labeled as white (background) (Trier and Jain, 1995). Some of the global binarization methods existing in the literature are Abutaleb’s method (Abutaleb, 1989), Kapur et al.’s method (Kapur et al., 1985), Kittler and Illingworth’s method (Kittler and Illingworth, 1986) and Otsu’s method (Otsu, 1979). Besides, locally adaptive binarization methods calculate a threshold value for each pixel on the basis of information contained in a neighborhood of the pixel. Some of these methods compute a threshold surface over the entire image. In the input image, if a pixel (x, y) has a higher gray level than threshold surface evaluated at (x, y), then the pixel (x, y) is marked as background, otherwise it is marked as foreground (Trier and Jain, 1995). In the literature, some of the locally adaptive binarization methods are Bernsen’s method (Bernsen, 1986), Chow and Kaneko’s method (Chow and Kaneko, 1972; Nakagawa and Rosenfeld, 1979), Eikvil et al.’s method (Eikvil et al., 1991), Mardia and Hainsworth’s method (Mardia and Hainsworth, 1988), Niblack’s method (Niblack, 1986), Taxt et al.’s method (Taxt et al., 1989), Yanowitz and Bruckstein’s method (Yanowitz and Bruckstein, 1989), White and Rohrer’s dynamic threshold algorithm (White and Rohrer, 1983), White and Rohrer’s integrated function algorithm (White and Rohrer, 1983), Parker’s method (Parker, 1991) and Trier and Taxt’s method (Trier and Taxt, 1995b).

本文的其余部分组织如下。在材料和方法部分Bernsen的局部自适应说明了二值化方法。在结果和讨论部分显示了在不同邻域值和对比度极限下将Bernsen方法应用于灰度图像的结果。最后结论部分总结了正在研究的结论。

The rest of the paper is organized as follows. In materials and methods section, Bernsen’s locally adaptive

binarization method is explained. In results and discussion section, the results of applying Bernsen’s method on gray scale images are showed for different neighborhood values and contrast limits. Finally, conclusions being under study are summarized in conclusion section.

2、局部阈值的伯恩森算法Bernsen Thresholding源程序

二值算法综述请阅读

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

namespace Legalsoft.Truffer.Binarization
{
public static partial class BinarizationHelper
{
    #region 灰度图像二值化 局部算法 Bernsen 算法

    /// <summary>
    /// Bernsen算法
    /// https://blog.csdn.net/Abaqus3_0/article/details/81205168
    /// </summary>
    /// <param name="data"></param>
    /// <param name="kernel"></param>
    /// <param name="threshold_diff"></param>
    /// <param name="global_threshold"></param>
    public static void Bernsen_Algorithm(byte[,] data, int kernel = 15, int threshold_diff = 32, int global_threshold = 128)
    {
        int height = data.GetLength(0);
        int width = data.GetLength(1);

        int halfKernel = kernel / 2;
        int[] kernelData = new int[kernel * kernel];
        byte[,] dump = (byte[,])data.Clone();
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (x < halfKernel || x > width - halfKernel - 1 ||
                    y < halfKernel || y > height - halfKernel - 1)
                {
                    data[y, x] = 0;
                }
                else
                {
                    int k = 0;
                    for (int yy = y - halfKernel; yy < y + halfKernel; yy++)
                    {
                        for (int xx = x - halfKernel; xx < x + halfKernel; xx++)
                        {
                            kernelData[k] = dump[yy, xx];
                        }
                    }

                    int kernelMax = kernelData.Max();
                    int kernelMin = kernelData.Min();
                    int Tkernel = (int)((kernelMax + kernelMin) * 0.5);

                    //灰度差值小该点要么在目标区域要么在背景区域
                    if (kernelMax - kernelMin <= threshold_diff)
                    {
                        data[y, x] = (byte)((Tkernel > global_threshold) ? 255 : 0);
                    }
                    //灰度差值大说明在边缘区域将均值作为局部阈值
                    else
                    {
                        data[y, x] = (byte)((data[y, x] > Tkernel) ? 255 : 0);
                    }
                }

            }
        }
    }

    #endregion
}
}

3、局部阈值的伯恩森算法Bernsen Thresholding计算效果

调整一些参数可以获得更好的或更不好的效果。大家可以试试。

POWER BY 315SOFT.COM & TRUFFER.CN

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6