-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path重新预测的mask每张提取shp.py
129 lines (99 loc) · 4.43 KB
/
重新预测的mask每张提取shp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
import glob
import json
import os
import cv2
import gdal
import numpy as np
import tqdm
# 每张mask影像中提取边界,转换到大影像坐标系再转换到经纬度
def imagexy2geo(dataset, row, col):
'''
根据GDAL的六参数模型将影像图上坐标(行列号)转为投影坐标或地理坐标(根据具体数据的坐标系统转换)
:param dataset: GDAL地理数据
:param row: 像素的行号
:param col: 像素的列号
:return: 行列号(row, col)对应的投影坐标或地理坐标(x, y)
'''
trans = dataset.GetGeoTransform()
px = trans[0] + row * trans[1] + col * trans[2]
py = trans[3] + row * trans[4] + col * trans[5]
return px, py
# 获取影像边界
def raster_boarder(im_geotrans, im_width, im_height):
raster_x_min = im_geotrans[0]
raster_x_max = raster_x_min + im_width * im_geotrans[1]
raster_y_max = im_geotrans[3]
raster_y_min = raster_y_max + im_height * im_geotrans[5]
# raster_x_min+=1024*im_geotrans[1]
# raster_x_max-=1024*im_geotrans[1]
# raster_y_max+=1024*im_geotrans[5]
# raster_y_min-=1024*im_geotrans[5]
return raster_x_min, raster_x_max, raster_y_min, raster_y_max
if __name__ == '__main__':
original_tifs = [r"J:\shanxibianpo\road_buffer.tif"]
# pred_jsons = [r"J:\shanxibianpo\bianpo_predv2.geojson"]
pred_jsons = [r"J:\shanxibianpo\buffer_predict_bianpo_predv.geojson"]
# mask_pngs = [r"J:\shanxibianpo\Val\shanxibianpo1024__*_*_yanshou.png"]
mask_pngs = [r"J:\shanxibianpo\imagebuffer_Val\shanxibianpo1024overlap__*_*.png"]
for j in range(len(original_tifs)):
result = []
num = 0
in_ds = gdal.Open(original_tifs[j])
im_width = in_ds.RasterXSize # 栅格矩阵的列数
im_height = in_ds.RasterYSize # 栅格矩阵的行数
# dst_ds = gdal.GetDriverByName('GTiff').Create(pred_tifs[j], im_width, im_height, 1, gdal.GDT_Byte)
# dst_ds.SetGeoTransform(in_ds.GetGeoTransform())
# dst_ds.SetProjection(in_ds.GetProjection())
masks = glob.glob(mask_pngs[j])
# masks = []
for i, m in enumerate(tqdm.tqdm(masks)):
name = os.path.split(m)[1][:-4]
name = name.split('_')
x0 = int(name[2])
y0 = int(name[3])
img = cv2.imread(m, cv2.IMREAD_GRAYSCALE)
if (np.sum(img) < 1):
continue
img = cv2.resize(img, (1024, 1024), interpolation=cv2.INTER_NEAREST)
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# cv2.imshow('ori',img)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
img = cv2.erode(img, kernel) # 腐蚀图像
img = cv2.erode(img, kernel) # 腐蚀图像
# cv2.imshow('erode',img)
# img = cv2.dilate(img, kernel) # 膨胀图像
img = cv2.dilate(img, kernel) # 膨胀图像
if (np.sum(img) < 1):
continue
# print m
contours, hierarch = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for c in contours:
rect = {}
rect['type'] = "Feature"
points = c.reshape([-1, 2]).astype(np.float)
points[:, 0] += y0
points[:, 1] += x0
points = np.asarray(imagexy2geo(in_ds, points[:, 0], points[:, 1]))
# points[:, 0] = points[:, 0] * x_delta + x0
# points[:, 1] = points[:, 1] * y_delta + y0
points = points.T
points = points.tolist()
points = [points]
rect["geometry"] = {}
rect["geometry"]["type"] = 'Polygon'
rect["geometry"]["coordinates"] = points
rect["properties"] = {}
rect['properties']['index'] = num
rect["properties"]["disaster_type"] = 'huapo'
num += 1
result.append(rect)
result1 = {}
result1['type'] = 'FeatureCollection'
result1['name'] = 'duxiang_polygonV1'
result1["crs"] = {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}
result1['features'] = result
with open(pred_jsons[j], 'w') as f:
json.dump(result1, f)