Original: Win8 Metro (C #) Digital image processing--2.55ostu image binary method
[ function name ]
Ostu method image binary writeablebitmap ostuthsegment (writeablebitmap src)
[ function Code ]
<summary>//Ostu method of Image segmention. </summary>//<param name= "src" >the source image.</param>//<returns></ret urns> public static WriteableBitmap ostuthsegment (WriteableBitmap src)////ostu method threshold split {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 (); Defining grayscale image information storage variables int[] Srcdata = new int[w * h]; Defining a threshold variable int Th = 0;; Defines the number of background and target pixels variable n1,n2, grayscale variable u1,u2, grayscale and variable sum1,sum2, temporary cache variable Temp int N1 = 0, N2 = 0, Sum1 = 0, Sum2 = 0; Define background and target pixel scale variable w1,w2, image overall average gray variable u, variance variable g, contrast threshold variable TT double W1 = 0, W2 = 0, U1 = 0, U2 = 0, g = 0, TT = 0; for (int j = 0; J < H; j + +) {for (int i = 0; i < W; i++) {srcdata[i + j * W] = (int) (double) Tempmask[i * 4 + J * W * 4] * 0.114 + (double) tempmask[i * 4 + 1 + J * W * 4] * 0.587 + (double) tempmask[i * 4 + 2 + J * W * 4] * 0.299); }}//Find the maximum inter-class variance for (int T = 0; T <= 255; t++) {for (int i = 0; i < srcdata.length; i++) { if (Srcdata[i] > T) {n2++; Sum2 + = Srcdata[i]; } else {n1++; Sum1 + = Srcdata[i]; }} W1 = (double) (N1/(N1 + N2)); W2 = (double) (1.0-W1); U1 = (N1 = = 0? 0.0: (SUM1/N1)); U2 = (N2 = = 0? 0.0: (SUM2/N2)); g = N1 * N2 * (U1-U2) * (U1-U2); if (g > TT) {tt = g; Th = T; } N1 = 0; N2 = 0; SUM1 = 0; Sum2 = 0; W1 = 0.0; W2 = 0.0; U1 = 0.0; U2 = 0.0; g = 0.0; } for (int j = 0; J < H; j + +) {for (int i = 0; i < W; i++) {Temp[i * 4 + J * W * 4] = temp[i * 4 + 1 + J * W * 4] = temp[i * 4 + 2 + J * W * 4] = (byte) (Srcdata[i + J * W] < Th 0:255); }} 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.55ostu image binary method