-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLIC_merge_superpixels.py
163 lines (123 loc) · 6.13 KB
/
SLIC_merge_superpixels.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
================================
SLIC superpixel
--------------------------------
input
|- $image/videofile
|- $stepsize
|- $M
output
=================================
ogaki@iis.u-tokyo.ac.jp
2012/09/07
'''
import sys
sys.path.insert(0,'/mnt/scratch/third-party-packages/libopencv_3.1.0/lib/python')
import cv2
import scipy
import scipy.linalg
import random
import math
import os.path
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import glob
import cPickle
from datetime import datetime
import collections
import copy
from PIL import Image
def find_unique_labels(current_superpixel_data):
# find unique vectors (number of dimensions equals number of layers)
current_superpixel_data_channel_info_all = np.array([x[2:] for x in current_superpixel_data[0]])
current_superpixel_data_channel_info_all_transformed = np.ascontiguousarray(
current_superpixel_data_channel_info_all).view(np.dtype((np.void,
current_superpixel_data_channel_info_all.dtype.itemsize *
current_superpixel_data_channel_info_all.shape[1])))
_, idx = np.unique(current_superpixel_data_channel_info_all_transformed, return_index=True)
unique_labels = current_superpixel_data_channel_info_all[idx].tolist()
return unique_labels
def assign_updated_labels(num_superpixels,current_superpixel_data,unique_labels):
# iterate: assign updated labels for all superpixels
return_map=np.ones((current_superpixel_data[1].shape[0],current_superpixel_data[1].shape[1]))*(-1)
for superpixel_index in range(num_superpixels):
print superpixel_index
current_superpixel_data_channel_info = current_superpixel_data[0][superpixel_index][2:].tolist()
index_in_list = unique_labels.index(current_superpixel_data_channel_info)
# replace labels
return_map[current_superpixel_data[1] == superpixel_index] = index_in_list
return return_map
def palette(new_superpixel_label):
palette = [0] * 256 * 3
for index in range(len(palette)):
palette[index] = np.random.choice(range(0,255))
return palette
if __name__ == '__main__':
# superpixel_result_folder='/mnt/scratch/panqu/SLIC/server_combine_all_val/'
# superpixel_result_folder='/mnt/scratch/panqu/SLIC/2016_08_24_00:19:23/'
superpixel_result_folder='/mnt/scratch/panqu/SLIC/server_val/2016_08_29_15:57:51/'
original_files_folder='/home/panquwang/Dataset/CityScapes/leftImg8bit_trainvaltest/leftImg8bit/val/'
gt_folder='/home/panquwang/Dataset/CityScapes/gtFine/val/'
superpixel_images=glob.glob(os.path.join(superpixel_result_folder,'*.png'))
superpixel_images.sort()
superpixel_data=glob.glob(os.path.join(superpixel_result_folder,'*.dat'))
superpixel_data.sort()
original_files=glob.glob(os.path.join(original_files_folder,"*","*.png"))
original_files.sort()
gt_files=glob.glob(os.path.join(gt_folder,"*","*gtFine_labelTrainIds.png"))
gt_files.sort()
time=datetime.now().strftime('%Y_%m_%d_%H:%M:%S')
place_to_save = '/mnt/scratch/panqu/SLIC/merged_results/'+time
# place_to_save_color = '/mnt/scratch/panqu/SLIC/merged_results/'+time
if not os.path.exists(place_to_save):
os.makedirs(place_to_save)
os.makedirs(os.path.join(place_to_save, 'data'))
os.makedirs(os.path.join(place_to_save, 'visualization'))
# iterate through all images to give them real super pixel labels
# for index in range(len(superpixel_data)):
for index in range(0,50):
current_superpixel_data = cPickle.load(open(superpixel_data[index], "rb"))
to_be_saved_file_name=superpixel_data[index].split('/')[-1][:-11]+'_merged.png'
to_be_saved_data_name = superpixel_data[index].split('/')[-1][:-11] + '_merged.dat'
# statisticst
num_superpixels=len(current_superpixel_data[0])
superpixel_labels=current_superpixel_data[1]
# refine superpixel within an image
index_superpixel=0
del_list=[]
new_starting_index=0
# find unique labels
unique_labels=find_unique_labels(current_superpixel_data)
# iterate: assign updated labels for all superpixels
return_map=assign_updated_labels(num_superpixels,current_superpixel_data,unique_labels)
# plt.imshow(current_superpixel_data[1])
# plt.show()
print "Finding connected component for "+str(index)+' '+superpixel_data[index].split('/')[-1]
new_superpixel_label=0
chosen_label_values=[]
final_map=np.ones((1024,2048))*(-1)
for index_unique_label,unique_label in enumerate(unique_labels):
current_unique_label_layer=return_map==index_unique_label
current_unique_label_layer=current_unique_label_layer.astype(np.uint8)
current_unique_label_layer_connected_component=cv2.connectedComponents(current_unique_label_layer, connectivity=8)
total_connected_components=current_unique_label_layer_connected_component[0]
# plt.imshow(current_unique_label_layer_connected_component[1])
# plt.show()
for index_connected_component in range(1,total_connected_components):
# chosen_label_value=np.random.choice(label_array)
final_map[current_unique_label_layer_connected_component[1]==index_connected_component]=new_superpixel_label
# label_array.remove(chosen_label_value)
chosen_label_values.append(new_superpixel_label)
new_superpixel_label=new_superpixel_label+1
# render
my_palette=palette(new_superpixel_label)
result_img = Image.fromarray(final_map.astype(np.uint8)).convert('P')
result_img.putpalette(my_palette)
result_img.save(os.path.join(place_to_save, 'visualization', to_be_saved_file_name))
# save data
final_superpixel_data=current_superpixel_data+(final_map,unique_labels,chosen_label_values)
cPickle.dump(final_superpixel_data, open(os.path.join(place_to_save, 'data', to_be_saved_data_name), "w+"))