How can i detect multiple segments using active_contour?

I am new to python and image processing and have very basic knowledge in them.

I am trying to implement active_contour to segment blue color on this image:

I got this code from the scikit-image website. How can I segment the blue color and count the segments?

import numpy as np
import cv2
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

img = cv2.imread(path)
#img = data.astronaut()
img = rgb2gray(img)

s = np.linspace(0, 2*np.pi, 400)
x = 220 + 100*np.cos(s)
y = 100 + 100*np.sin(s)
init = np.array([x, y]).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[:, 0], init[:, 1], '--r', lw=3)
ax.plot(snake[:, 0], snake[:, 1], '-b', lw=3)
ax.set_xticks([]), ax.set_yticks([])
ax.axis([0, img.shape[1], img.shape[0], 0])

I know what the above code is doing. It delineates a strong energy region inside the circle. How can i do that in the image provided above?

@kamilhassaan,

Welcome to the forum.

You’ve found and important limitation of active contours. It is not possible for a single active contour to delineate multiple objects. If it starts as a closed curve, it will stay a closed curve forever. The alternative is to have as many active contours as objects you want to segment, but that’s what you’re looking to compute! So that’s out of the question.

You should look into other active-contour-like methods, such as morphological snakes, or the “chan-vese” level set method, for example. These can change their topology (i.e. if it starts with one connected component, it can end up with an arbitrary number). Usually you initialize it with some circles spread out throughout the image and let some of them grow / move and others “disappear.” You can see that in examples on the skimage doc.

Post back if you have any questions, and good luck,
John

2 Likes

Good day,

not sure what you are trying to achievve, but perhaps this result may help:

C-1

Regards

Herbie