原文: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均值聚类的更多相关文章

  1. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

  2. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...

  3. Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

    原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...

  4. Win8 Metro(C#)数字图像处理--3.5图像形心计算

    原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...

  5. Win8 Metro(C#)数字图像处理--3.1图像均值计算

    原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...

  6. Win8 Metro(C#)数字图像处理--2.74图像凸包计算

    原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...

  7. Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器

    原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...

  8. Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法

    原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称]   图像雾化         AtomizationProcess(WriteableBitmap src,i ...

  9. Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果

    原文:Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果  [函数名称] RGB分量调整         RGBAdjustProcess(WriteableBitmap  ...

随机推荐

  1. [React] Break up components into smaller pieces using Functional Components

    We are going to ensure our app is structured in a clear way using functional components. Then, we ar ...

  2. [Most.js] Create Streams From Single Values With Most.js

    Most provides many means for creating streams, the simplest of which is the offunction. In this less ...

  3. [javase学习笔记]-6.5 类类型參数与匿名对象

    这一节我们来说说类类型參数和匿名对象. 我们继续用之前的小汽车类吧 class Car { int num;//这是轮胎数属性 String color;//这是颜色属性 String brand;/ ...

  4. 《Erlang程序设计》学习笔记-第1章 编译并运行程序

    http://blog.csdn.net/karl_max/article/details/3976372 1. erlang:halt()可以即刻停止系统运行. 2. q()命令可以完成文件和数据库 ...

  5. sqlserver中的存储过程 函数 事物 索引及视图

                                           存储过程和函数具体的区别: 核心提示:本质上没区别.只是函数有限制只能返回一个标量,而存储过程可以返回多个.并且函数是可以 ...

  6. 阿里云centos7.2自己安装mysql5.7远程不能访问解决方案

    版权声明:转载也行 https://blog.csdn.net/u010955892/article/details/72774920 最近,无意中看到阿里云服务器降价,所以一时手痒,买了一年的服务器 ...

  7. negative binomial(Pascal) distribution —— 负二项式分布(帕斯卡分布)

    1. 定义 假设一串独立的伯努利实验(0-1,成功失败,伯努利实验),每次实验(trial)成功和失败的概率分别是 p 和 1−p.实验将会一直重复下去,直到实验失败了 r 次.定义全部实验中成功的次 ...

  8. 关于ExpandableListView的一个小例子

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  9. java多线程模拟生产者消费者问题,公司面试常常问的题。。。

    package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...

  10. 对偶空间(dual linear space)

    1. 定义 设 V 为定义在数域 F 上的向量空间,定义 V 上的线性函数是从 V 到 F 的映射:f:V→F,且满足 ∀x,y∈V,k∈F 有:f(x+y)=f(x)+f(y),f(ka)=kf(a ...