Win8 Metro(C#)数字图像处理--2.52图像K均值聚类

简介: 原文:Win8 Metro(C#)数字图像处理--2.52图像K均值聚类  [函数名称]   图像KMeans聚类      KMeansCluster(WriteableBitmap src,int k) /// /// KMeans Cluster process.
原文: 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;
            }
        }

目录
相关文章
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
220 0
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
123 0
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
153 0
|
10月前
|
数据采集 JavaScript C#
C#图像爬虫实战:从Walmart网站下载图片
C#图像爬虫实战:从Walmart网站下载图片
|
并行计算 算法 C#
C# Mandelbrot和Julia分形图像生成程序更新到2010-9-14版 支持多线程计算 多核处理器
此文档是一个关于分形图像生成器的介绍,作者分享了个人开发的M-J算法集成及色彩创新,包括源代码和历史版本。作者欢迎有兴趣的读者留言交流,并提供了邮箱(delacroix_xu@sina.com)以分享资源。文中还展示了程序的发展历程,如增加了真彩色效果、圈选放大、历史记录等功能,并分享了几幅精美的分形图像。此外,还提到了程序的新特性,如导入ini文件批量输出图像和更新一批图片的功能。文档末尾附有多张程序生成的高分辨率分形图像示例。
|
存储 编解码 算法
C#.NET逃逸时间算法生成分形图像的毕业设计完成!晒晒功能
该文介绍了一个使用C#.NET Visual Studio 2008开发的程序,包含错误修复的Julia、Mandelbrot和优化过的Newton三种算法,生成色彩丰富的分形图像。作者改进了原始算法的效率,将内层循环的画点操作移至外部,提升性能。程序提供五种图形模式,支持放大缩小及颜色更新,并允许用户自定义画布大小以调整精度。还具备保存为高质JPG的功能。附有四张示例图片展示生成的分形效果。
|
存储 机器人 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换为Bitmap图像功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换为Bitmap图像功能(C#)
146 1
|
存储 新制造 C#
Baumer工业相机堡盟工业相机如何使用OpenCV实现相机图像的显示(C#)
Baumer工业相机堡盟工业相机如何使用OpenCV实现相机图像的显示(C#)
178 1
|
存储 传感器 监控
工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)
工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)
556 0
|
传感器 存储 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现Mono12和Mono16位深度的图像保存(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现Mono12和Mono16位深度的图像保存(C#)
158 0

热门文章

最新文章