-
Notifications
You must be signed in to change notification settings - Fork 0
/
Region_Growing.py
32 lines (23 loc) · 1007 Bytes
/
Region_Growing.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
import numpy as np
import cv2
def RegionGrowing(image_path, seed):
image=cv2.imread(image_path)
height, width = image.shape[:2]
mask = np.zeros((height, width), np.uint8)
seed_x_point, seed_y_point = seed
threshold = image[seed_x_point, seed_y_point]
queue = []
queue.append((seed_x_point, seed_y_point))
while queue:
seed_x_point, seed_y_point = queue.pop(0)
if seed_x_point < 0 or seed_y_point < 0 or seed_x_point >= height or seed_y_point >= width:
continue
if mask[seed_x_point, seed_y_point]:
continue
if abs(image[seed_x_point, seed_y_point] - threshold).any() <= 10:
mask[seed_x_point, seed_y_point] = 255
queue.append((seed_x_point - 1, seed_y_point))
queue.append((seed_x_point + 1, seed_y_point))
queue.append((seed_x_point, seed_y_point - 1))
queue.append((seed_x_point, seed_y_point + 1))
return mask