LBP特征处理(一)
LBP特征原理:
这里相当于将周边像素相对于中心像素阈值二值化,后编成八位二进制码处理
1 #include <opencv2/opencv.hpp> 2 #include <iostream> 3 #include "math.h" 4 5 using namespace cv; 6 using namespace std; 7 8 Mat src, gray_src; 9 10 int main(int argc, char** argv) { 11 src = imread("L:/opencv_picture/13.jpg"); 12 if (src.empty()) { 13 printf("could not load image...\n"); 14 return -1; 15 } 16 const char* output_tt = "LBP Result"; 17 namedWindow("input image", CV_WINDOW_AUTOSIZE); 18 namedWindow(output_tt, CV_WINDOW_AUTOSIZE); 19 imshow("input image", src); 20 21 // convert to gray 灰度图像 22 cvtColor(src, gray_src, COLOR_BGR2GRAY); 23 int width = gray_src.cols; 24 int height = gray_src.rows; 25 26 // 基本LBP演示 27 //大框架里采用3×3的滑框,所以上下左右都要减一行出来所以变成 row-2 ,cols-2 28 Mat lbpImage = Mat::zeros(gray_src.rows - 2, gray_src.cols - 2, CV_8UC1); 29 for (int row = 1; row < height - 1; row++) { 30 for (int col = 1; col < width - 1; col++) { 31 uchar c = gray_src.at<uchar>(row, col); 32 uchar code = 0; 33 code |= (gray_src.at<uchar>(row - 1, col - 1) > c) << 7;
// gray_src.at() 的像素值与阈值c比较,大于为1,小于等于为0 34 // a |= b 按位取或以后把值赋给a 35 // a |= b << 7 将b向左位移7位以后,将a与b按位取或以后把值赋给a 36 code |= (gray_src.at<uchar>(row - 1, col) > c) << 6; 37 code |= (gray_src.at<uchar>(row - 1, col + 1) > c) << 5; 38 code |= (gray_src.at<uchar>(row, col + 1) > c) << 4; 39 code |= (gray_src.at<uchar>(row + 1, col + 1) > c) << 3; 40 code |= (gray_src.at<uchar>(row + 1, col) > c) << 2; 41 code |= (gray_src.at<uchar>(row + 1, col - 1) > c) << 1; 42 code |= (gray_src.at<uchar>(row, col - 1) > c) << 0; 43 lbpImage.at<uchar>(row - 1, col - 1) = code; 44 //code为8位的二进制数,相当于LBP图像周边像素加权后的像素值 45 } 46 } 47 imshow(output_tt, lbpImage); 48 49 waitKey(0); 50 return 0; 51 }
实验结果
原图像: LBP处理: