C#:LBP特徵影象(VS2010窗體+程式碼) 阿新 • • 發佈:2019-01-29 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; } //鄰域值轉為LBP二進位制。全圖順序一致即可。 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具體窗體+程式碼見:點選開啟連結