由端点找出部分线段+单通道融合为RGB影像

# include<stdio.h>
# include<stdlib.h>
# include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
	Mat source = imread("C:\\Users\\93970\\Desktop\\马萨诸塞州影像成果\\本文算法成果挑选\\深度学习结果\\细化\\细化5.png", 0);
	Mat road_location = Mat::zeros(source.size(), CV_8UC1);
	for (int i = 0; i < source.rows; i++)
	{
		for (int j = 0; j < source.cols; j++)
		{
			if (source.ptr<uchar>(i)[j] > 0)
				road_location.ptr<uchar>(i)[j] = 1;//道路部分为1;
		}
	}
	int neighbor_x[8] = { -1,-1,-1,0,0,1,1,1 };
	int neighbor_y[8] = { -1,0,1,-1,1,-1,0,1 };
	vector<CvPoint> extrem_points;
	//寻找非影像边界端点,边界范围控制在4
	for (int i = 4; i < road_location.rows-4; i++)
	{
		for (int j = 4; j < road_location.cols-4; j++)
		{
			if (road_location.ptr<uchar>(i)[j] == 1)
			{
				int neighbor_num = 0;
				for (int k = 0; k < 8; k++)
				{
					CvPoint site = CvPoint(i + neighbor_x[k], j + neighbor_y[k]);
					if (road_location.ptr<uchar>(site.x)[site.y] == 1)
						++neighbor_num;
				}
				if (neighbor_num == 1)
				{
					extrem_points.push_back(CvPoint(i,j));//非影像边界端点

				}
			}
		}
	}
	for (auto &element : extrem_points)
		road_location.ptr<uchar>(element.x)[element.y] = 3;//将端点设置为3
	
	//创建含有端点的线段:线段终点为八邻域大于3的或者长度大于7的为
	vector<vector<CvPoint>> extrem_lines;
	CvPoint temp;

	for (auto &element : extrem_points)
	{
		vector<CvPoint> grow_buff;
		int line_length = 1;
		grow_buff.push_back(CvPoint(element.x, element.y));
		int flag = 0;
		for (int z = 0; z < grow_buff.size(); z++)//******重要步骤****
		{
			for (int q = 0; q < 8; q++)
			{
				temp = CvPoint(grow_buff[z].x + neighbor_x[q], grow_buff[z].y + neighbor_y[q]);
				if (road_location.ptr<uchar>(temp.x)[temp.y] == 1)
				{
					road_location.ptr<uchar>(temp.x)[temp.y] = 2; //使用过的点标记为2;
					grow_buff.push_back(temp);
					line_length++;
					
					if (line_length ==8)//当长度等于8时,加入集合
					{
						extrem_lines.push_back(grow_buff);
						flag = 1;
					}
					if (flag == 1)
					{
						flag = 2;
						break;
					}
				}
			}
			if (flag == 2)
				break;
			
		}
		
		
}

	
	
	//将单通道影像改为3通道
	Mat three = Mat::zeros(source.size(), CV_8UC3);
	vector<Mat> sigle;
	for (int i = 0; i < 3; i++)
	{
		sigle.push_back(source);
	}
	merge(sigle, three);
	imwrite("three.png", three);
	//展示提取结果
	for (auto &element : extrem_lines)
	{
		for (int i = 0; i < element.size(); i++)
		{
			three.ptr<Vec3b>(element[i].x)[element[i].y] = { 0,0,255 };
		}
	}
		
	
	imwrite("extrem_line.png", three);
	

return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘟嘟love佳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值