判断LBP值是否为uniform

LBP值是0~255之间的数值,uniform指的是其二进制形式的翻转次数是否超过2次,比如"00001110" 为unifom,而“00110011”不是uniform.


#include 
using namespace std;

//#define _DEBUG_INFO

void printBinary(const unsigned char val)
{
	for(int i = 7; i >= 0; i--){
		if(val & (1 << i))
			cout << "1";
		else
			cout << "0";
	}
}

bool IsUniform(const unsigned char val)
{
	int bit = 0;
	int last_bit = 0;
	int change_time = 0;

	if(val & (1 << 7))
		bit = 1;
	else
		bit = 0;
	last_bit = bit;
	
	for(int i = 6; i >= 0; i--){
		if(val & (1 << i))
			bit = 1;
		else
			bit = 0;

		if(bit != last_bit)
			change_time ++;

		last_bit = bit;
	}

#ifdef _DEBUG_INFO
	printf("change_time = %d\n", change_time);
#endif //_DEBUG_INFO

	if(change_time <= 2)
		return true;
	else
		return false;
}

int uniform_table[256] = {
	1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 
	1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 
	1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
	1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
	1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
	1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1,
};
int main(int argc, char* argv[]){
	int val = atoi(argv[1]);
	printBinary(val);
	cout << endl;
	if(IsUniform(val))
		cout << "Uniform" << endl;
	else
		cout << "Not uniform" << endl;

	printf("uniform_table[%d] = %d\n", val, uniform_table[val]);

	//int uniform_cnt = 0;
	//for(int i = 0; i <= 255; i++){
	//	if(IsUniform(i)){
	//		cout << "1, ";
	//		uniform_cnt ++;
	//	}
	//	else
	//		cout << "0, ";

	//	if((i+1)%16 == 0)
	//		cout << endl;
	//}
	//cout << "\n uniform_cnt = " << uniform_cnt << endl;
}

函数IsUniform判断某LBP值是否为uniform。最后列出了一个uniform_table直接查询某LBP值是否为uniform.

你可能感兴趣的:(C&C++,图像处理)