-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess.py
64 lines (48 loc) · 1.93 KB
/
preprocess.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
## Example: A simple example to obtain boundary map
import numpy as np
import os
import cv2
from osgeo import gdal
import scipy.ndimage as sn
def read_img(filename):
dataset=gdal.Open(filename)
im_width = dataset.RasterXSize
im_height = dataset.RasterYSize
im_geotrans = dataset.GetGeoTransform()
im_proj = dataset.GetProjection()
im_data = dataset.ReadAsArray(0,0,im_width,im_height)
del dataset
return im_proj, im_geotrans, im_width, im_height, im_data
def write_img(filename, im_proj, im_geotrans, im_data):
if 'int8' in im_data.dtype.name:
datatype = gdal.GDT_Byte
elif 'int16' in im_data.dtype.name:
datatype = gdal.GDT_UInt16
else:
datatype = gdal.GDT_Float32
if len(im_data.shape) == 3:
im_bands, im_height, im_width = im_data.shape
else:
im_bands, (im_height, im_width) = 1,im_data.shape
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(filename, im_width, im_height, im_bands, datatype)
dataset.SetGeoTransform(im_geotrans)
dataset.SetProjection(im_proj)
if im_bands == 1:
dataset.GetRasterBand(1).WriteArray(im_data)
else:
for i in range(im_bands):
dataset.GetRasterBand(i+1).WriteArray(im_data[i])
del dataset
maskRoot = r"E:\new_parcel_model\new_big_model\SD\test\mask"
boundaryRoot = r"E:\new_parcel_model\new_big_model\SD\test\contour"
for imgPath in os.listdir(maskRoot):
input_path = os.path.join(maskRoot, imgPath)
boundaryOutPath = os.path.join(boundaryRoot, imgPath)
im_proj, im_geotrans, im_width, im_height, im_data = read_img(input_path)
# im_data = im_data.astype(np.uint8)
###boundary(you can also use bwperim function in Matlab to obtain boundary map)
boundary = cv2.Canny(im_data, 50, 200)
kernel = np.ones((3, 3), np.uint8)
boundary = cv2.dilate(boundary, kernel, iterations=1)
write_img(boundaryOutPath, im_proj, im_geotrans, boundary)