How to improve performance of cell segmentation using watershed algorithm with opencv-python

442 Views Asked by At

I have tried to segment cells in H&E-stained histopathological images using Watershed algorithm of opencv-python. The code I used is totally same as Docs opencv code in link below.

Watershed Code Source

Result Image

But as you see the result, the performance of segmentation is not that much good. Some cells could not be detected.

I want to detect all of cells at a time in that image.

In the case of cell in biomedical, I think this is more sensitive than normal object segmentation.

In original code, I added and applied two Blur algorithms before using cv2.morphologyEx().

img = cv2.imread("Path_of_Image")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Convert to Binary Image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)

sure_bg = cv2.dilate(opening, kernel, iterations=3)
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)

ret, sure_fg = cv2.threshold(dist_transform, 0.5*dist_transform.max(), 255, 0)

sure_fg = np.uint8(sure_fg)

unknown = cv2.subtract(sure_bg, sure_fg)

ret, markers = cv2.connectedComponents(sure_fg)

markers = markers + 1
markers[unknown == 255] = 0

markers = cv2.watershed(img_rgb, markers)
img_rgb[markers == -1] = [255, 0, 0]

images = [gray, thresh, sure_bg, dist_transform, sure_fg, unknown, markers, img_rgb]
titles = ['Gray','Binary','Sure BG','Distance','Sure FG','Unknow','Markers','Result']

plt.figure(figsize=(12, 12))
for i in range(len(images)):
    plt.subplot(2, 4, i + 1),
    plt.imshow(images[i], cmap='gray'),
    plt.title(titles[i]),
    plt.xticks([]),plt.yticks([])
#     plt.figure(figsize= (5, 5))

# plt.tight_layout()
plt.show()

There was a litte bit improvement, but still need changes.

How can I deal with this problem? Do I have to more examine Marker value or something?

I wonder your thinking.

Thank you in advance.

[Add] This is Original Image. Original Image

0

There are 0 best solutions below