【OpenCV】纹理提取

求一幅图的纹理特征图

原图
【OpenCV】纹理提取_第1张图片
(1)转为灰度图

cvtColor(src_img, gray_img, CV_BGR2GRAY);

【OpenCV】纹理提取_第2张图片
(2)获得LBP模板并赋值

【OpenCV】纹理提取_第3张图片
LBP纹理提取代码

/*
CV:LBP
Author:1210
Date:2019/03/17
*/
#include 
#include  
using namespace std;
using namespace cv;
int main()
{
	Mat src_img = imread("E:/CV&AR/Task4/LBP/LBP/LBP/pku.jpg");
	imshow("src 1210", src_img);
	Mat gray_img;
	cvtColor(src_img, gray_img, CV_BGR2GRAY);
	imshow("gray 1210", gray_img);
	Mat dst_img = Mat(gray_img.rows - 2, gray_img.cols - 2, CV_8UC1, Scalar(0));
	uchar temp[8]= { 0 };
	uchar num;
	for (int i = 1; i < gray_img.rows - 1; i++)
	{
		for (int j = 1; j < gray_img.cols - 1; j++)
		{
			if (gray_img.at(i, j) < gray_img.at(i - 1, j - 1))
				temp[7] = 1;
			else temp[7] = 0;
			if (gray_img.at(i, j) < gray_img.at(i - 1, j ))
				temp[6] = 1;
			else temp[6] = 0;
			if (gray_img.at(i, j) < gray_img.at(i - 1, j + 1))
				temp[5] = 1;
			else temp[5] = 0;
			if (gray_img.at(i, j) < gray_img.at(i , j + 1))
				temp[4] = 1;
			else temp[4] = 0;
			if (gray_img.at(i, j) < gray_img.at(i + 1, j + 1))
				temp[3] = 1;
			else temp[3] = 0;
			if (gray_img.at(i, j) < gray_img.at(i + 1 , j ))
				temp[2] = 1;
			else temp[2] = 0;
if (gray_img.at(i, j) < gray_img.at(i + 1, j - 1))
				temp[1] = 1;
			else temp[1] = 0;
			if (gray_img.at(i, j) < gray_img.at(i , j - 1))
				temp[0] = 1;
			else temp[0] = 0;
			num = (temp[7] * 128 + temp[6] * 64 + temp[5] * 32 + temp[4] * 16 + temp[3] * 8 + temp[2] * 4 + temp[1] * 2 + temp[0]);
			dst_img.at(i-1, j-1) = num;
}
	}
	imshow("dst 1210", dst_img);
	waitKey(0);
	return 0;
}


欢迎批评指正!

你可能感兴趣的:(OpenCV)