проблема с получением каждого сегмента с помощью K означает сегментацию в отдельных изображениях с использованием Python и Opencv - PullRequest
0 голосов
/ 13 октября 2019

Я использую python и openCv для проекта сегментации мозга. Я сегментировал изображение МРТ головного мозга, используя K означает сегментацию. Я хочу, чтобы каждый сегмент приводился через k означает сегментацию в отдельных изображениях. пожалуйста, помогите мне в этом.

#k_means segmentation
epsilon = 0.01
number_of_iterations = 50
number_of_clusters = 4
print(criteria, 'Criteria K_means parameters')
#plt.imshow(criteria)

#k means segmentation
_, labels, centers =cv2.kmeans(kmeans_input, number_of_clusters, None, 
                               flags)
print(labels.shape, 'k-means segmentation')
#plt.imshow(labels)

#Adopting the labels
labels = labels.flatten('F')
for x in range (number_of_clusters): labels[labels == x] = centers [x]
print(labels.shape, 'adopting the tables value')
#plt.imshow(labels)

1 Ответ

0 голосов
/ 14 октября 2019

Я бы сделал это с помощью сегментации sklearn kmeans следующим образом. Я покажу, как создать сегментированное изображение, а затем выберите один цвет для представления. Я создаю маску из порога одного цвета, а затем применяю маску, чтобы затемнить другие цвета в сегментированном изображении. Вы можете написать цикл над каждым цветом, чтобы получить их все. Также возможно использовать маску, чтобы сделать цвет не прозрачным, а не черным. Но я не показываю это здесь. Или вы можете просто сохранить двоичные маски.

Ввод:

enter image description here

#!/bin/python3.7

from skimage import io
from sklearn import cluster
import sys
import cv2

# read input and convert to range 0-1
image = io.imread('barn.jpg')/255.0
h, w, c = image.shape

# reshape to 1D array
image_2d = image.reshape(h*w, c)

# set number of colors
numcolors = 6

# do kmeans processing
kmeans_cluster = cluster.KMeans(n_clusters=int(numcolors))
kmeans_cluster.fit(image_2d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

# need to scale result back to range 0-255
newimage = cluster_centers[cluster_labels].reshape(h, w, c)*255.0
newimage = newimage.astype('uint8')
io.imshow(newimage)
io.show()

# select cluster 3 (in range 1 to numcolors) and create mask
lower = cluster_centers[3]*255
upper = cluster_centers[3]*255
lower = lower.astype('uint8')
upper = upper.astype('uint8')
mask = cv2.inRange(newimage, lower, upper)

# apply mask to get layer 3
layer3 = newimage.copy()
layer3[mask == 0] = [0,0,0]
io.imshow(layer3)
io.show()

# save kmeans clustered image and layer 3
io.imsave('barn_kmeans.gif', newimage)
io.imsave('barn_kmeans_layer3.gif', layer3)


Кластерное изображение:

enter image description here

Результат для цвета 3:

enter image description here

ДОПОЛНЕНИЕ:

Для изображения в оттенках серого у меня работает следующее:

#!/bin/python3.7

from skimage import io
from sklearn import cluster
import sys
import cv2

# read input and convert to range 0-1
image = io.imread('barn_gray.jpg',as_gray=True)/255.0
h, w = image.shape

# reshape to 1D array
image_2d = image.reshape(h*w,1)

# set number of colors
numcolors = 6

# do kmeans processing
kmeans_cluster = cluster.KMeans(n_clusters=int(numcolors))
kmeans_cluster.fit(image_2d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

# need to scale result back to range 0-255
newimage = cluster_centers[cluster_labels].reshape(h, w)*255.0
newimage = newimage.astype('uint8')
io.imshow(newimage)
io.show()

# select cluster 3 (in range 1 to numcolors) and create mask
# note the cluster numbers and corresponding colors are not constant from run to run
lower = cluster_centers[3]*255
upper = cluster_centers[3]*255
lower = lower.astype('uint8')
upper = upper.astype('uint8')
print(lower)
print(upper)
mask = cv2.inRange(newimage, lower, upper)

# apply mask to get layer 3
layer3 = newimage.copy()
layer3[mask == 0] = [0]
io.imshow(layer3)
io.show()

# save kmeans clustered image and layer 3
io.imsave('barn_gray_kmeans.gif', newimage)
io.imsave('barn_gray_kmeans_layer3.gif', layer3)


...