WPF:图像处理(三)二值化

/* ----------------------------------------------------------
文件名称:Binarize.cs

作者:秦建辉

MSN:splashcn@msn.com
QQ:36748897

博客:http://blog.csdn.net/jhqin

开发环境:
    Visual Studio V2010
    .NET Framework 4 Client Profile

版本历史:
    V1.1    2012年04月17日
            实现迭代法阈值计算方法

    V1.0    2012年04月16日
            实现大津法阈值计算方法
------------------------------------------------------------ */
using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Splash.Imaging
{
    /// <summary>
    /// 二值化方法
    /// </summary>
    public enum BinarizationMethods
    {        
        Otsu,       // 大津法
        Iterative   // 迭代法
    }

    /// <summary>
    /// 图像处理:图像二值化
    /// </summary>
    public static partial class Binarize
    {      
        /// <summary>
        /// 全局阈值图像二值化
        /// </summary>
        /// <param name="bitmap">原始图像</param>
        /// <param name="method">二值化方法</param>
        /// <param name="threshold">输出:全局阈值</param>
        /// <returns>二值化后的图像数组</returns>        
        public static Byte[,] ToBinaryArray(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)
        {   // 位图转换为灰度数组
            Byte[,] GrayArray = bitmap.ToGrayArray();
            
            // 计算全局阈值
            if (method == BinarizationMethods.Otsu)
                threshold = OtsuThreshold(GrayArray);
            else
                threshold = IterativeThreshold(GrayArray);

            // 根据阈值进行二值化
            Int32 PixelHeight = bitmap.PixelHeight;
            Int32 PixelWidth = bitmap.PixelWidth;
            Byte[,] BinaryArray = new Byte[PixelHeight, PixelWidth];
            for (Int32 i = 0; i < PixelHeight; i++)
            {
                for (Int32 j = 0; j < PixelWidth; j++)
                {
                    BinaryArray[i,j] = Convert.ToByte((GrayArray[i,j] > threshold) ? 255 : 0);
                }
            }

            return BinaryArray;
        }

        /// <summary>
        /// 全局阈值图像二值化
        /// </summary>
        /// <param name="bitmap">原始图像</param>
        /// <param name="method">二值化方法</param>
        /// <param name="threshold">输出:全局阈值</param>
        /// <returns>二值化图像</returns>
        public static BitmapSource ToBinaryBitmap(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)
        {   // 位图转换为灰度数组
            Byte[,] GrayArray = bitmap.ToGrayArray();

            // 计算全局阈值
            if (method == BinarizationMethods.Otsu)
                threshold = OtsuThreshold(GrayArray);
            else
                threshold = IterativeThreshold(GrayArray);

            // 将灰度数组转换为二值数据
            Int32 PixelHeight = bitmap.PixelHeight;
            Int32 PixelWidth = bitmap.PixelWidth;
            Int32 Stride = ((PixelWidth + 31) >> 5) << 2;
            Byte[] Pixels = new Byte[PixelHeight * Stride];
            for (Int32 i = 0; i < PixelHeight; i++)
            {
                Int32 Base = i * Stride;
                for (Int32 j = 0; j < PixelWidth; j++)
                {
                    if (GrayArray[i, j] > threshold)
                    {
                        Pixels[Base + (j >> 3)] |= Convert.ToByte(0x80 >> (j & 0x7));
                    }
                }
            }

            // 从灰度数据中创建灰度图像
            return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, Pixels, Stride);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值