binary image of Ostu method
[Algorithm description]
Ostu method, also known as the maximal inter-class variance method, is a commonly used image segmentation algorithm. The basic algorithm idea is to divide the image into two classes according to the initial threshold, then calculate the variance between the two classes, update the threshold value, recalculate the inter-class variance, and when the threshold of the variance between the classes is met, the optimal threshold value is obtained, the specific process is as follows:
1, initialize a threshold of th, the image f (x, y) is divided into a, b two categories;
2, calculate the mean UA and UB for both A and B pixel sets, respectively, with the following formula:
Of these, NA and NB represent the number of pixels in the set, A and B respectively.
3, calculate the inter-class variance of A, B, the formula is as follows:
4, the th from 0 to 255 cycles, calculate the inter-class variance of A, B, when the variance between the maximum, the corresponding th is the optimal segmentation or the threshold value of the binary.
[function Code]
<summary>
Ostu method of Image segmention.
</summary>
<param name= "src" >the source image.</param>
<returns></returns>
public static WriteableBitmap ostuthsegment (WriteableBitmap src)////ostu method threshold Segmentation
{
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 Store variables
int[] Srcdata = new int[w * h];
Defining threshold variables
int Th = 0;;
Defines the number of background and target pixels variable n1,n2, grayscale variable u1,u2, grayscale and variable sum1,sum2, TEMP cache variable
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);
}
}
Finding 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;
}
}
[Image effect]
Fig.1 original fig.2 effect Diagram
binary image interpolation with iterative method
[Algorithm description]
The idea of iterative image binary algorithm is: First, initialize a threshold th, and then follow a strategy to constantly update the threshold value by iteration, until the given constraints are met, the steps are as follows:
1, for an image, assume that the current pixel is f (x, y), set a threshold th, according to the current threshold, loop f (x, y), the image is divided into two groups of pixels, A, b;
2, calculate the pixel mean UA and UB for A/b set, respectively, as follows:
of these, NA and NB represent the number of pixels in the set, A and B respectively.
3, UPDATE threshold:
4, determine whether the current calculated threshold value and the last calculated threshold value is satisfied with the constraints, that is, two times the threshold difference is less than a constrained value T, if less than the current threshold of th is the best threshold value, otherwise, go to step 2.
[function Code]
<summary>
Iterative method of image segmention.
</summary>
<param name= "src" >the source image.</param>
<returns></returns>
public static WriteableBitmap iterativethsegment (writeablebitmap src)////iterative threshold segmentation
{
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 Store variables
int[] Srcdata = new int[w * h];
Defines the number of background and target pixels variable c1,c2, the overall grayscale and the variable sum
int C1 = 0, C2 = 0, sum = 0;
Define the background and target of gray and variable g1,g2, and before and after two grayscale mean variables t0,t
Double G1 = 0, G2 = 0, t0 = n, t = 0;
Defining threshold variables
int Th = 0;
Defining loop control Variables
bool S = true;
for (int j=0;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);
Sum + = Srcdata[i + J * W];
}
}
&nbs