原文:Win8 Metro(C#)数字图像处理--2.57一维最大熵法图像二值化



[函数名称]

一维最大熵法图像二值化WriteableBitmap EntropymaxThSegment(WriteableBitmap src)

[算法说明]

一维最大熵法图像分割就是利用图像的灰度分布密度函数定义图像的信息熵,通过优化一定的熵

准则得到熵最大时对应的阈值,从而进行图像分割的方法。

算法过程:

1,对于一幅灰度图像,灰度范围为[0,L-1],求取图像的最小灰度级min,最大灰度级max;

[函数代码]

       /// <summary>
/// Entropy max method of image segmention.
/// </summary>
/// <param name="src">The source iamge.</param>
/// <returns></returns>
public static WriteableBitmap EntropymaxThSegment(WriteableBitmap src) ////一维熵最大法阈值分割
{
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;
//定义直方图存储变量
int[] histogram = new int[256];
//定义熵值变量
double Ht = 0.0;
double Hl = 0.0;
double sigma = 0.0;
//定义灰度最值变量
int max = 0;
int min = 255;
//定义临时变量
double t = 0.0, pt = 0.0, tempMax = 0.0;
int tempV = 0;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
tempV = (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);
srcData[i + j * w] = tempV;
histogram[tempV]++;
if (tempV > max)
{
max = tempV;
}
if (tempV < min)
{
min = tempV;
}
}
}
for (int i = min; i < max; i++)
{
t = (double)((double)histogram[i] / (double)(w * h));
if (t > 0.00000001)
{
Hl += -t * Math.Log10(t);
}
else
continue;
}
for (int i = min; i < max; i++)
{
t = (double)((double)histogram[i] / (double)(w * h));
pt += t;
if (t > 0.00000001)
{
Ht += -t * Math.Log10(t);
sigma = Math.Log10(pt * (1 - pt)) * Ht / pt + (Hl - Ht) / (1 - pt);
if (sigma > tempMax)
{
tempMax = (int)sigma;
Th = i;
}
}
else
continue;
}
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;
}
}

最新文章

  1. java 27 - 6 反射之 通过配置文件运行类中的方法
  2. JQuery-EasyUI DataGrid CRUD
  3. JS 瀑布流布局
  4. sort()的多种用法
  5. 8、SpringMVC源码分析(3):分析ModelAndView的形成过程
  6. 任务栏窗口和状态图标的闪动 z
  7. 传微软欲收购Xamarin:未来有望通过VS开发iOS和Android应用?
  8. Python设计模式——建造者模式
  9. OpenCV-ubuntu-install
  10. Android-----------国际化多国语言文件夹命名汇总
  11. tls和ssl
  12. c# 遍历文件夹及其所有文件
  13. Netty开发实现高性能的RPC服务器
  14. poj2635The Embarrassed Cryptographer(同余膜定理)
  15. ubuntu14.04 server ftp 服务安装配置详解
  16. Oracle EBS 查看执行计划
  17. python 中range函数的用法
  18. MVC和WebForm区别
  19. Oracle Drop表并未直接删除 drop table xx purge
  20. jeffy-vim-v3.2

热门文章

  1. SimpleDateFormat.format的简单使用小结
  2. Ajax详解及使用Ajax时的返回值类型有哪些?
  3. 【hdu 3863】No Gambling
  4. BZOJ 3631 松鼠的新家 - 树链剖分 / 树上差分
  5. Filter,Listener(转)
  6. 学习鸟哥的Linux私房菜笔记(10)——bash2
  7. 【25.23%】【codeforces 731C】Socks
  8. JAVA从本机获取IP地址
  9. 微信公众平台开发(1) 通用的工具类CommonUtil
  10. 在WSL中安装和运行Docker CE