openCV与C++的图像识别(四)——纹理图输出

#include "StdAfx.h"

#include 
#include  
using namespace std;
using namespace cv;
int main()
{
	Mat src_img = imread("F:/www/person.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;
}

你可能感兴趣的:(C,汇总)