首页 技术 正文
技术 2022年11月14日
0 收藏 833 点赞 2,679 浏览 1711 个字

原文: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;
}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291