首页 > 其他 > 详细

LBP简单实现

时间:2014-01-31 13:48:19      阅读:392      评论:0      收藏:0      [点我收藏+]

Local Binary Pattern

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

 

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

LBP简单实现

原文:http://www.cnblogs.com/x1957/p/3536677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!