当前位置:网站首页>从mask-rcnn到shp
从mask-rcnn到shp
2022-08-11 05:33:00 【我是无名的我】
最近做一个图斑识别的项目,要从mask转到shp文件,花了几周时间整理了以下代码,从shape转json的coco数据集代码以后再整理。
代码
# -*- coding: utf-8 -*-
""" Created on Mon Jan 20 16:15:37 2020 @author: hgh """
import geopandas as gp
from shapely.geometry import Polygon
import numpy as np
from math import sqrt, pow
from Douglas_Peuker import DouglasPeuker
points = np.load(r'E:\Project\npy\Blocks.npy')
global edge
''' 判断边 '''
def is_edge(temp, point):
locat = [[0, -1], [1, 0], [0, 1], [-1, 0]]
count = 0
for xy in locat:
check_point = [point[0] + xy[0], point[1] + xy[1]]
is_ourer = np.where((temp[:,0]==check_point[0]) & (temp[:,1]==check_point[1]))[0]
count += len(is_ourer)
return count
''' 得到边后,对边逆时针排序 '''
def rank(point):
# global edge
check = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]
for x, y in check:
check_x = point[0] + x
check_y = point[1] + y
check_point = [check_x, check_y]
if check_point in edge:
return check_point
return False
def create_shp(points):
global edge
# global edge_list
# global d
geometry = list()
# for i in range(points.shape[0]):
for i in range(1):
temp = np.argwhere(points[i]==True)
temp = np.c_[np.argwhere(points[i]==True), np.zeros(len(np.argwhere(points[i]==True)))]
count = 0
for point in temp:
if is_edge(temp, point) != 4:
temp[count, 2] = 1
count +=1
t = temp[temp[:, 2]==1]
edge = [[x, y] for x, y, z in t]
h = len(edge)
edge.sort()
edge_list = [edge[0],]
next_point = edge[0]
edge.remove(edge[0])
try:
for j in range(h):
next_point = rank(next_point)
if next_point in edge:
edge_list.append(next_point)
net_point = net_point
edge.remove(net_point)
if False in edge_list:
edge_list.remove(False)
d = DouglasPeuker()
d.main(edge_list)
edge_list = [point for point in d.qualify_list]
edge_list = Polygon(edge_list)
geometry.append(edge_list)
print('完成第%s个' % str(i+1))
except:
print('错误')
gdf = gp.GeoDataFrame(geometry=geometry)
return gdf
gdf = create_shp(points)
问题解释
- 创建shp文件用到了geopandas,安装这库的时候最好新建一个新的环境,要不然各种问题(反正我的是).
- 我把mask-rcnn得到的点存在了Blocks.npy里面。
- 一个点,判断是否为边界,主要是判断它的上下左右是否都在mask里面,这个函数是运算时间最大的。
- 对边界的排序,对点 ( x , y ) (x, y) (x,y)(最低点),依次判断 ( a + x , b + y ) (a+x, b+y) (a+x,b+y)for x, y in ([[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]] ),若 ( a + 0 , b − 1 ) (a+0,b-1) (a+0,b−1)在边界上面,将 ( a + 0 , b − 1 ) (a+0,b-1) (a+0,b−1)标记为 ( a , b ) (a,b) (a,b)的next_point,将判断位置转移到 ( a + 0 , b − 1 ) (a+0,b-1) (a+0,b−1)。
- 通过三的步骤,mask显然是有很多点的,这时候我们要对排序后的点进行抽稀,用到道格拉斯抽稀算法这里的代码。
思考
按道理说,直接使用步骤3的算法,能在不取出边界的条件下生成有顺序的边界,但是可能是我技术的问题(我菜),老是有问题,以后有空再研究这个。
边栏推荐
- 中小微企业需要使用SSL证书吗?
- Es common operations and classical case
- Project Notes - Take Notes
- Solve the problem that port 8080 is occupied
- uboot code analysis 1: find the main line according to the purpose
- Deep Learning Matlab Toolbox Code Comments
- slurm集群搭建
- AUTOMATION DAY06( Ansible进阶 、 Ansible Role)
- C语言两百题(0基础持续更新)(1~5)
- CLUSTER DAY04(块存储应用案例 、 分布式文件系统 、 对象存储)
猜你喜欢
随机推荐
Vulnhub靶机--DC7
SECURITY DAY01(监控概述 、 Zabbix基础 、 Zabbix监控服 )
SSL证书为什么要选付费?
无胁科技-TVD每日漏洞情报-2022-7-28
mysql数据库安装教程(超级超级详细)
解决8080端口被占用问题
CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )
(1) Software testing theory (0 basic understanding of basic knowledge)
无胁科技-TVD每日漏洞情报-2022-8-1
CLUSTER DAY01(集群及LVS简介 、 LVS-NAT集群 、 LVS-DR集群)
xx is not recognized as internal or external command
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
无胁科技-TVD每日漏洞情报-2022-7-19
window7开启远程桌面功能
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-22
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-31
SSH服务详解
ETCD集群故障应急恢复-本地数据可用
TCP 三次握手、四次断开
mongo-express 远程代码执行漏洞复现