-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageSegmentationUsingRegionGrowingUsingAnotherApproach.py
142 lines (115 loc) · 3.67 KB
/
imageSegmentationUsingRegionGrowingUsingAnotherApproach.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
import math
from PIL import Image
from pylab import *
import matplotlib.cm as cm
import scipy as sp
import random
im = Image.open('imagetr.jpg').convert('L')
arr = np.asarray(im)
out = Image.open('imagetr.jpg').convert('L')
arr_out = np.asarray(out)
rows, columns = np.shape(arr)
# print '\nrows',rows,'columns',columns
plt.figure()
plt.imshow(im)
plt.gray()
# User selects the intial seed point
print('\nPlease select the initial seed point')
pseed = plt.ginput(1)
# pseed
# print pseed[0][0],pseed[0][1]
x = int(pseed[0][0])
y = int(pseed[0][1])
# x = int(179)
# y = int(86)
seed_pixel = []
seed_pixel.append(x)
seed_pixel.append(y)
print('you clicked:', seed_pixel)
# closing figure
plt.close()
img_rg = np.zeros((rows + 1, columns + 1))
img_rg[seed_pixel[0]][seed_pixel[1]] = 255.0
img_display = np.zeros((rows, columns))
region_points = []
region_points.append([x, y])
def find_region():
print('\nloop runs till region growing is complete')
# print 'starting points',i,j
count = 0
x = [-1, 0, 1, -1, 1, -1, 0, 1]
y = [-1, -1, -1, 0, 0, 1, 1, 1]
while (len(region_points) > 0):
if count == 0:
point = region_points.pop(0)
i = point[0]
j = point[1]
print('\nloop runs till length become zero:')
print('len', len(region_points))
# print 'count',count
val = arr[i][j]
lt = val - 8
ht = val + 8
# print 'value of pixel',val
for k in range(8):
# print '\ncomparison val:',val, 'ht',ht,'lt',lt
if img_rg[i + x[k]][j + y[k]] != 1:
try:
if arr[i + x[k]][j + y[k]] > lt and arr[i + x[k]][j + y[k]] < ht:
# print '\nbelongs to region',arr[i+x[k]][j+y[k]]
img_rg[i + x[k]][j + y[k]] = 1
p = [0, 0]
p[0] = i + x[k]
p[1] = j + y[k]
if p not in region_points:
if 0 < p[0] < rows and 0 < p[1] < columns:
''' adding points to the region '''
region_points.append([i + x[k], j + y[k]])
else:
# print 'not part of region'
img_rg[i + x[k]][j + y[k]] = 0
except IndexError:
continue
# print '\npoints list',region_points
point = region_points.pop(0)
i = point[0]
j = point[1]
count = count + 1
# find_region(point[0], point[1])
find_region()
ground_out = np.zeros((rows, columns))
for i in range(rows):
for j in range(columns):
if arr_out[i][j] > 125:
ground_out[i][j] = int(1)
else:
ground_out[i][j] = int(0)
tp = 0
tn = 0
fn = 0
fp = 0
for i in range(rows):
for j in range(columns):
if ground_out[i][j] == 1 and img_rg[i][j] == 1:
tp = tp + 1
if ground_out[i][j] == 0 and img_rg[i][j] == 0:
tn = tn + 1
if ground_out[i][j] == 1 and img_rg[i][j] == 0:
fn = fn + 1
if ground_out[i][j] == 0 and img_rg[i][j] == 1:
fp = fp + 1
''' ********************************** Calculation of Tpr, Fpr, F-Score ***************************************************'''
print('\n************Calculation of Tpr, Fpr, F-Score********************')
# TP rate = TP/TP+FN
tpr = float(tp) / (tp + fn)
print("\nTPR is:", tpr)
# fp rate is
fpr = float(fp) / (fp + tn)
print("\nFPR is:", fpr)
# F-score as 2TP/(2TP + FP + FN)
fscore = float(2 * tp) / ((2 * tp) + fp + fn)
print("\nFscore:", fscore)
plt.figure()
plt.imshow(img_rg, cmap="Greys_r")
plt.colorbar()
plt.show()