原文: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. SQLServer重建索引

    Use [数据库名称]Go DECLARE @DBCCString NVARCHAR(1000)DECLARE @TableName VARCHAR(100)DECLARE Cur_Index CUR ...

  2. 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...

  3. Lucene + Pinyin4J 提供首字母搜索(——)

    遇到一个集团需求,要求在地址查询时候提供拼音搜索,第一反应应该不难,不过实现过程中却一波三折. 1.第一步是讲字段首字母进行索引,具体可以使用Pinyin4j提供的方法完成. 2.原来系统用的luce ...

  4. webpack单独构建scss文件与.vue组件里构建scss的一个坑

    在入口main.js里构建scss是通过引入模块的方式 import './assets/_reset.scss'; import './assets/_flex.scss'; import './a ...

  5. 经Gradle采取Jenkins的build

    如今,企业都太多Jenkins去管理apk,后该代码被提交jenkins在生成build 因此,我们可以得到jenkins提交版本 Jenkins在编制job什么时候,有一个内置的可变BUILD_NU ...

  6. PHP中的加密方式有如下几种

    1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. raw_output  --  ...

  7. jQuery整体架构

    (function(global, factory) { factory(global); }(typeof window !== "undefined" ? window : t ...

  8. 微信公众平台消息接口开发(31)微信浏览器HTTP_USER_AGENT判断

    微信公众平台开发 微信公众平台开发者 微信公众平台开发模式 微信浏览器 HTTP_USER_AGENT作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/archiv ...

  9. 在MVC项目中分页使用MvcPager插件

    参考网站  http://www.webdiyer.com/mvcpager/demos/ 这个插件非常简单易用,如果想快速使用 可以参考我这篇文章,其实参考网站也是非常简单的 首先选择你的web项目 ...

  10. 关于FileZilla上传文件后服务器端文件与本地文件大小不一致的解决方法

    最近在调试网站时发现,通过ftp上传工具FileZilla上传至服务器端的文件与本地文件大小不一致,虽然没有影响网站的最终显示效果,但仍让我困惑不解.后发现是传输类型的原因,解决方法如下: 中文版Fi ...