ULBP(uniform LBP) c++代码

<span style="font-size:18px;">#include<iostream>
#include<stdio.h>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<algorithm>
#include<fstream>

#include<opencv.hpp>
#include<opencv.hpp>
using namespace cv;
using namespace std;
void LBP(IplImage* src, IplImage* dst);
void UniformPatternLBP(IplImage* src, IplImage* dst);
int getHopCount(uchar i);
int main()
{
	IplImage* img = cvLoadImage("E://6.jpg");
	IplImage* imggray = cvCreateImage(cvGetSize(img), 8, 1);
	cvCvtColor(img, imggray, CV_BGR2GRAY);
	IplImage* dst = cvCreateImage(cvSize(img->width,img->height),8,1);
	/*LBP(imggray, dst);*/
	UniformPatternLBP(imggray, dst);
	cvNamedWindow("shiyan");
	cvShowImage("shiyan", dst);
	waitKey(0);
	cvReleaseImage(&dst);
	system("pause");
	return 0;
}

//void LBP(IplImage* src, IplImage* dst){
//	int width = src->width;
//	int height = src->height;
//	for (int j = 1; j < width; j++)
//	{
//		for (int i = 1; i < height; i++)
//		{
//			uchar neighborhood[8] = { 0 };
//			neighborhood[7] = CV_IMAGE_ELEM(src, uchar, i - 1, j - 1);
//			neighborhood[6] = CV_IMAGE_ELEM(src, uchar, i - 1, j);
//			neighborhood[5] = CV_IMAGE_ELEM(src, uchar, i - 1, j + 1);
//			neighborhood[4] = CV_IMAGE_ELEM(src, uchar, i, j - 1);
//			neighborhood[3] = CV_IMAGE_ELEM(src, uchar, i, j + 1);
//			neighborhood[2] = CV_IMAGE_ELEM(src, uchar, i + 1, j - 1);
//			neighborhood[1] = CV_IMAGE_ELEM(src, uchar, i + 1, j);
//			neighborhood[0] = CV_IMAGE_ELEM(src, uchar, i + 1, j + 1);
//			uchar center = CV_IMAGE_ELEM(src, uchar, i, j);
//			uchar temp = 0;
//
//			for (int k = 0; k<8; k++){
//				temp += (neighborhood[k]>center)*(1 << k);
//			}
//			CV_IMAGE_ELEM(dst, uchar, i, j) = temp;
//		}
//	}
//}
void UniformPatternLBP(IplImage* src, IplImage* dst) {
	int width = src->width;
	int height = src->height;
	uchar table[256];
	memset(table, 0, 256);
	uchar temp = 1;
	ofstream file("E://data1.txt",ios::out);
	for (int i = 0; i<256; ++i)
	{
		if (getHopCount(i) <= 2) {
			table[i] = temp;
			temp++;
		}
		file<<(int)table[i] <<' ';
	}
	for (int j = 1; j < width; j++)
	{
		for (int i = 1; i < height; i++)
		{
			uchar neighborhood[8] = { 0 };
			neighborhood[7] = CV_IMAGE_ELEM(src, uchar, i - 1, j - 1);
			neighborhood[6] = CV_IMAGE_ELEM(src, uchar, i - 1, j);
			neighborhood[5] = CV_IMAGE_ELEM(src, uchar, i - 1, j + 1);
			neighborhood[4] = CV_IMAGE_ELEM(src, uchar, i, j + 1);
			neighborhood[3] = CV_IMAGE_ELEM(src, uchar, i + 1, j + 1);
			neighborhood[2] = CV_IMAGE_ELEM(src, uchar, i + 1, j);
			neighborhood[1] = CV_IMAGE_ELEM(src, uchar, i + 1, j - 1);
			neighborhood[0] = CV_IMAGE_ELEM(src, uchar, i, j - 1);

			uchar center = CV_IMAGE_ELEM(src, uchar, i, j);
			uchar temp = 0;

			for (int k = 0; k<8; k++){
				temp += (neighborhood[k]>center)*(1 << k);
			}
			CV_IMAGE_ELEM(dst, uchar, i, j) = table[temp];
			/*cout << (int)CV_IMAGE_ELEM(src, uchar, i, j) << ' ';*/
		}
		cout << endl;
	}
}
int getHopCount(uchar i){
	int a[8] = { 0 };
	int k = 7;
	int cnt = 0;
	while (i){
		a[k] = i & 1;
		i >>= 1;
		--k;
	}
	for (int k = 0; k<8; ++k){
		if (a[k] != a[k + 1 == 8 ? 0 : k + 1]) {
			++cnt;
		}
	}
	return cnt;
}</span>

你可能感兴趣的:(ULBP(uniform LBP) c++代码)