Win8 Metro(C#)数字图像处理--2.52图像K均值聚类
原文:Win8 Metro(C#)数字图像处理--2.52图像K均值聚类
[函数名称]
图像KMeans聚类 KMeansCluster(WriteableBitmap src,int k)
/// <summary>
/// KMeans Cluster process.
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="k">Cluster threshould, from 2 to 255.</param>
/// <returns></returns>
public static WriteableBitmap KMeansCluster(WriteableBitmap src,int k)////KMeansCluster
{
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 b = 0, g = 0, r = 0;
//定义灰度图像信息存储变量
byte[] imageData = new byte[w * h];
//定义聚类均值存储变量(存储每一个聚类的均值)
double[] meanCluster = new double[k];
//定义聚类标记变量(标记当前像素属于哪一类)
int[] markCluster = new int[w * h];
//定义聚类像素和存储变量(存储每一类像素值之和)
double[] sumCluster = new double[k];
//定义聚类像素统计变量(存储每一类像素的数目)
int []countCluster = new int[k];
//定义聚类RGB分量存储变量(存储每一类的RGB三分量大小)
int[] sumR = new int[k];
int[] sumG = new int[k];
int[] sumB = new int[k];
//临时变量
int sum = 0;
//循环控制变量
bool s = true;
double[] mJduge = new double[k];
double tempV = 0;
int cou = 0;
//获取灰度图像信息
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
b = tempMask[i * 4 + j * w * 4];
g = tempMask[i * 4 + 1 + j * w * 4];
r = tempMask[i * 4 + 2 + j * w * 4];
imageData[i + j * w] = (byte)(b * 0.114 + g * 0.587 + r * 0.299);
}
}
while (s)
{
sum = 0;
//初始化聚类均值
for (int i = 0; i < k; i++)
{
meanCluster[i] = i * 255.0 / (k - 1);
}
//计算聚类归属
for (int i = 0; i < imageData.Length; i++)
{
tempV = Math.Abs((double)imageData[i] - meanCluster[0]);
cou = 0;
for (int j = 1; j < k; j++)
{
double t = Math.Abs((double)imageData[i] - meanCluster[j]);
if (tempV > t)
{
tempV = t;
cou = j;
}
}
countCluster[cou]++;
sumCluster[cou] += (double)imageData[i];
markCluster[i] = cou;
}
//更新聚类均值
for (int i = 0; i < k; i++)
{
meanCluster[i] = sumCluster[i] / (double)countCluster[i];
sum += (int)(meanCluster[i] - mJduge[i]);
mJduge[i] = meanCluster[i];
}
if (sum == 0)
{
s = false;
}
}
//计算聚类RGB分量
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
sumB[markCluster[i + j * w]] += tempMask[i * 4 + j * w * 4];
sumG[markCluster[i + j * w]] += tempMask[i * 4 + 1 + j * w * 4];
sumR[markCluster[i + j * w]] += tempMask[i * 4 + 2 + j * w * 4];
}
}
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
temp[i * 4 + j * 4 * w] = (byte)(sumB[markCluster[i + j * w]] / countCluster[markCluster[i + j * w]]);
temp[i * 4 + 1 + j * 4 * w] = (byte)(sumG[markCluster[i + j * w]] / countCluster[markCluster[i + j * w]]);
temp[i * 4 + 2 + j * 4 * w] = (byte)(sumR[markCluster[i + j * w]] / countCluster[markCluster[i + j * w]]);
}
}
Stream sTemp = dstImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return dstImage;
}
else
{
return null;
}
}
Win8 Metro(C#)数字图像处理--2.52图像K均值聚类的更多相关文章
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
- Win8 Metro(C#)数字图像处理--3.3图像直方图计算
原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...
- Win8 Metro(C#)数字图像处理--3.4图像信息熵计算
原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...
- Win8 Metro(C#)数字图像处理--3.5图像形心计算
原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...
- Win8 Metro(C#)数字图像处理--3.1图像均值计算
原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.74图像凸包计算
原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器
原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称] 图像雾化 AtomizationProcess(WriteableBitmap src,i ...
- Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果
原文:Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果 [函数名称] RGB分量调整 RGBAdjustProcess(WriteableBitmap ...
随机推荐
- [GraphQL] Add an Interface to a GraphQL Schema
As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...
- php标准库spl栈SplStack如何使用?
php标准库spl栈SplStack如何使用? 一.总结 php标准库spl栈SplStack介绍.(SplStack类)(各种方法都支持) 1.SplStack类:$stack = new SplS ...
- NSString与int和float的相互转换
NSString *tempA = @"123"; NSString *tempB = @"456"; 1,字符串拼接 NSString *newString ...
- Erlang学习笔记2
http://wgcode.iteye.com/blog/1007623 第二章 入门 1.所有的变量都必须以大写字母开头,如果要查看某个变量,只要输入变量的名字即可,如果一个变量被赋予值,就称为绑定 ...
- Oracle中的游标(转)
Oracle中的游标有两种:显式游标.隐式游标.显示游标是用cursor...is命令定义的游标,它可以对查询语句(select)返回的多条记录进行处理,而隐式游标是在执行插入 (insert).删除 ...
- matlab 运行 AlexNet
0. alexnet 工具箱下载 下载地址:Neural Network Toolbox(TM) Model for AlexNet Network 需要先注册(十分简单),登陆,下载: 下载完成之后 ...
- 小强的HTML5移动开发之路(26)—— JavaScript回顾1
很久没有怎么用过JavaScript了,感觉有点生疏,最近在看关于HTML5移动开发方面的资料,有一种直觉告诉我,JavaScript昨天.今天很重要,明天会更重要.现在好多基于JavaScript的 ...
- 【25.47%】【codeforces 733D】Kostya the Sculptor
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Bridge 、 Adapter 和 Facade 的区别
Bridge和Adapter是没有关系的,而和Facade比较象,但在我的经验中更多的时候是会混淆Bridge和adapter而不是Facade,这里详细的列出三个模式的比较. 一. 定义: 1.Fa ...
- 防止SQL/XSS攻击
function clean($str) { $str=trim($str); $str=strip_tags($str); $str=stripslashes($str ...