-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregion_of_interest.py
More file actions
219 lines (203 loc) · 7.16 KB
/
region_of_interest.py
File metadata and controls
219 lines (203 loc) · 7.16 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 19 12:53:52 2019
@author: kota421
"""
import numpy as np
import cv2
def makeArray(image):
array = np.zeros_like(image)
return array
def labelArray(array,island,label):
"""
array: zero_like array of shape == array.shape
island: List of tuples where each tuple has two ints, representing a xy
coordinate of a point in an image.
label: int
"""
for (x,y) in island:
array[x,y] = label
return array
def BFS(x,y,image,isValidPixel):
"""
Assumes:
x,y: int, the start point of the flood fill
isValidPixel: a function that given coordinates of the point tells you whether
the point is valid or not
Returns:
island: list of tuples. Each tuple contains two ints, representing a xy
coordinate of a point.
"""
island = [] # Represents an island, store valid pixels (valid: consecuitive AND
# check_validity == True)
q = []
q.append((x,y))
island.append((x,y))
while q:
(x1,y1) = q.pop()
if (isValidPixel(x1+1,y1,image)) and ((x1+1,y1) not in island):
q.append((x1+1,y1))
island.append((x1+1,y1))
if (isValidPixel(x1-1,y1,image)) and ((x1-1,y1) not in island):
q.append((x1-1,y1))
island.append((x1-1,y1))
if (isValidPixel(x1,y1+1,image)) and ((x1,y1+1) not in island):
q.append((x1,y1+1))
island.append((x1,y1+1))
if (isValidPixel(x1,y1-1,image)) and ((x1,y1-1) not in island):
q.append((x1,y1-1))
island.append((x1,y1-1))
return island
def getIslands(image,isValidPixel):
"""
Assumes:
image: cv2.image, gray
isValidPixel: a function that, given coordinates of a point, tells you whether
the point is valid or not
Returns:
islands: list of lists; each list contains xy coordinates of points as tuple
label_arrays: np array of the same shape as image
"""
islands = []
master_label = makeArray(image)
label_arrays = []
label = 1 # Name for each island
# At the first valid pixel encountered:
for y in range(image.shape[1]):
for x in range(image.shape[0]):
# First, check if it already exists in islands,
if master_label[x][y]:
pass
# If it's not yet in islands, then start a floodfill from that pixel.
elif isValidPixel(x,y,image):
## Flood-fill algorithm (BFS)
label_array = makeArray(image)
island = BFS(x,y,image,isValidPixel)
## Keeping track of valid pixels
labelArray(label_array,island,label)
labelArray(master_label,island,label)
#print('Island Area[px]:',len(island))
## Adding a new island to islands
islands.append(island)
label_arrays.append(label_array)
## Changing island label for the next island
label += 0
return islands,label_arrays
def array2Coordinates(array):
"""
Assumes:
array:
[
[0 0 0 0 0 0 0 0...]
[0 0 0 0 1 1 1 0...]
[0 0 0 1 1 1 1 1...]
[0 0 1 1 1 1 0 0...]
...
]
Returns:
x, y: Two 1D np arrays: xy coordinates of a non-empty position
in the array
"""
points = []
for i in range(len(array)):
for j in range(len(array[i])):
if array[i][j]: # if it has a value (i.e. colored or white in image)
points.append((i,j))
points = np.array(points)
## Create x, y scatter poitns from POINTS
y= points[:,0].reshape(-1,1)
x= points[:,1].reshape(-1,1)
return x,y
def maskingPolygonMaker(label_array):
x,y = array2Coordinates(label_array)
maxX, minX =max(x),min(x)
maxY, minY =max(y),min(y)
(h, w) = int((maxY-minY)[0]), int((maxX-minX)[0])
polygon = np.array([
[(minX,minY), (maxX,minY),(maxX,maxY),(minX,maxY)]
])
mask = np.zeros_like(label_array)
cv2.fillPoly(mask, polygon, 255)
result = np.zeros([mask.shape[0],mask.shape[1],3]) # Conveting to a color image
result[:,:,0] = mask
result[:,:,1] = mask
result[:,:,2] = mask
result = result.astype(np.uint8)
return result, (h,w)
def mask4ROI(image,isValidPixel,mode='biggest'):
"""
Assumes:
image: cv2.image gray.
isValidPixel: a function that, given coordinates of a point, tells you whether
the point is valid or not.
mode: Method to choose which island to leave intact.
'biggest': leaves the biggest island , default
'top':leaves the island on the top of the image
Returns:
i) If image has the target color:
mask: numpy.array of shape == image.shape
centerX, centerY: int, int. x-y coordinates of the center pixel of mask;
ii) If image does not have the target color:
None, None, None
"""
islands,label_arrays = getIslands(image,isValidPixel)
if not islands:
return None, 0, 0,(0,0)
if mode=='top':
x,y = array2Coordinates(label_arrays[0])
maxX, minX =max(x),min(x)
maxY, minY =max(y),min(y)
mask, (h,w) = maskingPolygonMaker(label_arrays[0])
return mask, (maxX+minX)/2, (maxY+minY)/2,(h,w)
if mode=='biggest':
island_areas = []
for island in islands:
island_areas.append(len(island))
biggest = island_areas.index(max(island_areas))
x,y = array2Coordinates(label_arrays[biggest])
maxX, minX =max(x),min(x)
maxY, minY =max(y),min(y)
mask,(h,w) = maskingPolygonMaker(label_arrays[biggest])
return mask, int((maxX+minX)/2), int((maxY+minY)/2), (h,w)
def region_of_interest(image,mask):
"""
Assumes:
image: color (width,height,3)
mask: np.array (width,height,3) from the function mask4ROI
Returns:
i) If mask is not empty:
Bitwise and of image and mask
ii) If mask is empty:
image
"""
# Is mask empty or not?
try:
mask.any()
return cv2.bitwise_and(image, mask)
except AttributeError:
return image
##
# =============================================================================
# def DFS(x, y, visited,image,isValidPixel):
# """
# Flood-fill algorithm in depth first search
# """
# n,m = image.shape[0],image.shape[1]
# if (x >= n or y >= m):
# return
# if(x < 0 or y < 0):
# return
# if (x,y) in visited.keys():
# return
# visited[(x,y)] = True
# if isValidPixel(x-1,y,image):
# DFS(x-1, y, visited, image,isValidPixel)
# if isValidPixel(x,y-1,image):
# DFS(x, y-1, visited, image,isValidPixel)
# if isValidPixel(x,y+1,image):
# DFS(x, y+1, visited, image,isValidPixel)
# if isValidPixel(x+1,y,image):
# DFS(x+1, y, visited, image,isValidPixel)
#
# =============================================================================