-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
65 lines (54 loc) · 1.75 KB
/
maze.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
import math
from skimage import io
from skimage.filters import threshold_isodata
from skimage.color import rgb2gray, rgb2hsv
from matplotlib import pyplot as plt
from skimage.measure import label, regionprops
from skimage.morphology import binary_dilation, binary_erosion, disk
import numpy as np
img = io.imread('maze.jpg')
img_hsv = rgb2hsv(img)
img_gray = rgb2gray(img)
# Find red markers
# Red hue is near 0.0
red_dots_bin = np.logical_or(img_hsv[:, :, 0] < 0.1, img_hsv[:, :, 0] > 0.9)
# Moderate saturation treshold
red_dots_bin &= img_hsv[:, :, 1] > 0.3
# Moderate value treshold
red_dots_bin &= img_hsv[:, :, 2] > 0.3
red_dots_bin = red_dots_bin.astype(np.uint8)
markers_labels = label(red_dots_bin)
markers_regions = regionprops(markers_labels)
markers = []
diams = []
for props in markers_regions:
markers.append(props.centroid)
diams.append(props.equivalent_diameter)
max_marker_diam = math.ceil(max(diams))
plt.figure()
plt.imshow(red_dots_bin, cmap='gray')
for marker in markers:
plt.plot(marker[1], marker[0], '.r')
plt.title('Markers')
# Walls binarisation
thr = threshold_isodata(img_gray)
img_bin = img_gray > thr
img_bin = img_bin.astype(np.uint8)
plt.figure()
plt.imshow(img_bin, cmap='gray')
plt.title('Binarised')
# Remove marker dots
dilated = binary_dilation(red_dots_bin,
np.ones((max_marker_diam * 2,
max_marker_diam * 2)))
img_bin = np.clip(img_bin + dilated, 0, 1)
plt.figure()
plt.imshow(img_bin, cmap='gray')
plt.title('Binarised with markers removed')
# Expand walls
diam = np.linalg.norm(np.array(markers[0]) - np.array(markers[1]))
margin = int(0.55 * diam)
img_bin_exp = binary_erosion(img_bin, disk(margin))
plt.figure()
plt.imshow(img_bin_exp, cmap='gray')
plt.show()