Repository navigation
Expand file tree
/
Copy pathactiveContour.py
More file actions
160 lines (127 loc) · 4.67 KB
/
Copy pathactiveContour.py
File metadata and controls
160 lines (127 loc) · 4.67 KB
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
155
156
157
158
159
160
"""
Abeer Mohamed
"""
import os
import sys
import cv2 as cv2
import matplotlib.cm as cm
from scipy.ndimage import filters
import numpy as np
import pylab as plb
import matplotlib.pyplot as plt
import copy
import scipy.ndimage
import scipy.ndimage as nd
import matplotlib.pyplot as plt
_ALPHA = 300
_BETA = 2
_W_LINE = 80
_W_EDGE = 80
_NUM_NEIGHBORS = 9
# the candaidate 8 movment
neighbors = np.array([[i, j] for i in range(-1, 2) for j in range(-1, 2)])
# internal forces = summation from i to n alph * |Vi+1 - Vi|2 + BETA * |Vi+1 - 2* Vi + Vi-1 |2
def internalEnergy(snake):
iEnergy=0
snakeLength=len(snake)
for index in range(snakeLength-1,-1,-1):
nextPoint = (index+1)%snakeLength
currentPoint = index % snakeLength
previousePoint = (index - 1) % snakeLength
iEnergy = iEnergy+ (_ALPHA *(np.linalg.norm(snake[nextPoint] - snake[currentPoint] )**2))\
+ (_BETA * (np.linalg.norm(snake[nextPoint] - 2 * snake[currentPoint] + snake[previousePoint])**2))
return iEnergy
# total energy for internal and external without constant
def totalEnergy(grediant, image, snake):
iEnergy = internalEnergy(snake)
eEnergy=externalEnergy(grediant, image, snake)
tEnergy = iEnergy+eEnergy
return tEnergy
#plot
def _display(image, changedPoint=None, snaxels=None):
plb.clf()
if snaxels is not None:
for s in snaxels:
if(changedPoint is not None and (s[0] == changedPoint[0] and s[1] == changedPoint[1])):
plb.plot(s[0], s[1], 'r', markersize=10.0)
else:
plb.plot(s[0],s[1],'g.',markersize=10.0)
plb.imshow(image, cmap=cm.Greys_r)
plb.draw()
return
# external forces summation of image grediant for all contour points
def externalEnergy(grediant,image,snak):
sum = 0
snaxels_Len = len(snak)
for index in range(snaxels_Len - 1):
point = snak[index]
sum = +(image[point[1]][point[0]])
pixel = 255 * sum
eEnergy = _W_LINE*pixel -_W_EDGE*imageGradient(grediant, snak)
return eEnergy
def interval_mapping(image, from_min, from_max, to_min, to_max):
from_range = from_max - from_min
to_range = to_max - to_min
scaled = np.array((image - from_min) / float(from_range), dtype=float)
return to_min + (scaled * to_range)
def basicImageGradiant(image):
s_mask = 17
sobelx = np.abs(cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=s_mask))
sobelx = interval_mapping(sobelx, np.min(sobelx), np.max(sobelx), 0, 255)
sobely = np.abs(cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=s_mask))
sobely = interval_mapping(sobely, np.min(sobely), np.max(sobely), 0, 255)
gradient = 0.5 * sobelx + 0.5 * sobely
return gradient
def imageGradient(gradient, snak):
sum = 0
snaxels_Len= len(snak)
for index in range(snaxels_Len-1):
point = snak[index]
sum = sum+((gradient[point[1]][point[0]]))
return sum
def isPointInsideImage(image, point):
return np.all(point < np.shape(image)) and np.all(point > 0)
def _pointsOnCircle(center, radius, num_points=12):
points = np.zeros((num_points, 2), dtype=np.int32)
for i in range(num_points):
theta = float(i)/num_points * (2 * np.pi)
x = center[0] + radius * np.cos(theta)
y = center[1] + radius * np.sin(theta)
p = [x, y]
points[i] = p
return points
def activeContour(image_file, center, radius):
image = cv2.imread(image_file, 0)
plb.ion()
plb.figure(figsize=np.array(np.shape(image)) / 50.)
snake = _pointsOnCircle(center, radius, 20)
grediant = basicImageGradiant(image)
plb.ioff()
snakeColon = copy.deepcopy(snake)
for i in range(100):
for index,point in enumerate(snake):
min_energy2 = float("inf")
for cindex,movement in enumerate(neighbors):
next_node = (point +movement)
if not isPointInsideImage(image, next_node):
continue
if not isPointInsideImage(image, point):
continue
snakeColon[index]=next_node
totalEnergyNext = totalEnergy(grediant, image, snakeColon)
if (totalEnergyNext < min_energy2):
min_energy2 = copy.deepcopy(totalEnergyNext)
indexOFlessEnergy = copy.deepcopy(cindex)
snake[index] = (snake[index]+neighbors[indexOFlessEnergy])
snakeColon = copy.deepcopy(snake)
plb.ioff()
_display(image,None, snake)
plb.plot()
plb.savefig(os.path.splitext(image_file)[0] + "-segmented.png")
plb.show()
return
def _test():
activeContour("brainTumor.png", (98, 152), 30)
return
if __name__ == '__main__':
_test()