图像基本变换---图像二值化(包含OSTU/迭代法/统计法/双峰法/P分位法/最大熵法)

OSTU法图像二值化

[算法说明]

  Ostu法又叫做最大类间方差法,是一种常用的图像分割算法。基本算法思想是根据初始阈值把图像分为两类,然后计算两类之间的方差,更新阈值,重新计算类间方差,当满足类间方差最大时的阈值,即为所求最佳阈值,具体过程如下:

  1,初始化一阈值Th,将图像f(x,y)分为AB两类;

  2,分别计算AB两类像素集合的均值ua和ub,公式如下:

  其中,Na和Nb分别表示集合AB中的像素个数。

  3,计算AB两类的类间方差,公式如下:

  

  4,将Th0255循环,分别计算AB的类间方差,当类间方差最大时,对应的Th即为所求的最佳分割或二值化的阈值。

[函数代码]

        /// <summary>

        /// Ostu method of image segmention.

        /// </summary>

        /// <param name="src">The source image.</param>

        /// <returns></returns>

        public static WriteableBitmap OstuThSegment(WriteableBitmap src) Ostu法阈值分割

        {

            if (src != null)

            {

                int w = src.PixelWidth;

                int h = src.PixelHeight;

                WriteableBitmap dstImage = new WriteableBitmap(w, h);

                byte[] temp = src.PixelBuffer.ToArray();

                byte[] tempMask = (byte[])temp.Clone();

                //定义灰度图像信息存储变量

                int[] srcData = new int[w * h];

                //定义阈值变量

                int Th = 0; ;

                //定义背景和目标像素数目变量N1,N2,灰度变量U1,U2,灰度和变量Sum1,Sum2,临时缓存变量Temp

                int N1 = 0, N2 = 0, Sum1 = 0, Sum2 = 0;

                //定义背景和目标像素比例变量W1,W2,图像整体平均灰度变量U,方差变量g,对比阈值变量TT

                double W1 = 0, W2 = 0, U1 = 0, U2 = 0, g = 0, TT = 0;

                for (int j = 0; j < h; j++)

                {

                    for (int i = 0; i < w; i++)

                    {

                        srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299);

                    }

                }

                //寻找最大类间方差

                for (int T = 0; T <= 255; T++)

                {

                    for (int i = 0; i < srcData.Length; i++)

                    {

                        if (srcData[i] > T)

                        {

                            N2++;

                            Sum2 += srcData[i];

                        }

                        else

                        {

                            N1++;

                            Sum1 += srcData[i];

                        }

                    }

                    W1 = (double)(N1 / (N1 + N2));

                    W2 = (double)(1.0 - W1);

                    U1 = (N1 == 0 ? 0.0 : (Sum1 / N1));

                    U2 = (N2 == 0 ? 0.0 : (Sum2 / N2));

                    g = N1 * N2 * (U1 - U2) * (U1 - U2);

                    if (g > TT)

                    {

                        TT = g;

                        Th = T;

                    }

                    N1 = 0; N2 = 0;

                    Sum1 = 0; Sum2 = 0; W1 = 0.0; W2 = 0.0; U1 = 0.0; U2 = 0.0; g = 0.0;

                }

                for (int j = 0; j < h; j++)

                {

                    for (int i = 0; i < w; i++)

                    {

                        temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);

                    }

                }

                Stream sTemp = dstImage.PixelBuffer.AsStream();

                sTemp.Seek(0, SeekOrigin.Begin);

                sTemp.Write(temp, 0, w * 4 * h);

                return dstImage;

            }

            else

            {

                return null;

            }

        }

[图像效果]

Fig.1原图                              Fig.2效果图





迭代法图像二值化

[算法说明]

  迭代法图像二值化的算法思想是:首先,初始化一个阈值Th,然后按照某种策略通过迭代不断更新这一阈值,直到满足给定的约束条件为止,步骤如下:

  1,对于一幅图像,假设当前像素为f(x,y),设定一阈值Th,跟据当前阈值,循环f(x,y),将图像分为两类像素的集合AB

  2,分别计算AB集合的像素均值ua和ub,公式如下:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值