-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres_extraction.py
146 lines (119 loc) · 4.51 KB
/
res_extraction.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
# /******************************************
# *MIT License
# *
# *Copyright (c) [2023] [Giuseppe Sorrentino, Marco Venere, Eleonora D'Arnese, Davide Conficconi, Isabella Poles, Marco Domenico Santambrogio]
# *
# *Permission is hereby granted, free of charge, to any person obtaining a copy
# *of this software and associated documentation files (the "Software"), to deal
# *in the Software without restriction, including without limitation the rights
# *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# *copies of the Software, and to permit persons to whom the Software is
# *furnished to do so, subject to the following conditions:
# *
# *The above copyright notice and this permission notice shall be included in all
# *copies or substantial portions of the Software.
# *
# *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# *SOFTWARE.
# ******************************************/
def binarize(img, soglia):
img[img>=soglia]=255
img[img<soglia]=0
return(img)
def correct(i):
gamma_corrected = exposure.adjust_log(i, 0.8)
return(gamma_corrected)
def distances(g,t):
g_t=np.asarray(g).astype(np.bool_)
t_t=np.asarray(t).astype(np.bool_)
di=1-(distance.dice(g_t.ravel(),t_t.ravel()))
ja=1-(distance.jaccard(g_t.ravel(),t_t.ravel()))
return(di,ja)
def IoU(g,t):
overlap = g & t # Logical AND
union = g | t # Logical OR
IOU = overlap.sum()/float(union.sum())
return(IOU)
def diff_pixel(g,t):
tot=0
for i in range(512):
for j in range(512):
if g[i][j]==t[i][j]:
tot=tot+1
tot1=(tot/(512*512))*100
return(tot1)
import argparse
from skimage import exposure
from scipy.spatial import distance
import cv2
import matplotlib.pyplot as plt
import pydicom
import glob
import pandas as pd
import numpy as np
parser = argparse.ArgumentParser(description='Athena software for res analysis onto a python env')
parser.add_argument("-f", "--flag", nargs='?', type=int, help='0 Read png images, 1 read dcm', default=0)
parser.add_argument("-tg", "--threshold_g", nargs='?', help='Threshold of G', default=2)
parser.add_argument("-tt", "--threshold_t", nargs='?', help='Threshold of T', default=2)
parser.add_argument("-rg", "--res_gold_path", nargs='?', help='Path of the Golden Results', default='./')
parser.add_argument("-rt", "--res_test_path", nargs='?', help='Path of the Results to compare against gold', default='./')
parser.add_argument("-l", "--label", nargs='?', help='prefix of the final file name', default='mat')
parser.add_argument("-rp", "--res_path", nargs='?', help='path of where store the results', default='./')
args = parser.parse_args()
flag=args.flag
soglia_g=args.threshold_g
#def 2
soglia_t=args.threshold_t
#def 3
res_gold_path = args.res_gold_path
res_test_path = args.res_test_path
prefix = args.res_path+"/gold-"+args.label+"-"
#print(res_gold_path)
#print(res_test_path)
dice=[]
jaccard=[]
acc=[]
curr_g_nmbr=[]
curr_test_nmbr=[]
Iou=[]
Gold=glob.glob(res_gold_path+'*.dcm')
if flag==0:
Test=glob.glob(res_test_path+'*.png')
else:
Test=glob.glob(res_test_path+'*.dcm')
Gold.sort()
Test.sort()
# print(Gold)
# print(Test)
for i,j in zip(Gold, Test):
# print(i)
# print(j)
curr_g_nmbr.append(((i.split('/')).pop()).split('.')[0][2:5])
curr_test_nmbr.append(((j.split('/')).pop()).split('.')[0][2:5])
g=pydicom.dcmread(i)
go=g.pixel_array
gold=cv2.normalize(go, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
if flag==0:
test=cv2.imread(j,0)
else:
t=pydicom.dcmread(j)
te=t.pixel_array
test=cv2.normalize(te, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
gold_cor=correct(gold)
test_cor=correct(test)
gold_bin=binarize(gold_cor,soglia_g)
test_bin=binarize(test_cor,soglia_t)
d=distances(gold_bin,test_bin)
iou=IoU(gold_bin,test_bin)
n_pix_diff=diff_pixel(gold_bin,test_bin)
dice.append(d[0])
jaccard.append(d[1])
Iou.append(iou)
acc.append(n_pix_diff)
df = pd.DataFrame(list(zip(*[Iou, acc])),columns=['IoU', 'Accuracy'])
df.to_csv(prefix+'score_results.csv', index=False)