#include <opencv2/opencv.hpp>
#include <iostream>
#include "math.h" using namespace cv;
using namespace std; Mat src, gray_src; const char* output_tt = "LBP Result"; int main(int argc, char** argv) {
src = imread("test.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -;
} namedWindow("input image", CV_WINDOW_AUTOSIZE);
namedWindow(output_tt, CV_WINDOW_AUTOSIZE);
imshow("input image", src); // convert to gray
cvtColor(src, gray_src, COLOR_BGR2GRAY);
int width = gray_src.cols;
int height = gray_src.rows; // 基本LBP演示
Mat lbpImage = Mat::zeros(gray_src.rows - , gray_src.cols - , CV_8UC1);//3*3窗口,边界有左右各1个像素不处理
for (int row = ; row < height - ; row++) {
for (int col = ; col < width - ; col++) {
uchar c = gray_src.at<uchar>(row, col);//获取中心像素值
uchar code = ;//特征码
code |= (gray_src.at<uchar>(row - , col - ) > c) << ;
code |= (gray_src.at<uchar>(row - , col) > c) << ;
code |= (gray_src.at<uchar>(row - , col + ) > c) << ;
code |= (gray_src.at<uchar>(row, col + ) > c) << ;
code |= (gray_src.at<uchar>(row + , col + ) > c) << ;
code |= (gray_src.at<uchar>(row + , col) > c) << ;
code |= (gray_src.at<uchar>(row + , col - ) > c) << ;
code |= (gray_src.at<uchar>(row, col - ) > c) << ;
lbpImage.at<uchar>(row - , col - ) = code;//原图1,1=效果图0,0
}
}
imshow(output_tt, lbpImage); waitKey();
return ;
}
LBP的扩展(多尺度表达):
#include <opencv2/opencv.hpp>
#include <iostream>
#include "math.h" using namespace cv;
using namespace std; Mat src, gray_src;
int current_radius = ;
int max_count = ; const char* output_tt = "LBP Result"; void ELBP_Demo(int, void*);
int main(int argc, char** argv) {
src = imread("test.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -;
} namedWindow("input image", CV_WINDOW_AUTOSIZE);
namedWindow(output_tt, CV_WINDOW_AUTOSIZE);
imshow("input image", src); // convert to gray
cvtColor(src, gray_src, COLOR_BGR2GRAY);
int width = gray_src.cols;
int height = gray_src.rows; // 基本LBP演示
Mat lbpImage = Mat::zeros(gray_src.rows - , gray_src.cols - , CV_8UC1);//3*3窗口,边界有左右各1个像素不处理
for (int row = ; row < height - ; row++) {
for (int col = ; col < width - ; col++) {
uchar c = gray_src.at<uchar>(row, col);//获取中心像素值
uchar code = ;//特征码
code |= (gray_src.at<uchar>(row - , col - ) > c) << ;
code |= (gray_src.at<uchar>(row - , col) > c) << ;
code |= (gray_src.at<uchar>(row - , col + ) > c) << ;
code |= (gray_src.at<uchar>(row, col + ) > c) << ;
code |= (gray_src.at<uchar>(row + , col + ) > c) << ;
code |= (gray_src.at<uchar>(row + , col) > c) << ;
code |= (gray_src.at<uchar>(row + , col - ) > c) << ;
code |= (gray_src.at<uchar>(row, col - ) > c) << ;
lbpImage.at<uchar>(row - , col - ) = code;
}
}
imshow(output_tt, lbpImage); // ELBP 演示 namedWindow("ELBP Result", CV_WINDOW_AUTOSIZE);
createTrackbar("ELBP Radius:", "ELBP Result", ¤t_radius, max_count, ELBP_Demo);
ELBP_Demo(, ); waitKey();
return ;
} void ELBP_Demo(int, void*) {
int offset = current_radius * ;//输出图无法计算到的边界宽度
Mat elbpImage = Mat::zeros(gray_src.rows - offset, gray_src.cols - offset, CV_8UC1);
int width = gray_src.cols;
int height = gray_src.rows; int numNeighbors = ;//应根据半径变化,为了简便这里固定为8
for (int n = ; n < numNeighbors; n++) {
float x = static_cast<float>(current_radius) * cos(2.0 * CV_PI*n / static_cast<float>(numNeighbors));//x=r*cos(2*PI*n/8)
float y = static_cast<float>(current_radius) * -sin(2.0 * CV_PI*n / static_cast<float>(numNeighbors)); int fx = static_cast<int>(floor(x));//floor“向下取整”
int fy = static_cast<int>(floor(y));
int cx = static_cast<int>(ceil(x));//ceil“向上取整”
int cy = static_cast<int>(ceil(y)); //双线性插值
float ty = y - fy;
float tx = x - fx; //双线性插值,周边4个点的权重
float w1 = ( - tx)*( - ty);
float w2 = tx * ( - ty);
float w3 = ( - tx)* ty;
float w4 = tx * ty; for (int row = current_radius; row < (height - current_radius); row++) {
for (int col = current_radius; col < (width - current_radius); col++) {
float t = w1 * gray_src.at<uchar>(row + fy, col + fx) + w2 * gray_src.at<uchar>(row + fy, col + cx) +
w3 * gray_src.at<uchar>(row + cy, col + fx) + w4 * gray_src.at<uchar>(row + cy, col + cx);
elbpImage.at<uchar>(row - current_radius, col - current_radius) +=
((t > gray_src.at<uchar>(row, col)) && (abs(t - gray_src.at<uchar>(row, col)) > std::numeric_limits<float>::epsilon())) << n;
}
}
}
imshow("ELBP Result", elbpImage);
return;
}