-
Notifications
You must be signed in to change notification settings - Fork 11
/
RegionGrowing.py
154 lines (107 loc) · 4.3 KB
/
RegionGrowing.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
import cv2
import itertools
import numpy as np
import random
import sys
#class pour une pile
class Stack():
def __init__(self):
self.item = []
self.obj=[]
def push(self, value):
self.item.append(value)
def pop(self):
return self.item.pop()
def size(self):
return len(self.item)
def isEmpty(self):
return self.size() == 0
def clear(self):
self.item = []
class regionGrow():
def __init__(self,im_path,th):
self.readImage(im_path)
self.h, self.w,_ = self.im.shape
self.passedBy = np.zeros((self.h,self.w), np.double)
self.currentRegion = 0
self.iterations=0
self.SEGS=np.zeros((self.h,self.w,3), dtype='uint8')
self.stack = Stack()
self.thresh=float(th)
def readImage(self, img_path):
self.im = cv2.imread(img_path,1).astype('int')
def getNeighbour(self, x0, y0):
return [
(x, y)
for i, j in itertools.product((-1, 0, 1), repeat=2)
if (i, j) != (0, 0) and self.boundaries(x := x0 + i, y := y0 + j)
]
def create_seeds(self):
return [
[self.h/2,self.w/2],
[self.h/3,self.w/3],[2*self.h/3,self.w/3],[self.h/3-10,self.w/3],
[self.h/3,2*self.w/3],[2*self.h/3,2*self.w/3],[self.h/3-10,2*self.w/3],
[self.h/3,self.w-10],[2*self.h/3,self.w-10],[self.h/3-10,self.w-10]
]
def ApplyRegionGrow(self, cv_display = True):
randomseeds = self.create_seeds()
np.random.shuffle(randomseeds)
for x0 in range (self.h):
for y0 in range (self.w):
if self.passedBy[x0,y0] == 0 : #and (np.all(self.im[x0,y0] > 0)) :
self.currentRegion += 1
self.passedBy[x0,y0] = self.currentRegion
self.stack.push((x0,y0))
self.prev_region_count= 0
while not self.stack.isEmpty():
x,y = self.stack.pop()
self.BFS(x,y)
self.iterations+=1
if self.PassedAll():
break
if self.prev_region_count< 8*8 :
x0, y0 = self.reset_region(x0, y0)
if self.iterations>200000:
print("Max Iterations")
print(f"Iterations : {str(self.iterations)}")
if cv_display:
[self.color_pixel(i,j) for i, j in itertools.product(range(self.h), range (self.w))]
self.display()
def reset_region(self, x0, y0):
self.passedBy[self.passedBy==self.currentRegion] = 0
x0=random.randint(x0-4,x0+4)
y0=random.randint(y0-4,y0+4)
x0 = np.clip(x0, 0, self.h - 1)
y0 = np.clip(y0, 0, self.w - 1)
self.currentRegion-=1
return x0, y0
def color_pixel(self, i, j):
val = self.passedBy[i][j]
self.SEGS[i][j] = (255, 255, 255) if (val==0) else (val*35, val*90, val*30)
def display(self):
cv2.imshow("",self.SEGS)
cv2.waitKey(0)
cv2.destroyAllWindows()
def BFS(self, x0,y0):
regionNum = self.passedBy[x0,y0]
elems = [np.mean(self.im[x0, y0])]
var=self.thresh
neighbours=self.getNeighbour(x0,y0)
for x,y in neighbours:
if self.passedBy[x,y] == 0 and self.distance(x,y,x0,y0) < var:
if self.PassedAll():
break
self.passedBy[x,y] = regionNum
self.stack.push((x,y))
elems.append(np.mean(self.im[x,y]))
var=np.var(elems)
self.prev_region_count+=1
var=max(var,self.thresh)
def PassedAll(self, max_iteration = 200000):
return self.iterations > max_iteration or np.all(self.passedBy > 0)
def boundaries(self, x,y):
return 0<=x<self.h and 0<=y<self.w
def distance(self,x,y,x0,y0):
return np.linalg.norm(self.im[x0, y0] - self.im[x, y])
exemple = regionGrow(sys.argv[1],sys.argv[2])
exemple.ApplyRegionGrow()