Win8 Metro (C #) Digital image processing--2.52 image K-mean clustering

Source: Internet
Author: User

Original: Win8 Metro (C #) Digital image processing--2.52 image K-mean clustering

??

[ function name ]

?? Image Kmeans clustering?????? kmeanscluster (writeablebitmapsrc,int.k)

<summary>//Kmeans Cluster process. </summary>//<param name= "src" >the source image.</param>//<param name= "K" >c Luster Threshould, from 2 to 255.</param>//<returns></returns> public static Writeableb                Itmap 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;                Defining grayscale image information storage variables byte[] ImageData = new byte[w * h];                Define the cluster mean store variable (store the mean value of each cluster) double[] Meancluster = new Double[k];                Define a cluster tag variable (mark the current pixel belongs to which class) int[] Markcluster = new int[w * h]; //Define the cluster pixels and storage variables (the sum of the stored values of each class of pixels) double[] Sumcluster = new Double[k];                Defines the cluster pixel statistic variable (the number of pixels stored in each class) int []countcluster = new Int[k];                Define a clustered RGB component storage variable (storing RGB three-component size for each Class) int[] Sumr = new Int[k];                int[] sumg = new Int[k];                int[] Sumb = new Int[k];                temporary variable int sum = 0;                Cyclic control variable bool s = true;                double[] Mjduge = new Double[k];                Double TEMPV = 0;                int cou = 0; Gets the grayscale image information 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;  Initialize cluster mean for (int i = 0; i < K; i++) {Meancluster[i] = I                    * 255.0/(K-1);                        }//Compute cluster attribution 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;                        }//Update cluster mean 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;                    }}//Compute the cluster RGB component 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 #) Digital image processing--2.52 image K-mean clustering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.