LBP

Posted 图米大侠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LBP相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

class LBP_Common{
private:
    Mat image;
    Mat LBP_image;
public:
    LBP_Common(Mat src);
    void LBP_Pixel();   //循环计算每个像素的LBP值;
    void Image_Show();  //图像显示;
    Mat GetImage();     //输出LBP图片;
};

LBP_Common::LBP_Common(Mat src){
    image = src;
}

void LBP_Common::LBP_Pixel(){
    //LBP_image = image;
    int row = image.rows;       //获取图像的行;
    int col = image.cols;      //获取每一行的元素;

    Mat P(row, col, CV_8U, Scalar(255));

    //遍历图中每一个像素,并对像素进行操作;
    for (int i = 1; i < row - 1; i++){
        for (int j = 1; j < col - 1; j++){
            int pixel = 0;
            if (image.at<uchar>(i - 1, j - 1)>image.at<uchar>(i, j)){
                pixel = pixel*2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i-1, j)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }
            
            if (image.at<uchar>(i - 1, j + 1)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i , j + 1)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i + 1, j + 1)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i + 1, j )>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i + 1, j - 1)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }

            if (image.at<uchar>(i , j - 1)>image.at<uchar>(i, j)){
                pixel = pixel *2 + 1;
            }
            else{
                pixel = pixel *2 + 0;
            }
            P.at<uchar>(i, j) = pixel;

        }
    }
    LBP_image = P;
}

void LBP_Common::Image_Show(){
    namedWindow("src_Image", 1);
    imshow("src_Image", image);
    namedWindow("LBP_Image", 1);
    imshow("LBP_Image", LBP_image);
}

Mat LBP_Common::GetImage(){
    return LBP_image;
}

class HistoGram_Image{
private:
    Mat image;
    MatND hist;

    //直方图的相关数据:
    int histSize[1];          //维度数;
    float hranges[2];         //像素的最大值和最小值;
    const float *ranges[1];
    int channels[1];          //单通道;
public:
    HistoGram_Image(Mat src);
    MatND GetHistgram();     //获得直方图数据;
    Mat Draw_Histgram();   //绘制直方图;
};

HistoGram_Image::HistoGram_Image(Mat src){
    image = src;
    histSize[0] = 256;
    hranges[0] = 0.0;
    hranges[1] = 255.0;
    channels[0] = 0;
    ranges[0] = hranges;
}

MatND HistoGram_Image::GetHistgram(){
    MatND Hist;
    calcHist(&image,         //输入图片;
              1,             //计算单张图片的直方图;
              channels,      //通道数;
              Mat(),         //不使用图像作为掩码;
              Hist,          //返回的直方图数据;
              1,             //1维直方图;
              histSize,      //维数的大小;
              ranges         //像素的大小范围;
            );

    return Hist;
}

Mat HistoGram_Image::Draw_Histgram(){
    hist = GetHistgram();
    Mat hist_Image(histSize[0], histSize[0], CV_8U, Scalar(255));
    double MaxVal;
    double MinVal;
    minMaxLoc(hist, &MinVal, &MaxVal, 0, 0);
    int hpt = static_cast<int>(0.9*histSize[0]);   //设置最高点为nbins的90%;
    for (int i = 0; i < histSize[0]; i++){
        float binVal = hist.at<float>(i);
        int intensity = static_cast<int>(binVal*hpt / MaxVal);
        //绘制两点之间的直线;
        line(hist_Image, Point(i,histSize[0]), Point(i,histSize[0] - intensity), Scalar::all(0));
    }
    return hist_Image;

}

int main(){
    Mat src = imread("F:/3.jpg", 1);
    if (src.empty() ){
        cout << "图片输入错误";
        return -1;
    }
    Mat dist;
    Mat hist_image;
    cvtColor(src, dist, COLOR_BGR2GRAY);
    LBP_Common LBP(dist);
    LBP.LBP_Pixel();
    LBP.Image_Show();
    HistoGram_Image Hist(LBP.GetImage());
    hist_image = Hist.Draw_Histgram();
    namedWindow("hist_image", 1);
    imshow("hist_image", hist_image);
    waitKey(0);
    return 0;
}

 

以上是关于LBP的主要内容,如果未能解决你的问题,请参考以下文章

LBP特征提取实现

求高手给一个matlab提取图像lbp的代码 谢谢啦

创建 LBP 直方图

急求灰度图像LBP特征提取matlab源代码

转载:LBP的初步理解

使用HOG+LBP实现动物分类:matlab版本