OpenCV - calculation method of basic LBP

1. Algorithm principle

1. Principle overview

  LBP is the abbreviation of Local Binary Pattern (Local Binary Pattern), proposed by TimoOjala et al. at the University of Oulu, Finland. It is an operator that describes the local texture characteristics of an image based on grayscale. The basic idea is to use the relationship between the center value and the neighborhood value to quantify and express the local texture features of the image.
This operator is generated based on the analysis of various texture operators. Compared with other operators of the same category, LBP has the following advantages: (1) The
principle is profound, easy to understand, and easy to expand and improve;
(2) Grayscale The robustness of the scale is good, that is, the influence of light is weak;
(3) The calculation is convenient, and the calculation is performed in the local neighborhood, which provides the possibility for real-time analysis of images in complex situations.
  Based on the above advantages, LBP is widely used in the fields of image processing and pattern recognition, such as texture classification, face or expression recognition, object detection such as pedestrians and cars, behavior discrimination, and remote sensing image classification.
  The basic LBP takes 3 × 3 3\times33×The 3 grid is the window, and there are nine pixels in the window, and the value is the gray value. The threshold value of the center pixel is compared with the value of the neighboring pixels. If the value of the neighboring pixel is greater than or equal to the value of the central pixel, the threshold value of the pixel is 1, otherwise it is 0. The resulting 8-bit binary number is converted to a decimal number. The LBP value of the center cell. The calculation formula of neighborhood threshold is as follows:
si = { 1 , pi − pc ≥ 0 0 , pi − pc < 0 (1) s_i= \begin{cases} 1,\quad p_i-p_c\geq 0\\ 0, \quad p_i-p_c<0 \end{cases} \tag{1}si={ 1,pipc00,pipc<0( 1 )
  wheresi s_isiis the threshold value of neighboring pixels in the window, pc p_cpcis the gray value of the center pixel, pt p_tptis the gray value of the neighboring pixels.
The calculation process of the LBP value is shown in Figure 1. The center pixel value is 6, arranged in a clockwise direction, starting from the upper left corner, the neighborhood pixel values ​​are 6, 5, 2, 1, 7, 3, 9, 7, and The domain thresholds are 1, 0, 0, 0, 1, 0, 1, 1 respectively, the binary value of LBP is 110110001, and the LBP value of the center pixel is 209 after converting to decimal.
insert image description here

LBP value calculation

2. References

[1] Ma Xinjiang. Road boundary extraction in vehicle laser point cloud based on multivariate LBP features [D]. Shandong University of Science and Technology, 2019.

2. Code implementation

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

using namespace std;
// 基本LBP的计算方法
cv::Mat LocalBinaryPattern(cv::Mat& orignImg)
{
    
    
	cv::Mat grayImg;
	cvtColor(orignImg, grayImg, cv::COLOR_BGR2GRAY);

	int width = orignImg.cols - 2;
	int hight = orignImg.rows - 2;

	cv::Mat lbpImg = cv::Mat::zeros(hight, width, CV_8UC1);
	for (int row = 1; row < orignImg.rows - 1; row++)
	{
    
    
		for (int col = 1; col < orignImg.cols - 1; col++)
		{
    
    
			uchar c = grayImg.at<uchar>(row, col);
			uchar code = 0;
			code |= (grayImg.at<uchar>(row - 1, col - 1) > c) << 7;
			code |= (grayImg.at<uchar>(row - 1, col) > c) << 6;
			code |= (grayImg.at<uchar>(row - 1, col + 1) > c) << 5;
			code |= (grayImg.at<uchar>(row, col + 1) > c) << 4;
			code |= (grayImg.at<uchar>(row + 1, col + 1) > c) << 3;
			code |= (grayImg.at<uchar>(row + 1, col) > c) << 2;
			code |= (grayImg.at<uchar>(row + 1, col - 1) > c) << 1;
			code |= (grayImg.at<uchar>(row, col) > c) << 0;
			lbpImg.at<uchar>(row - 1, col - 1) = code;

		}
	}
	return lbpImg;
};


int main(int argc, char** argv)
{
    
    
	cv::Mat img = cv::imread("luna.png");
	//cv::Mat img;
	//resize(img, img, cv::Size(800, 500), 0, 0, cv::INTER_AREA);
	if (img.empty())
	{
    
    
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	imshow("img", img);
	// 基本LBP
	cv::Mat lbpImg = LocalBinaryPattern(img);

	imshow("LBP", lbpImg);
	cv::waitKey(0);

	return 0;

}

3. Results display

  It can be seen that the LBP operator can better preserve details such as building outlines, eyes, noses, mouths, and hat feathers.

insert image description here

Guess you like

Origin blog.csdn.net/qq_36686437/article/details/130980985