原文: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. android WebView总 结

    浏览器控件是每个开发环境都具备的,这为马甲神功提供了用武之地,windows的有webbrowser,android和ios都有webview.只是其引擎不同,相对于微软的webbrowser,and ...

  2. PHP的SPL标准库里面的堆(SplHeap)怎么使用

    PHP的SPL标准库里面的堆(SplHeap)怎么使用 一.总结 1.因为SplHeap是抽象类,所以要先继承,实现里面的抽象方法compare后,才能new对象使用. 二.PHP的SPL标准库里面的 ...

  3. Kinect 摄像头范围介绍和玩家舒适距离实测

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/44588097 作者:ca ...

  4. 【codeforces 758C】Unfair Poll

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. Vert.x ——概述

    Vert.x是什么 Vert.x(http://vertx.io/)是一个基于JVM.轻量级.高性能的应用平台,非常适用于最新的移动端后台.互联网.企业应用架构. Vert.x框架基于事件和异步,依托 ...

  6. git基本原理

    git基本原理 一.总结 一句话总结:把原理那张图图背下来 1.git中的四大区,除了远程仓库和本地仓库,剩下两个是什么? 解答:工作区和暂存区. 2.git中的四大区(例如远程仓库和本地仓库),他们 ...

  7. Delphi中预编译指令

    本文转自 http://www.cnblogs.com/JackSun/archive/2010/12/20/1911250.html <Delphi下深入Windows核心编程>(附录A ...

  8. 1 WCF 一个基础理论 以及如何实现一个简单wcf服务

    1 SOA : service oriented architecture 面向服务的架构 2 web service标准 3 概念理解图 4 WCF类库 项目的 wcf简单实现 首先创建一个简单的w ...

  9. NAT对于网络上的其它人都不可见(但可以转发端口),Bridged和网络里的其它物理机没区别,Internal只能在虚拟机之间可见,Host-only只对主机可见(最符合我的服务器需求),最多可设置8块网卡实现混杂

    VirtualBox 可以为每一个虚拟机分配8个网卡.每一个网卡的连接方式可以选为下列之一: Not attached Network Address Translation (NAT) Bridge ...

  10. delphi的bpl、dcp 、dcu文件意义(BPL相当于C++中的DLL,DCP相当于C++中的Lib,编译时需要)

    BPL  英文全称 Borland Package library ,是一种特殊的DLL文件,用于代码重用和减少可执行文件.编译bpl时,仅需要添加相应功能的pas文件,如果有窗体,则需要添加dfm文 ...