Win8 Metro(C#)数字图像处理--2.59 P分位法图像二值化
[函数名称]
P分位法图像二值化
[算法说明]
所谓P分位法图像分割,就是在知道图像中目标所占的比率Ratio时,循环不同的灰度值对图像进行
分割,并计算对应的目标所占的比率,如果该比率与Ratio的差值足够小,那么该阈值就是所求的最
佳分割阈值。
/// <summary>
/// P-Parameter method of image segmention.
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="P">The ratio of object, from 0 to 1.</param>
/// <returns></returns>
public static WriteableBitmap PParameterThSegment(WriteableBitmap src,double P) ////P参数法阈值分割
{
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[] srcData = new int[w * h];
//定义背景和目标像素个数变量
int C1 = 0, C2 = 0;
//定义阈值变量
int Th = 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);
}
}
for (int T = 0; T <= 255; T++)
{
for (int i = 0; i < srcData.Length; i++)
{
if (srcData[i] > T)
{
C1++;
}
else
{
C2++;
}
}
double t = Math.Abs((double)((double)C1 / ((double)C1 + (double)C2)) - P);
if (t < 0.01)
{
Th = T;
break;
}
C1 = 0;
C2 = 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;
}
}