LBP + SVM Face recognition based on OpenCV

Source: Internet
Author: User
Tags abs assert pow svm

In this paper, the extraction of LBP features of the face, using the round operator of LBP, through the identification of the samples in the ORL92112 face database, according to statistics, the training set and test sets of the accuracy rate reached 100%;

The image after LBP processing is shown in the following illustration:

As shown in the above illustration, the left image is the original image, the right image is the extracted LBP image, and the LBP circular operator can be used to describe the facial feature very clearly.

Therefore, the LBP operator can be used to extract and recognize face features, and in the process of processing, the image is not affected by illumination, rotation, angle and other factors;

The processing code for the algorithm is described as follows:

LBP Algorithm header file:

Head.h:

#ifndef _head_h
#define _HEAD_H

#include <opencv2/opencv.hpp>
#include <iostream>
# Include <cassert>
#include <vector>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>

#endif		//_head_h
LBP.h

#ifndef _lbp_h
#define _LBP_H

#include "head.h"

typedef std::vector<double> vecdouble;
typedef std::vector<int> Vecint;
typedef std::vector<float> VECFLOAT;

Class LBP
{public
:
	LBP ();
	~LBP ();

Public:
	
	enum Lbp_type
	{
		lbp_uniform = 0,		//uniform LBP
		lbp_normal = 1,			//LBP
		Lbp_ CIRCLE = 2			//circular operator of LBP
	};

Public:
	//traditional LBP algorithm
	void callbp (iplimage* srcimg, iplimage* dstimg);
	LBP round operator algorithm
	void calcirlbp (iplimage* srcimg, iplimage* dstimg, int radius, int neighbor);
	Uniform LBP algorithm
	void caluniformlbp (iplimage* src, iplimage* DST);

Public:
	//Set radius
	void Setradius (int radius) {M_radius = radius;}
	Set realm number
	void setneighbors (int neighbor) {M_neighbor = neighbor;}
	Compute eigenvalue
	void callbpfeatures (iplimage* srcimg, iplimage* dstimg, vecdouble& hist, Lbp_type lbptype);

Private:
	int m_radius;
	int m_neighbor;

};

#endif  //_lbp_h

LBP.cpp

