-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGET_CLASSES_DEPENDENCE.py
70 lines (61 loc) · 2.41 KB
/
GET_CLASSES_DEPENDENCE.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
import numpy as np
import queue
from skimage import io
GTS_IND_LIST = [3,5,11,13,17,21,23,26,28,30,32,34,37]
MAIN_PATH = "/Users/ekaterina/Documents/Semantic Segmentation/ISPRS_semantic_labeling_Vaihingen"
GTS_FORMAT = "/gts_for_participants/top_mosaic_09cm_area{}.tif"
COV_RES = "/Output/class_dependence.txt"
categories = np.zeros((6, 3), dtype = 'uint8')
categories[0] = [255, 255, 255]
categories[1] = [0, 0, 255]
categories[2] = [0, 255, 255]
categories[3] = [0, 255, 0]
categories[4] = [255, 255, 0]
categories[5] = [255, 0, 0]
def get_gts(num):
label_path = MAIN_PATH + "/gts_for_participants/top_mosaic_09cm_area{}.tif". format(num)
res = io.imread(label_path)
return res
def convert_color_to_labels(color_map):
label_map = np.zeros((color_map.shape[0],color_map.shape[1]), dtype = 'uint8')
for i in range(color_map.shape[0]):
for j in range (color_map.shape[1]):
pics = color_map[i][j][:]
for k in range(6):
if np.array_equal(pics, categories[k, :]):
label_map[i][j] = k
break
return label_map
def bfs(img):
bfs_queue= queue.Queue()
bfs_queue.put((0,0))
visited = np.zeros((img.shape[0],img.shape[1],1), dtype = 'uint64')
while not bfs_queue.empty():
x,y = bfs_queue.get()
if (x>0 and not visited[x-1][y]):
if (img[x,y] != img[x-1,y]):
neighbours[img[x,y], img[x-1,y]] += 1
visited[x-1][y] = 1
bfs_queue.put((x-1,y))
if (y>0 and not visited[x][y-1]):
if (img[x,y-1] != img[x,y]):
neighbours[img[x,y], img[x,y-1]] += 1
visited[x][y-1] = 1
bfs_queue.put((x,y-1))
if ((x!= (img.shape[0] - 1)) and not visited[x+1][y]):
if (img[x+1,y] != img[x,y]):
neighbours[img[x,y], img[x+1,y]] += 1
visited[x+1][y] = 1
bfs_queue.put((x+1,y))
if ((y!= (img.shape[1] - 1)) and not visited[x][y+1]):
if (img[x,y+1] != img[x,y]):
neighbours[img[x,y], img[x,y+1]] += 1
visited[x][y+1] = 1
bfs_queue.put((x,y+1))
return 0
neighbours = np.zeros((6, 6), dtype = 'uint64')
for i in GTS_IND_LIST:
bfs(convert_color_to_labels(get_gts(i)))
neighbours = np.transpose(neighbours) + neighbours
res = np.divide(neighbours, np.sum(neighbours)/200)
np.savetxt(MAIN_PATH + COV_RES, res)