WPF:图像处理(三)二值化
来源:互联网 发布:linux gtk 编辑:程序博客网 时间:2024/05/16 18:05
作者:Splash
转自:http://blog.csdn.net/jhqin/article/details/7472212
- /* ----------------------------------------------------------
- 文件名称: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
- WPF:图像处理(三)二值化
- WPF:图像处理(三)二值化
- WPF:图像处理(*)二值化演示程序及源代码下载
- WPF:图像处理(*)二值化演示程序及源代码下载
- WPF备忘录三(图像裁切)
- WPF:图像处理(二)灰度化
- WPF:图像处理(四)大津法
- WPF:图像处理(五)迭代法
- WPF:图像处理(二)灰度化
- WPF:图像处理(四)大津法
- WPF:图像处理(五)迭代法
- WPF的图像处理
- WPF中的图像处理简介
- WPF中的图像处理简介
- WPF:图像处理(一)图像文件获取与预览
- WPF:图像处理(一)图像文件获取与预览
- 图像处理(三)直方图匹配
- 常用图像处理算法(三)
- WPF:图像处理(一)图像文件获取与预览
- 一个简单的例子说明dynamic_cast的作用
- WPF:图像处理(二)灰度化
- Codeforces Round #231 (Div. 2)
- 铁路通订票助手破解
- WPF:图像处理(三)二值化
- vijos1698题解
- java调用c/c++编写的第三方dll动态链接库
- WPF:图像处理(四)大津法
- codeforces div2 round#231 B
- Linux之mount与umount命令
- WPF:图像处理(五)迭代法
- vijos1027题解
- Johnny仔推荐的2个2014年最值得收藏的微信公众号