C#:LBP特征图像(VS2010窗体+代码)

1 篇文章 0 订阅

        

     

图2 LBP特征图像

分享给有需要的人,代码质量勿喷。

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具体窗体+代码见:点击打开链接

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

累了就要打游戏

把我养胖,搞代码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值