-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcutimg.py
56 lines (49 loc) · 1.75 KB
/
cutimg.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 19:03:22 2018
@author: Neoooli
"""
import numpy as np
import os
import glob
from gdaldiy import *
"""
The source file should be organized as follows by running fuseimg.py first:
sourcedir
/10m/file1.tif...filen.tif (4 bands 2/3/4/8)
/20m/file1.tif...filen.tif (6 bands 5/6/7/8A/11/12)
/60m/file1.tif...filen.tif (3 bands 1/9/10)
"""
sourcedir='F:/WHU/WHUS2-CD+/'#source dir
types=["train","test"]
names=['10m','20m','60m']
window_sizes,strides=[384,192,64],[384,192,64]
def cut_data(filetype,name,window_size,stride):
filedir=sourcedir+filetype+"/"+name
print(filedir)
filedirs=glob.glob(os.path.join(filedir, '*'))
for i in range(len(filedirs)):
filepath=filedirs[i]
print(filepath)
savedirname=filepath.split('\\')[-1].split('.tif')[0][33:44]
savedirpath=filedir.replace(filetype,filetype+'DNclips')+"/"+savedirname
if not os.path.exists(savedirpath):
os.makedirs(savedirpath)
img = imgread(filepath)
h,w=img.shape[0],img.shape[1]
h_steps=(w-window_size)//stride+1
w_steps=(h-window_size)//stride+1
n=0
for i in range(h_steps):
high=i*stride
for j in range(w_steps):
width=j*stride
n=n+1
if np.all(img[high:high+window_size,width:width+window_size]>0):
imgwrite(savedirpath+"/"+str(n)+'.tif',img[high:high+window_size,width:width+window_size])
def multi_dir(filetype,name,window_size,stride):
cut_data(filetype,name,window_size,stride)
for i in range(len(types)):
filetype=types[i]
for j in range(len(names)):
multi_dir(filetype,names[j],window_sizes[j],strides[j])