-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactiveContour.py
More file actions
24 lines (23 loc) · 792 Bytes
/
Copy pathactiveContour.py
File metadata and controls
24 lines (23 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contour
import cv2
img = cv2.imread("resources/einstein.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img = data.astronaut()
# img = rgb2gray(img)
s = np.linspace(0, 2 * np.pi, 240)
r = 170 + 150 * np.sin(s)
c = 260 + 150 * np.cos(s)
init = np.array([r, c]).T
snake = active_contour(gaussian(img, 3), init, alpha=0.015, beta=10, gamma=0.001)
fig, ax = plt.subplots(figsize=(7, 7))
ax.imshow(img, cmap=plt.cm.gray)
ax.plot(init[:, 1], init[:, 0], '--r', lw=3)
ax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)
ax.set_xticks([]), ax.set_yticks([])
ax.axis([0, img.shape[1], img.shape[0], 0])
plt.show()