分享给有需要的人,代码质量勿喷。
private Bitmap xjGetLBP(Bitmap BitmapOld)
{
//原始LBP
int xjWidth = BitmapOld.Width;//宽度
int xjHeight = BitmapOld.Height;//高度
Bitmap xjBitmapLBP = new Bitmap(xjWidth, xjHeight);
for (int width = 0; width < xjWidth; width++)
{
for (int height = 0; height < xjHeight; height++)
{
//中心像素
Color xjCenterColor = BitmapOld.GetPixel(width, height);
byte xjCRed = xjCenterColor.R;
byte xjCGreen = xjCenterColor.G;
byte xjCBlue = xjCenterColor.B;
int xjCenterValue = (xjCRed * 38 + xjCGreen * 75 + xjCBlue * 15) >> 7;
//8邻域
string xjFieldValue = string.Empty;
for (int w = width - 1; w <= width + 1; w++)
{
for (int h = height - 1; h <= height + 1; h++)
{
if ((w == width) && (h == height))
{
continue;
}
int xjValue = -1;
if ((w < 0) || (w >= xjWidth) || (h < 0) || (h >= xjHeight))
{
xjValue = xjCenterValue;
}
else
{
Color xjFieldColor = BitmapOld.GetPixel(w, h);
byte xjRed = xjFieldColor.R;
byte xjGreen = xjFieldColor.G;
byte xjBlue = xjFieldColor.B;
xjValue = (xjRed * 38 + xjGreen * 75 + xjBlue * 15) >> 7;
}
//关键:大小判断,略有改变(小于中心像素值为0,大于等于中心像素值为1)
if (xjValue < xjCenterValue)
{
xjFieldValue += "0";
}
else
{
xjFieldValue += "1";
}
}
}
//LBP二进制
string xjLBPBinary = xjGetLBPBinary(xjFieldValue);
//原始LBP值(十进制)
int xjLbpValue = Convert.ToInt32(xjLBPBinary, 2);
//赋值
xjBitmapLBP.SetPixel(width, height, Color.FromArgb(xjLbpValue, xjLbpValue, xjLbpValue));
}
}
return xjBitmapLBP;
}
/// <summary>
/// 邻域值转为LBP二进制。全图顺序一致即可。
/// </summary>
/// <param name="FieldValue"></param>
/// <returns></returns>
private string xjGetLBPBinary(string FieldValue)
{
string xjOrderValue = string.Empty;
xjOrderValue += FieldValue.Substring(0, 3);
xjOrderValue += FieldValue.Substring(4, 1);
xjOrderValue += FieldValue.Substring(7, 1);
xjOrderValue += FieldValue.Substring(6, 1);
xjOrderValue += FieldValue.Substring(5, 1);
xjOrderValue += FieldValue.Substring(3, 1);
return xjOrderValue;
}
VS2010具体窗体+代码见:点击打开链接