8.4 局部二值模式LBP

8.4.1 经典的LPB

8.4.2 圆形LBP

 1 #include <opencv2/opencv.hpp>
 2 #include <opencv2/highgui/highgui.hpp>
 3 #include <opencv2/nonfree/features2d.hpp>
 4 #include <opencv2/features2d/features2d.hpp>
 5 using namespace std;
 6 using namespace cv;
 7 // 计算原始LBP特征
 8 cv::Mat OLBP(cv::Mat& srcImage)  
 9 {  
10     const int nRows = srcImage.rows;
11     const int nCols = srcImage.cols;
12     cv::Mat resultMat(srcImage.size(), srcImage.type());
13     // 遍历图像,生成LBP特征
14     for(int y = 1; y < nRows-1; y++)  
15     {  
16         for(int x = 1; x < nCols-1; x++)  
17         {  
18             // 定义邻域
19             uchar neighbor[8] = {0};  
20             neighbor[0] = srcImage.at<uchar>(y-1, x-1);  
21             neighbor[1] = srcImage.at<uchar>(y-1, x);  
22             neighbor[2] = srcImage.at<uchar>(y-1, x+1);  
23             neighbor[3] = srcImage.at<uchar>(y, x+1);  
24             neighbor[4] = srcImage.at<uchar>(y+1, x+1);  
25             neighbor[5] = srcImage.at<uchar>(y+1, x);  
26             neighbor[6] = srcImage.at<uchar>(y+1, x-1);  
27             neighbor[7] = srcImage.at<uchar>(y, x-1); 
28             // 当前图像的处理中心 
29             uchar center = srcImage.at<uchar>(y, x);  
30             uchar temp = 0;  
31             // 计算LBP的值 
32             for(int k = 0; k < 8; k++)  
33             {  
34                 // 遍历中心点邻域
35                 temp += (neighbor[k] >= center)* (1<<k);   
36             }  
37             resultMat.at<uchar>(y,x) = temp;   
38         }  
39     }
40     return resultMat;  
41 }  
42 int main()  
43 {  
44     cv::Mat srcImage = cv::imread("hand1.jpg",0);  
45     if(!srcImage.data)   
46         return 0;  
47     Mat resultMat = OLBP(srcImage);
48     cv::imshow("srcImage", srcImage);   
49     cv::imshow("resultMat", resultMat);  
50     cv::waitKey(0);  
51     return 0;  
52 } 
原文地址:https://www.cnblogs.com/thebreakofdawn/p/9635156.html