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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/vivien1997/article/details/102723430
#include "StdAfx.h"

#include <opencv2/opencv.hpp>
#include  <opencv/highgui.h>
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<uchar>(i, j) < gray_img.at<uchar>(i - 1, j - 1))
				temp[7] = 1;
			else temp[7] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i - 1, j ))
				temp[6] = 1;
			else temp[6] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i - 1, j + 1))
				temp[5] = 1;
			else temp[5] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i , j + 1))
				temp[4] = 1;
			else temp[4] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i + 1, j + 1))
				temp[3] = 1;
			else temp[3] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i + 1 , j ))
				temp[2] = 1;
			else temp[2] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(i + 1, j - 1))
				temp[1] = 1;
			else temp[1] = 0;
			if (gray_img.at<uchar>(i, j) < gray_img.at<uchar>(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<uchar>(i-1, j-1) = num;
		}
	}
	imshow("dst 1210", dst_img);
	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/vivien1997/article/details/102723430