Original: http://www.cnblogs.com/mikewolf2002/p/3438698.html
In this chapter we study the principles and use of LBP images, because next we will use the histogram of LBP images for face recognition.
Resources:
Http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html
Http://www.cnblogs.com/mikewolf2002/p/3438166.html
The basic idea of LBP is to compare the threshold values of neighboring pixels with a pixel in the image as the center. If the luminance of the center pixel is greater than or equal to its neighboring pixels, the neighboring pixels are marked as 1, otherwise marked as 0. We can use binary numbers to represent the LBP encoding for each pixel in the LBP graph, such as the central pixel in the following image, which has a LBP code of: 00010011 and a decimal value of 19.
The formula means:
where (XC,YC) is the center pixel, the IC is the grayscale value, in is the gray value of the neighboring pixels, S is a symbolic function:
In the LBP algorithm of OpenCV, a circular LBP operator is used:
For a point, its nearest neighbor is calculated using the following formula:
where r is the radius and P is the number of sample points.
If the result is not in pixel coordinates, we use bilinear interpolation for approximate processing.
In the following code, we have implemented the LBP diagram, which is usually LBP graph and circular operator respectively.
ELBP is a circular operator LBP function, ELBP1 is usually LBP graph, we respectively to the Lena image is processed, the results are as follows, from the way can be seen, the use of circular operator effect sharpness stronger.
#include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp" #
Include <iostream> #include <fstream> #include <sstream> using namespace CV;
using namespace Std;
void Elbp (mat& src, Mat &dst, int radius, int neighbors) {for (int n=0; n<neighbors; n++) { Calculation of the sampling point float x = static_cast<float> (-radius * sin (2.0*cv_pi*n/static_cast<float> (neighbors)))
;
Float y = static_cast<float> (radius * cos (2.0*cv_pi*n/static_cast<float> (neighbors)));
The value int fx = static_cast<int> (floor (x)) on rounding and rounding down;
int fy = static_cast<int> (Floor (y));
int cx = static_cast<int> (ceil (x));
int cy = static_cast<int> (ceil (y));
Part of the decimal point float ty = y-fy;
float tx = X-FX;
Set interpolation weights float w1 = (1-tx) * (1-ty);
float W2 = tx * (1-ty); float W3 = (1-tx) * TY;
float W4 = tx * TY; Loop processing image data for (int i=radius; i < src.rows-radius;i++) {for (int j=radius;j < src.co ls-radius;j++) {//calculated interpolation float t = static_cast<float> (w1*src.at< Uchar> (I+FY,J+FX) + w2*src.at<uchar> (I+FY,J+CX) + w3*src.at<uchar> (I+CY,J+FX) + W4*src.at<uchar
> (I+CY,J+CX)); Encode dst.at<uchar> (I-radius,j-radius) + = ((T > src.at<uchar> (i,j)) | | (Std::abs (t-src.at<uchar> (i,j)) < Std::numeric_limits<float>::epsilon ()))
<< N; }}}} void Elbp1 (mat& src, Mat &dst) {//Loop processing image data for (int i=1; i < src.rows-1;i++) {for (int j=1;j < src.cols-1;j++) {Uchar
tt = 0;
int tt1 = 0; Uchar U = src.at<uchar>(I,J);
if (src.at<uchar> (i-1,j-1) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i-1,j) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i-1,j+1) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i,j+1) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i+1,j+1) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i+1,j) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i+1,j-1) >u) {tt + = 1 <<tt1;}
tt1++;
if (src.at<uchar> (i-1,j) >u) {tt + = 1 <<tt1;}
tt1++;
Dst.at<uchar> (i-1,j-1) = TT; }}} int main () {Mat img = Cv::imread (".
/lenna.jpg ", 0); Namedwindow ("ImaGE ");
Imshow ("Image", IMG);
int radius, neighbors;
RADIUS = 1;
Neighbors = 8;
Create a LBP//note in order to overflow, our ranks all subtract 2 radius from the original image mat DST = Mat (Img.rows-2*radius, IMG.COLS-2*RADIUS,CV_8UC1, Scalar (0));
ELBP1 (IMG,DST);
Namedwindow ("normal");
Imshow ("normal", DST);
Mat Dst1 = Mat (Img.rows-2*radius, IMG.COLS-2*RADIUS,CV_8UC1, Scalar (0));
ELBP (img,dst1,1,8);
Namedwindow ("Circle");
Imshow ("Circle", Dst1);
while (1) cv::waitkey (0); }
Let's take another picture, which includes four photos under different lights, and then look at the effects of the LBP chart:
Program code:
FirstOpenCV36