LBP简单实现

Local Binary Pattern

确实够简单。。。先写个代码在这儿,空了再弄

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>

using namespace std;
using namespace cv;
void LBP(const Mat& src , Mat& dst) {
    int rows =src.rows;
    int cols = src.cols;
    
    auto expand = [&](int x , int y) {
        int dx[] = {-1,-1,-1,0,1,1,1,0};
        int dy[] = {-1,0,1,1,1,0,-1,-1};
        vector<pair<int,int> > result(8);
        for(int i = 0 ; i < 8 ; ++i) 
            result[i] = make_pair(x+dx[i] , y+dy[i]);
        return result;
    };
    auto judge = [&](int x1 , int y1 , int x2 , int y2) {
        return src.at<unsigned char> (x1 , y1) >= src.at<unsigned char>(x2, y2) ? 1 : 0;
    };
    
    //split the block
    //parallelism
    for(int i = 1 ; i < rows - 1 ; ++i) {
        for(int j = 1 ; j < cols - 1 ; ++j) {
            auto near = expand(i , j);
            int lbp = 0;
            int pos = 0;
            for(auto& location : near) {
                lbp += judge(location.first , location.second , i , j) * (1<<pos);
                ++pos;
            }
            dst.at<unsigned char>(i-1 , j-1) = lbp;
        }
    }
}
int main() {
    Mat imgr = imread("lenna.png", CV_LOAD_IMAGE_COLOR);
    Mat img;
    cvtColor(imgr,img,CV_RGB2GRAY);
    Mat out(img);
    LBP(img , out);
    imwrite("image_lbp.png", out);
    return 0;
}
原文地址:https://www.cnblogs.com/x1957/p/3536677.html