0

I tried to crop each mask label from CAMUS dataset but in vain.

enter image description here

I wanted to get the patches of left atrium (white) and left ventricle (dark gray) separately.

import numpy as np
import cv2

img = cv2.imread('t_0_2CH_ED.jpg')
Z = np.float32(img.reshape((-1,3)))

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
_,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.reshape((img.shape[:-1]))
reduced = np.uint8(centers)[labels]
result = [np.hstack([img, reduced])]
for i, c in enumerate(centers):
    mask = cv2.inRange(labels, i, i)
    mask = np.dstack([mask]*3) # Make it 3 channel
    ex_img = cv2.bitwise_and(img, mask)
    ex_reduced = cv2.bitwise_and(reduced, mask)
    result.append(np.hstack([ex_img, ex_reduced]))

cv2.imwrite('camus_kmeans.jpg', np.vstack(result))

But I had trouble in cutting non-binary mask.

enter image description here

TseHsien
  • 73
  • 8
  • I don't understand your code. You have kmeans with k=2, but show 4 colors in your images. Please explain. – fmw42 Jul 17 '23 at 15:41

0 Answers0