-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.py
executable file
·91 lines (78 loc) · 2.53 KB
/
draft.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
import torch # for speed
import torch.nn.functional as F
import cv2
from tqdm import tqdm
import numpy as np
import pandas as pd
import h5py
# from gen_dense_feature import GaborFilters, make_gabor_conv_weight, gabor_batch
from config import *
import os.path as osp
train_h5 = h5py.File(train_file, 'r')
vali_h5 = h5py.File(val_file, 'r')
label_names = ['LCZ 1: Compact high-rise', 'LCZ 2: Compact mid-rise', 'LCZ 3: Compact low-rise',
'LCZ 4: Open high-rise', 'LCZ 5: Open mid-rise', 'LCZ 6: Open low-rise',
'LCZ 7: Lightweight low-rise', 'LCZ 8: Large low-rise', 'LCZ 9: Sparsely built',
'LCZ 10: Heavy industry', 'LCZ A: Dense trees', 'LCZ B: Scattered trees',
'LCZ C: Bush, scrub', 'LCZ D: Low plants', 'LCZ E: Bare rock or paved',
'LCZ F: Bare soil or sand', 'LCZ G: Water']
def GaborFilters(ksize=None, n_direct=4):
filters = []
if ksize is None:
ksize = [3, 5, 7]
for th_id, theta in enumerate(np.arange(0, np.pi, np.pi / n_direct)): # gabor方向,0°,45°,90°,135°,共四个
filters.append([])
for K in range(len(ksize)):
kern = cv2.getGaborKernel(ksize=(ksize[K], ksize[K]),
sigma=1,
theta=theta,
lambd=5,
gamma=0.1,
psi=0, ktype=cv2.CV_32F)
kern /= 1.5 * kern.sum()
# filters[K].append(kern)
filters[th_id].append(kern)
return filters
def gabor_img(filters, img):
out = []
# img = img.transpose(2,0,1)
print(img.shape)
for ksize_fs in filters:
ksize_out = 0
for f in ksize_fs:
o = cv2.filter2D(np.uint8(img*255), cv2.CV_8UC3, f).astype(float)/255
ksize_out += o
ksize_out /= len(ksize_fs)
out.append(ksize_out[None,:,:,:])
out = np.concatenate(out, axis=0)
return out
filters = GaborFilters(ksize=[5])
def view_gabor_data(datasource, num, dims=None):
if dims is None:
dims = [2, 1, 0]
img = np.array(datasource['sen2'][num, :, :, dims])
import matplotlib.pyplot as plt
out = gabor_img(filters, img)
RGB = np.concatenate([datasource['sen2'][num, :, :, [2]],
datasource['sen2'][num, :, :, [1]],
datasource['sen2'][num, :, :, [0]]], -1)
plt.figure(figsize=(10, 5))
plt.subplot(251)
plt.imshow(out[0]*3)
plt.subplot(252)
plt.imshow(out[1]*3)
plt.subplot(253)
plt.imshow(out[2]*3)
plt.subplot(254)
plt.imshow(out[3]*3)
plt.subplot(255)
plt.imshow(RGB*3)
plt.show()
# for fs in filters:
# for f in fs:
# plt.subplot()
# plt.imshow(f)
# plt.show()
print(label_names[np.argmax(datasource['label'][num])])
if __name__ == '__main__':
view_gabor_data(train_h5, 83, [0,1,2])