Win8 Metro(C#)数字图像处理--2.59 P分位法图像二值化

本文详细介绍了P参数法图像分割算法的原理、实现步骤及应用实例,旨在帮助读者理解如何通过已知目标占比来自动寻找最优阈值进行图像分割。


[函数名称]

  P分位法图像二值化

[算法说明]

  所谓P分位法图像分割,就是在知道图像中目标所占的比率Ratio时,循环不同的灰度值对图像进行

分割,并计算对应的目标所占的比率,如果该比率与Ratio的差值足够小,那么该阈值就是所求的最

佳分割阈值。

        /// <summary>
        /// P-Parameter method of image segmention.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="P">The ratio of object, from 0 to 1.</param>
        /// <returns></returns>
         public static WriteableBitmap PParameterThSegment(WriteableBitmap src,double P) ////P参数法阈值分割
        {
            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 C1 = 0, C2 = 0;
                //定义阈值变量
                int Th = 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)
                        {
                            C1++;
                        }
                        else
                        {
                            C2++;
                        }
                    }
                    double t = Math.Abs((double)((double)C1 / ((double)C1 + (double)C2)) - P);
                    if (t < 0.01)
                    {
                        Th = T;
                        break;
                    }
                    C1 = 0;
                    C2 = 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;
            }
        }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值