OpenCV学习笔记_图像细化

#include
#include
#include

using namespace cv;

bool thinImage_first(Mat inputImage, Mat& outputImage){
	bool retValue = false;
	int count = 0;
	outputImage = inputImage.clone();

	uchar p1, p2, p3, p4, p5, p6, p7, p8, p9;
	
	for (int row = 1; row < inputImage.rows - 1; row++){

		uchar* ptr_0 = inputImage.ptr(row - 1);
		uchar* ptr_1 = inputImage.ptr(row);
		uchar* ptr_2 = inputImage.ptr(row + 1);
		uchar* ptr_dst = outputImage.ptr(row);

		for (int col = 1; col < inputImage.cols - 1; col++){
			p1 = ptr_1[col];
			if (p1 != 1){
				continue;
			}
			else{
				p2 = ptr_0[col];
				p3 = ptr_0[col + 1];
				p4 = ptr_1[col + 1];
				p5 = ptr_2[col + 1];
				p6 = ptr_2[col];
				p7 = ptr_2[col - 1];
				p8 = ptr_1[col - 1];
				p9 = ptr_0[col - 1];
				int temp = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
				if (temp >= 2 && temp <= 6){
					int S = 0;
					if (p2 == 0 && p3 == 1)
						S++;
					if (p3 == 0 && p4 == 1)
						S++;
					if (p4 == 0 && p5 == 1)
						S++;
					if (p5 == 0 && p6 == 1)
						S++;
					if (p6 == 0 && p7 == 1)
						S++;
					if (p7 == 0 && p8 == 1)
						S++;
					if (p8 == 0 && p9 == 1)
						S++;
					if (p9 == 0 && p2 == 1)
						S++;
					if (S == 1 && (p2 * p4 * p6) == 0 && (p4 * p6 * p8) == 0){
						ptr_dst[col] = 0;
						count++;
					}
				}
			}
		}
	}

	if (count > 0){
		retValue = true;
	}
	return retValue;
}

bool thinImage_second(Mat inputImage, Mat& outputImage){
	bool retValue = false;
	int count = 0;
	outputImage = inputImage.clone();

	uchar p1, p2, p3, p4, p5, p6, p7, p8, p9;

	for (int row = 1; row < inputImage.rows - 1; row++){

		uchar* ptr_0 = inputImage.ptr(row - 1);
		uchar* ptr_1 = inputImage.ptr(row);
		uchar* ptr_2 = inputImage.ptr(row + 1);
		uchar* ptr_dst = outputImage.ptr(row);

		for (int col = 1; col < inputImage.cols - 1; col++){
			p1 = ptr_1[col];
			if (p1 != 1){
				continue;
			}
			else{
				p2 = ptr_0[col];
				p3 = ptr_0[col + 1];
				p4 = ptr_1[col + 1];
				p5 = ptr_2[col + 1];
				p6 = ptr_2[col];
				p7 = ptr_2[col - 1];
				p8 = ptr_1[col - 1];
				p9 = ptr_0[col - 1];
				int temp = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
				if (temp >= 2 && temp <= 6){
					int S = 0;
					if (p2 == 0 && p3 == 1)
						S++;
					if (p3 == 0 && p4 == 1)
						S++;
					if (p4 == 0 && p5 == 1)
						S++;
					if (p5 == 0 && p6 == 1)
						S++;
					if (p6 == 0 && p7 == 1)
						S++;
					if (p7 == 0 && p8 == 1)
						S++;
					if (p8 == 0 && p9 == 1)
						S++;
					if (p9 == 0 && p2 == 1)
						S++;
					if (S == 1 && (p2 * p4 * p8) == 0 && (p2 * p6 * p8) == 0){
						ptr_dst[col] = 0;
						count++;
					}
				}
			}
		}
	}

	if (count > 0){
		retValue = true;
	}
	return retValue;
}


int main(void){
	Mat src = imread("A.png", CV_LOAD_IMAGE_GRAYSCALE);
	Mat input;
	cv::threshold(src, input, 128, 1, cv::THRESH_BINARY);

	Mat out1, out2;
	while (true){
		bool retFirst = thinImage_first(input, out1);
		if (retFirst == false){
			out2 = out1;
			break;
		}

		bool retSecond = thinImage_second(out1, out2);
		if (retSecond == false){
			break;
		}
		else{
			input = out2;
		}	
	}
	out2 *= 255;
	imshow("out2", out2);
	waitKey(0);
	
	return 0;
}

你可能感兴趣的:(OpenCV学习)