Home > Mobile >  K-Means Binary Clustering in OpenCV to Extract Mask
K-Means Binary Clustering in OpenCV to Extract Mask

Time:07-06

I try to use cv2.kmeans to segment the left auricle DICOM image as mask.

enter image description here

I use the following code to do the k-means binary clustering in OpenCV.

import numpy as np
import cv2
import os
from matplotlib import pyplot as plt

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

criteria = (cv2.TERM_CRITERIA_EPS   cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, label, center = cv2.kmeans(img2, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)

res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imwrite('1_LA_kmeans.jpg', res2)

Then, I can get this segmentation result well.

enter image description here

But how can I extract one of the segmentations as mask?

I have referred other similar questions, and I try to use the code from enter image description here

Becase I want to calculate the area of the left auricle, I need to extract the mask like below.

enter image description here

So, how can I extract one of the binary segmentation results?

CodePudding user response:

Thanks for @fmw42's help.

After I refer this enter image description here

  • Related