/
compute_global_contrast.py
135 lines (86 loc) · 3.23 KB
/
compute_global_contrast.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
from matplotlib import pyplot as plt
import numpy as np
import argparse
import cv2
import os
import scipy.io as io
import shutil
import torch
def MaxMinNormalization(x, Max, Min):
x = (x - Min) / (Max - Min)
return x
def cal_chi_distance(img_path, gt_path):
# print(img_path)
image = cv2.imread(img_path)
print(img_path)
mask = cv2.imread(gt_path)
mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# print('img.shape', image.shape)
# print('mask.shape', mask.shape)
reverse_mask = 255 - mask
fore_hist = cv2.calcHist([image], [0, 1, 2], mask, [8, 8, 8], [0, 255, 0, 255, 0, 255])
back_hist = cv2.calcHist([image], [0, 1, 2], reverse_mask, [8, 8, 8], [0, 255, 0, 255, 0, 255])
mask[mask > 0] = 1
mask_area = mask.sum()
reverse_mask[reverse_mask > 0] = 1
reverse_mask_area = reverse_mask.sum()
fore_hist = (fore_hist/mask_area).flatten()
back_hist = (back_hist/reverse_mask_area).flatten()
fore_hist = fore_hist.astype(np.float32)
back_hist = back_hist.astype(np.float32)
d = cv2.compareHist(fore_hist, back_hist, method=cv2.HISTCMP_CHISQR)
return d
def cal_chi_distance_for_depth(depth_path, gt_path):
depth = cv2.imread(depth_path)
mask = cv2.imread(gt_path)
mask = cv2.resize(mask, (depth.shape[1], depth.shape[0]))
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
reverse_mask = 255 - mask
fore_hist = cv2.calcHist([depth], [0], mask, [255], [0, 255])
back_hist = cv2.calcHist([depth], [0], reverse_mask, [255], [0, 255])
mask[mask > 0] = 1
mask_area = mask.sum()
reverse_mask[reverse_mask > 0] = 1
reverse_mask_area = reverse_mask.sum()
fore_hist = (fore_hist / mask_area).flatten()
back_hist = (back_hist / reverse_mask_area).flatten()
fore_hist = fore_hist.astype(np.float32)
back_hist = back_hist.astype(np.float32)
d = cv2.compareHist(fore_hist, back_hist, method=cv2.HISTCMP_CHISQR)
return d
def load_list(file):
with open(file) as f:
lines = f.read().splitlines()
files = []
depths = []
labels = []
for line in lines:
files.append(line.split(' ')[0])
depths.append(line.split(' ')[1])
labels.append(line.split(' ')[2])
return files, depths, labels
if __name__ == "__main__":
lst = '../list/'
# compute = 'rgbImg_global_contrast'
compute = 'depthMap_global_contrast'
save_path = compute + '/ori_txt/'
if not os.path.exists(save_path):
os.makedirs(save_path)
datasets_lists = os.listdir(lst)
for dataset_list in datasets_lists:
print('starting analyze {}'.format(dataset_list))
dataset_name = dataset_list.split('_list.txt')[0]
imgs, depths, gts = load_list(lst + dataset_list)
if compute == 'rgbImg_global_contrast':
imgs = imgs
else:
imgs = depths
ratio = np.zeros(len(imgs))
for i in range(len(imgs)):
if compute == 'rgbImg_global_contrast':
d = cal_chi_distance(imgs[i], gts[i])
else:
d = cal_chi_distance_for_depth(depths[i], gts[i])
ratio[i] = d