LBP特征提取算法的提取与实现

LBP的基本思想是对图像的像素和它周围8个像素进行比较,然后求和。如果中心像素的亮度大于等于他的相邻像素,把他标记为1,否则标记为0。最终可以获取2^8个可能的组合,称为局部二值模式或LBP码。

这样做的原理:因为对于一张图片,相邻像素之 间的的值是有关系的。因此中心点和相邻像素比较后就能得到一个二进制串,这个二进制串就能很好的表示这个点和相邻像素之间的模式,这个模式就是这个二进制串。 这种描述方法就可以很好的捕捉到图像中的细节。
#include"imgProcess.h"


int main()
{

	IplImage* color_face = cvLoadImage("D://test.png",1);
	IplImage* gray_face = cvCreateImage(cvSize(color_face->width, color_face->height), color_face->depth, 1);
	cvCvtColor(color_face, gray_face, CV_BGR2GRAY);
	IplImage* LBP_face = cvCreateImage(cvSize(color_face->width, color_face->height), color_face->depth, 1);//先分配图像空间  

	cvNamedWindow("Color Image", 1);
	cvShowImage("Color Image", color_face);

	cvNamedWindow("Gray Image", 1);
	cvShowImage("Gray Image", gray_face);
	ImgProcess imgp;
	imgp.src_LBP(gray_face, LBP_face);

	cvNamedWindow("LBP Image", CV_WINDOW_AUTOSIZE);
	cvShowImage("LBP Image", LBP_face);

	waitKey();

	cvReleaseImage(&color_face);
	cvReleaseImage(&gray_face);
	cvReleaseImage(&LBP_face);
	cvDestroyWindow("Color Image");
	cvDestroyWindow("Gray Image");
	cvDestroyWindow("LBP Image");

	return 0;
}


imgProcess.h
#include <opencv2/opencv.hpp>  
#include  <cv.h>   
#include  <highgui.h>  
#include  <cxcore.h>  
using namespace std;
using namespace cv;

class ImgProcess{
public:
	int tmp[8];
	IplImage *temp;
	ImgProcess(){};
	~ImgProcess(){};
	void src_LBP(IplImage *src, IplImage *dst);

};

imgProcess.cpp
#include "imgProcess.h"
void ImgProcess::src_LBP(IplImage *src, IplImage *dst){
	CvScalar s;
	temp = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
	uchar *data = (uchar*)src->imageData;
	int step = src->widthStep;
	tmp[8] = { 0 };
	cout << "step" << step << endl;

	for (int i = 1; i<src->height - 1; i++)
		for (int j = 1; j<src->width - 1; j++)
		{
			int sum = 0;
			if (data[(i - 1)*step + j - 1]>data[i*step + j])
				tmp[0] = 1;
			else
				tmp[0] = 0;
			if (data[i*step + (j - 1)]>data[i*step + j])
				tmp[1] = 1;
			else
				tmp[1] = 0;
			if (data[(i + 1)*step + (j - 1)]>data[i*step + j])
				tmp[2] = 1;
			else
				tmp[2] = 0;
			if (data[(i + 1)*step + j]>data[i*step + j])
				tmp[3] = 1;
			else
				tmp[3] = 0;
			if (data[(i + 1)*step + (j + 1)]>data[i*step + j])
				tmp[4] = 1;
			else
				tmp[4] = 0;
			if (data[i*step + (j + 1)]>data[i*step + j])
				tmp[5] = 1;
			else
				tmp[5] = 0;
			if (data[(i - 1)*step + (j + 1)]>data[i*step + j])
				tmp[6] = 1;
			else
				tmp[6] = 0;
			if (data[(i - 1)*step + j]>data[i*step + j])
				tmp[7] = 1;
			else
				tmp[7] = 0;
			//计算LBP编码  
			s.val[0] = (tmp[0] * 1 + tmp[1] * 2 + tmp[2] * 4 + tmp[3] * 8 + tmp[4] * 16 + tmp[5] * 32 + tmp[6] * 64 + tmp[7] * 128);
			cvSet2D(dst, i, j, s); //写入LBP图像
		}
}


猜你喜欢

转载自blog.csdn.net/wjmishuai/article/details/50400945