对轮廓逆时针排序,并写入txt

对轮廓逆时针排序,并写入txt,第1张

对轮廓逆时针排序,并写入txt
#include 
#include 
#include 
#include 
#include 
#include
#include
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("C:/Users/jl/Desktop/chunge/20210526_154432_425_R.JPG", 1);
	vector x;
	vector y;
	Mat img_gray, img_threshold;
	Mat MedianBlurImg;
	Mat changed;
	vectorhang;
	vectorlie;
	cvtColor(src, img_gray, COLOR_BGR2GRAY, 3);//opencv默认图片为BGR,进行阈值分割前需转为灰度图
	threshold(img_gray, img_threshold, 200, 255, THRESH_BINARY);//阈值分割,阈值大小225
	medianBlur(img_threshold, MedianBlurImg, 5);
	int i, j;
	for (i = 3; i < MedianBlurImg.rows - 5; i++)
	{
		for (j = 3; j < MedianBlurImg.cols - 5; j++)
		{
			int num = 0;
			int a = MedianBlurImg.at(i, j);
			int a1 = MedianBlurImg.at(i - 1, j - 1);
			int a2 = MedianBlurImg.at(i, j - 1);
			int a3 = MedianBlurImg.at(i + 1, j - 1);
			int a4 = MedianBlurImg.at(i - 1, j);
			int a5 = MedianBlurImg.at(i + 1, j);
			int a6 = MedianBlurImg.at(i - 1, j + 1);
			int a7 = MedianBlurImg.at(i, j + 1);
			int a8 = MedianBlurImg.at(i + 1, j + 1);
			if (a > 150)
			{
				if (a1 < 100 || a2 < 100 || a3 < 100 || a4 < 100 || a5 < 100 || a6 < 100 || a7 < 100 || a8 < 100)
				{
					src.at(i, j)[0] = 0;
					src.at(i, j)[1] = 0;
					src.at(i, j)[2] = 255;
					hang.push_back(i);
					lie.push_back(j);	
				}
			}
		}
	}
	int x0 = 1;
	int y0 = 1;
	int B1, G1, R1;
	int B2, G2, R2;
	int B3, G3, R3;
	int B4, G4, R4;
	int B5, G5, R5;
	int B6, G6, R6;
	int B7, G7, R7;
	int B8, G8, R8;
	int B9, G9, R9;
	int B10, G10, R10;
	int B11, G11, R11;
	int B12, G12, R12;
	int B13, G13, R13;
	int B14, G14, R14;
	int B15, G15, R15; 
	int B16, G16, R16;
	int B17, G17, R17;
	int B18, G18, R18;
	int B19, G19, R19;
	int B20, G20, R20;
	int B21, G21, R21;
	int B22, G22, R22;
	int B23, G23, R23;
	int B24, G24, R24;
	int Ba, Ga, Ra;
	int num_max = 0;
	for (int i = 50; i < src.rows - 50; i++)
	{
		for (int j = 50; j < src.cols - 50; j++)
		{
			
			if (src.at(i, j)[0] == 0 && src.at(i, j)[1] == 0 && src.at(i, j)[2] == 255)
			{
				num_max = num_max + 1;
			}
		}
	}


	for (int index = 0; index < hang.size(); index++)
	{
		if (src.at(hang[index] - 1, lie[index])[2] == 255 && src.at(hang[index], lie[index] + 1)[2] == 255)
		{
			src.at(hang[index], lie[index])[0] = 0;
			src.at(hang[index], lie[index])[1] = 255;
			src.at(hang[index], lie[index])[2] = 0;
		}
		else if (src.at(hang[index] , lie[index]+1)[2] == 255 && src.at(hang[index]+1, lie[index] )[2] == 255)
		{
			src.at(hang[index], lie[index])[0] = 0;
			src.at(hang[index], lie[index])[1] =255;
			src.at(hang[index], lie[index])[2] = 0;
		}
		else if (src.at(hang[index], lie[index] - 1)[2] == 255 && src.at(hang[index] + 1, lie[index])[2] == 255)
		{
			src.at(hang[index], lie[index])[0] = 0;
			src.at(hang[index], lie[index])[1] = 255;
			src.at(hang[index], lie[index])[2] = 0;
		}
		else if (src.at(hang[index], lie[index] - 1)[2] == 255 && src.at(hang[index] -1, lie[index])[2] == 255)
		{
			src.at(hang[index], lie[index])[0] = 0;
			src.at(hang[index], lie[index])[1] = 255;
			src.at(hang[index], lie[index])[2] = 0;
		}

	}
	cout << "num_max=" << num_max << endl;

	imshow("src", src);
	changed = src.clone();
	imwrite("C:/Users/jl/Desktop/tiqu/1.png", src);
	int num0 = 0;
	for (int i = 150; i < src.rows - 150; i++)
	{
		if (num0 != 0)
		{
			break;
		}
		for (int j = 150; j < src.cols - 150; j++)
		{
			if (src.at(i, j)[0] == 0 && src.at(i, j)[1] == 0 && src.at(i, j)[2] == 255)
			{
				x0 = i;
				y0 = j;
				cout << "x0=" << x0 << "   " << "y0=" << y0 << endl;
				num0 = 1;
				break;
			}
		}
	}
	cout << "x0=" << x0 << "   " << "y0=" << y0 << endl;

	ofstream outfile("C:/Users/jl/Desktop/tiqu/tiqu2.txt");
	

	for (int num = 0; num <= num_max; num++)
	{

		B1 = changed.at(x0 - 1, y0)[0];
		G1 = changed.at(x0 - 1, y0)[1];
		R1 = changed.at(x0 - 1, y0)[2];

		B2 = changed.at(x0 - 1, y0 + 1)[0];
		G2 = changed.at(x0 - 1, y0 + 1)[1];
		R2 = changed.at(x0 - 1, y0 + 1)[2];

		B3 = changed.at(x0, y0 + 1)[0];
		G3 = changed.at(x0, y0 + 1)[1];
		R3 = changed.at(x0, y0 + 1)[2];

		B4 = changed.at(x0 + 1, y0 + 1)[0];
		G4 = changed.at(x0 + 1, y0 + 1)[1];
		R4 = changed.at(x0 + 1, y0 + 1)[2];

		B5 = changed.at(x0 + 1, y0)[0];
		G5 = changed.at(x0 + 1, y0)[1];
		R5 = changed.at(x0 + 1, y0)[2];

		B6 = changed.at(x0 + 1, y0 - 1)[0];
		G6 = changed.at(x0 + 1, y0 - 1)[1];
		R6 = changed.at(x0 + 1, y0 - 1)[2];

		B7 = changed.at(x0, y0 - 1)[0];
		G7 = changed.at(x0, y0 - 1)[1];
		R7 = changed.at(x0, y0 - 1)[2];

		B8 = changed.at(x0 - 1, y0 - 1)[0];
		G8 = changed.at(x0 - 1, y0 - 1)[1];
		R8 = changed.at(x0 - 1, y0 - 1)[2];

		B9 = changed.at(x0 - 2, y0)[0];
		G9 = changed.at(x0 - 2, y0)[1];
		R9 = changed.at(x0 - 2, y0)[2];

		B10 = changed.at(x0 - 2, y0 + 1)[0];
		G10 = changed.at(x0 - 2, y0 + 1)[1];
		R10 = changed.at(x0 - 2, y0 + 1)[2];

		B11 = changed.at(x0 - 2, y0 +2)[0];
		G11 = changed.at(x0 - 2, y0 + 2)[1];
		R11 = changed.at(x0 - 2, y0 + 2)[2];

		B12 = changed.at(x0 - 1, y0 +2)[0];
		G12 = changed.at(x0 - 1, y0 + 2)[1];
		R12 = changed.at(x0 - 1, y0 + 2)[2];

		B13 = changed.at(x0 , y0 +2)[0];
		G13 = changed.at(x0, y0 + 2)[1];
		R13 = changed.at(x0, y0 + 2)[2];

		B14 = changed.at(x0 + 1, y0 +2)[0];
		G14 = changed.at(x0 + 1, y0 + 2)[1];
		R14 = changed.at(x0 + 1, y0 + 2)[2];

		B15 = changed.at(x0 +2, y0 +2)[0];
		G15 = changed.at(x0 + 2, y0 + 2)[1];
		R15 = changed.at(x0 + 2, y0 + 2)[2];

		B16 = changed.at(x0 +2, y0 +1)[0];
		G16 = changed.at(x0 + 2, y0 + 1)[1];
		R16 = changed.at(x0 + 2, y0 + 1)[2];

		B17 = changed.at(x0 +2, y0 )[0];
		G17 = changed.at(x0 + 2, y0)[1];
		R17 = changed.at(x0 + 2, y0)[2];

		B18 = changed.at(x0 +2, y0 - 1)[0];
		G18 = changed.at(x0 + 2, y0 - 1)[1];
		R18 = changed.at(x0 + 2, y0 - 1)[2];

		B19 = changed.at(x0 +2, y0 - 2)[0];
		G19 = changed.at(x0 + 2, y0 - 2)[1];
		R19 = changed.at(x0 + 2, y0 - 2)[2];

		B20 = changed.at(x0 +1, y0 - 2)[0];
		G20 = changed.at(x0 + 1, y0 - 2)[1];
		R20 = changed.at(x0 + 1, y0 - 2)[2];

		B21 = changed.at(x0 , y0 - 2)[0];
		G21 = changed.at(x0, y0 - 2)[1];
		R21 = changed.at(x0, y0 - 2)[2];

		B22 = changed.at(x0 - 1, y0 - 2)[0];
		G22 = changed.at(x0 - 1, y0 - 2)[1];
		R22 = changed.at(x0 - 1, y0 - 2)[2];

		B23 = changed.at(x0 - 2, y0 - 2)[0];
		G23= changed.at(x0 - 2, y0 - 2)[1];
		R23 = changed.at(x0 - 2, y0 - 2)[2];

		B24 = changed.at(x0 - 2, y0 - 1)[0];
		G24 = changed.at(x0 - 2, y0 - 1)[1];
		R24 = changed.at(x0 - 2, y0 - 1)[2];

		Ba = changed.at(x0, y0)[0];
		Ga = changed.at(x0, y0)[1];
		Ra = changed.at(x0, y0)[2];
		if (num == 0)
		{
			x.push_back(x0);
			y.push_back(y0);
			if (B1 == 0 && R1 == 255 && G1 == 0)
			{
				x0 = x0 - 1;
				y0 = y0;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B2 == 0 && R2 == 255 && G2 == 0)
			{
				x0 = x0 - 1;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B3 == 0 && R3 == 255 && G3 == 0)
			{
				x0 = x0;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B4 == 0 && R4 == 255 && G4 == 0)
			{
				x0 = x0 + 1;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else
			{
				cout << "无法向右遍历" << endl;
			}
			x.push_back(x0);
			y.push_back(y0);
		}
		else
		{
			if (B1 == 0 && R1 == 255 && G1 == 0)
			{
				x0 = x0 - 1;
				y0 = y0;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 1 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B2 == 0 && R2 == 255 && G2 == 0)
			{
				x0 = x0 - 1;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 2 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B3 == 0 && R3 == 255 && G3 == 0)
			{		
				x0 = x0;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 3 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;	
			}
			else if (B4 == 0 && G4 == 0 && R4 == 255)
			{	
				x0 = x0 + 1;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 4 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;	
			}
			else if (B5 == 0 && R5 == 255 && G5 == 0)
			{
				x0 = x0 + 1;
				y0 = y0;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 5 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B6 == 0 && R6 == 255 && G6 == 0)
			{
				x0 = x0 + 1;
				y0 = y0 - 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 6 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B7 == 0 && R7 == 255 && G7 == 0)
			{
				x0 = x0;
				y0 = y0 - 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 7 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B8 == 0 && R8 == 255 && G8 == 0)
			{
				x0 = x0 - 1;
				y0 = y0 - 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 8 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B9 == 0 && R9 == 255 && G9 == 0)
			{
				x0 = x0 - 2;
				y0 = y0 ;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 9 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B10 == 0 && R10 == 255 && G10 == 0)
			{
				x0 = x0 - 2;
				y0 = y0 +1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 10 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B11 == 0 && R11 == 255 && G11 == 0)
			{
				x0 = x0 - 2;
				y0 = y0 +2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 11 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B12 == 0 && R12 == 255 && G12 == 0)
			{
				x0 = x0 - 1;
				y0 = y0 +1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 12 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B13 == 0 && R13 == 255 && G13 == 0)
			{
				x0 = x0 ;
				y0 = y0 +2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 13 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B14 == 0 && R14 == 255 && G14 == 0)
			{
				x0 = x0+1;
				y0 = y0 + 2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 14 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B15 == 0 && R15 == 255 && G15 == 0)
			{
				x0 = x0+2;
				y0 = y0 + 2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 15 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B16 == 0 && R16 == 255 && G16 == 0)
			{
				x0 = x0+2;
				y0 = y0 + 1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 16 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B17 == 0 && R17 == 255 && G17 == 0)
			{
				x0 = x0+2;
				y0 = y0 ;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 17 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B18 == 0 && R18 == 255 && G18 == 0)
			{
				x0 = x0+2;
				y0 = y0 -1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 18" << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B19 == 0 && R19 == 255 && G19 == 0)
			{
				x0 = x0+2;
				y0 = y0 -2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 19" << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B20 == 0 && R20 == 255 && G20 == 0)
			{
				x0 = x0+1;
				y0 = y0 -2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 20 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B21 == 0 && R21 == 255 && G21 == 0)
			{
				x0 = x0;
				y0 = y0 -2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 21 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B22 == 0 && R22 == 255 && G22 == 0)
			{
				x0 = x0-1;
				y0 = y0 -2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 22 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B23 == 0 && R23 == 255 && G23 == 0)
			{
				x0 = x0-2;
				y0 = y0 -2;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 23 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			else if (B24 == 0 && R24 == 255 && G24 == 0)
			{
				x0 = x0-2;
				y0 = y0 -1;
				changed.at(x0, y0)[2] = 0;
				cout << x0 << " 24 " << y0 << endl;
				outfile << x0 << " " << y0 << endl;
			}
			
			
			x.push_back(x0);
			y.push_back(y0);
		}
	}
	outfile.close();
	namedWindow("changed", 0);
	imshow("changed", changed);
	imwrite("C:/Users/jl/Desktop/tiqu/new2.png", changed);
	waitKey(0);
	return 0;
}

本文内容仅供个人或实验室内部使用,不做过多解释

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5671038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存