QQ登录

只需一步,快速开始

登录 | 立即注册 | 找回密码
微像素首页 图像处理 图像基础 查看内容
订阅

文章

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

发表时间:2016-5-10 00:41作者:bean 阅读(1184) 评论: 1

导读: 本文详细介绍了图像基本变换---图像二值化(包含OSTU/迭代法/统计法/双峰法/P分位法/最大熵法)的相关知识,并 给出了完整程序DEMO的下载链接,跟大家分享一下,希望大家喜欢!

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

            {


鲜花

握手

雷人

路过

鸡蛋
上一篇:图像基本变换--- 平移、旋转、缩放、仿射变换、镜像
发表评论

最新评论

引用 卡图Catus 2016-5-18 09:05
OTSU方法和迭代法中求解B类均值的时候,为什么都乘以了A类的均值?不太明白...

查看全部评论(1)

    联系我们
  • QQ:3243218172
  • 邮箱:3243218172@qq.com
  • QQ交流群:567648913
    手机扫描二维码