#include "stdafx.h" #include "LBP.h" #include <iostream> using namespace std; LBP::LBP (): M_radius (0), M_neighbor (0) {} lbp::~lbp () {}//LBP algorithm based on old version OpenCV//3 x 3 matrix as shown below//[1, 2, 3]//[8, ij,4]//[7, 6, 5] void lbp::callbp (iplimage* srcimg, iplimage* dstimg) {//The original image is a single channel image, the target image is a single channel image assert
	(srcimg!= NULL && srcimg->nchannels = 1);

	ASSERT (dstimg!= NULL && dstimg->nchannels = 1);

	unsigned tmp[8] = {0}; Traversal image for (int rows = 1; rows < srcimg->height-1; ++rows) {for (int cols = 1; cols < srcimg->width- 1;
			++cols) {int sum = 0;
			
			Double Center = cvgetreal2d (srcimg, rows, cols);
			Traverse double val = 0.0 clockwise;
			upper left corner val = cvgetreal2d (srcimg, rows-1, cols-1); Val > Center?
			Tmp[0] = 1:tmp[0] = 0;
			Just above val = cvgetreal2d (srcimg, rows, cols-1); Val > Center?
			TMP[1] = 1:tmp[1] = 0;
			upper right corner val = cvgetreal2d (srcimg, rows + 1, cols-1); Val > CenTer?
			TMP[2] = 1:tmp[2] = 0;
			Right val = cvgetreal2d (srcimg, rows + 1, cols); Val > Center?
			TMP[3] = 1:tmp[3] = 0;
			Lower right corner val = cvgetreal2d (srcimg, rows + 1, cols + 1); Val > Center?
			TMP[4] = 1:tmp[4] = 0;
			Below val = cvgetreal2d (srcimg, rows, cols + 1); Val > Center?
			TMP[5] = 1:tmp[5] = 0;
			Lower left corner val = cvgetreal2d (srcimg, rows-1, cols + 1); Val > Center?
			TMP[6] = 1:tmp[6] = 0;
			Left val = cvgetreal2d (srcimg, rows-1, cols); Val > Center?

			TMP[7] = 1:tmp[7] = 0;
			Calculates LBP encoding for (int i = 0; i < 8; ++i) {sum + = tmp[i] * POW (2, i);
		} cvsetreal2d (dstimg, rows, cols, sum); }//End for}//uniform LBP, consistent local two value mode void LBP::CALUNIFORMLBP (Iplimage *src, Iplimage *dst) {assert (src!= NULL &A
	mp;& Src->nchannels = = 1);

	ASSERT (DST!= NULL && dst->nchannels = 1);

	int tmp[8] = {0};
	int rows = src->height-1;

	int cols = src->width-1; for (int i = 1; I &Lt Rows
			++i) {for (int j = 1; j < cols; ++j) {int sum = 0;

			Double Center = cvgetreal2d (SRC, I, j);
			Traverse double val = 0.0 clockwise;		val = cvgetreal2d (src, i-1, j-1); Upper left corner val > center?
			Tmp[0] = 1:tmp[0] = 0;			val = cvgetreal2d (src, I, j-1); Just above val > center?
			TMP[1] = 1:tmp[1] = 0;		val = cvgetreal2d (src, i + 1, j-1); upper right corner val > center?
			TMP[2] = 1:tmp[2] = 0;			val = cvgetreal2d (src, i + 1, j); Right val > Center?
			TMP[3] = 1:tmp[3] = 0;		val = cvgetreal2d (src, i + 1, j + 1); Right bottom corner val > Center?
			TMP[4] = 1:tmp[4] = 0;			val = cvgetreal2d (src, I, j + 1); Right below Val > center?
			TMP[5] = 1:tmp[5] = 0;		val = cvgetreal2d (src, i-1, j + 1); Lower left corner val > center?
			TMP[6] = 1:tmp[6] = 0;			val = cvgetreal2d (src, i-1, j); Left Val > Center?

			TMP[7] = 1:tmp[7] = 0; Calculate 0, 1 flip times for (int k = 0; k < 8; k++) {if (K!= 7) {sum = ABS (Tmp[k]-tMp[k + 1]);
				else {sum = ABS (Tmp[k]-tmp[0]);  To determine the specific eigenvalue if (sum <= 2) {for (int i = 0; i < 8; ++i) {sum = tmp[i] * POW (2, 7
				-i));
		} else {sum = 5;///will not be satisfied with the 5} cvset2d (DST, I, J, Cvscalar (sum)); }}//round LBP operator void lbp::calcirlbp (iplimage* src, iplimage* dst, int radius, int neighbors) {//processed image as single channel image Asser

	T (src->nchannels = 1); for (int i = 0; i < neighbors ++i) {//sine radians double Sradian = sin (2.0 * cv_pi * i/static_cast<double> (nei
		ghbors));

		Cosine radians Double Cradian = cos (2.0 * cv_pi * i/static_cast<double> (neighbors));
		Compute the sampling point double x = static_cast<double> (-radius * Sradian);

		Double y = static_cast<double> (RADIUS * Cradian);
		Integer value int fx = static_cast<int> (floor (x));
		int fy = static_cast<int> (Floor (y));
		Integer value int cx = static_cast<int> (ceil (x));
		int cy = static_cast<int> (ceil (y));Decimal part Double tx = X-FX;

		Double ty = y-fy;
		Set interpolation weights Double w1 = (1-TX) * (1-ty);
		Double W2 = tx * (1-ty);
		Double W3 = (1-tx) * TY;


		Double W4 = tx * TY; Looping image data for (int rows = radius; rows < src->height-radius; ++rows) {for (int cols = radius; cols < src->width-radius;
				++cols) {//compute interpolation Double t1 = W1 * CVGETREAL2D (src, rows + fy, cols + FX);
				Double t2 = W2 * cvgetreal2d (src, rows + fy, cols + cx);
				double t3 = W3 * CVGETREAL2D (SRC, rows + cy, cols + FX);
				double T4 = W4 * CVGETREAL2D (SRC, rows + cy, cols + cx);

				Double t = t1 + t2 + t3 + T4;
				Double val = cvgetreal2d (src, rows, cols);

				Double epsilon = Std::numeric_limits<double>::epsilon ();
				Uchar C = ((T > val) | | ABS (T-VAL) < epsilon);
				UCHAR tmp = c * POW (2, i);
				Double v = cvgetreal2d (DST, Rows-radius, Cols-radius);
				V + = tmp;
			cvsetreal2d (DST, Rows-radius, Cols-radius, V); Calculate LPB Specialeigenvalues void Lbp::callbpfeatures (iplimage* srcimg, iplimage* dstimg, vecdouble& hist, Lbp_type Lbptype) {assert (srcImg!=
	NULL && srcimg->nchannels = 1);

	ASSERT (dstimg!= NULL && dstimg->nchannels = 1);
	Calculates LBP image if (Lbptype = = Lbp_normal) {CALLBP (srcimg, dstimg);
	else if (Lbptype = = Lbp_uniform) {CALUNIFORMLBP (srcimg, dstimg); else if (Lbptype = = lbp_circle) {try {if (M_radius = 0 | | m_neighbor = = 0) {Throw "please call SE
			Tradius () and Setneighbor () to set values. ";}
			else {CALCIRLBP (srcimg, dstimg, M_radius, M_neighbor);
		The catch (char* e) {cout << e << Endl;
	}//End If//computed histogram vecint VEC;
	
	Vec.resize (256); for (int i = 0, i < dstimg->width; ++i) {for (int j = 0; J < dstimg->height; ++j) {cvscalar s = Cvge
			T2D (Dstimg, J, i);
			int val = s.val[0];
		++vec[val]; }//end to//histogram normalized int maxval = *max_element (Vec.begin (), Vec.end ());
	int minval = *min_element (Vec.begin (), Vec.end ());
		for (int i = 0; i < vec.size (); ++i) {double tmp = 0.0;
		TMP = static_cast<double> (Vec[i])/static_cast<double> (maxval);
	Hist.push_back (TMP); }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.