拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 k表示影像分割后,有什么功能或方法可以显示影像簇吗?

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

白鹭 - 2022-01-25 1998 0 0
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('jelly.jpg') //reading the img
cv2.imshow(' img',img) //initial image
cv2.waitKey(0)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) // converting image to RGB

pixel_vals =img.reshape((-1,3)) //reshaping coloured 3d image to 2d image

pixel_vals = np.float32(pixel_vals)

criteria = (cv2.TERM_CRITERIA_EPS   cv2.TERM_CRITERIA_MAX_ITER,100,0.85) //setting criteria for kmeans

k= 5 //number of clusters

retval, labels, centers = cv2.kmeans(pixel_vals,k,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)


centers = np.uint8((centers))
segmented_data = centers[labels.flatten()]
segmented_img = segmented_data.reshape((img.shape)) //final image

cv2.imshow('K-means segmented img',segmented_img) // showing the final image after k means segmentation
cv2.waitKey(0)

cv2.destroyAllWindows() //destroying all window pop-up of images

我只想根据强度得到紫色部分或棕色部分。我试过寻找,但找不到任何功能。因为蓝色或任何其他颜色有可能以不同的色调出现。有没有办法让不同颜色的特定阴影掩盖其他区域? k表示影像分割后,有什么功能或方法可以显示影像簇吗? Original Image k表示影像分割后,有什么功能或方法可以显示影像簇吗? K 表示分割后的影像

uj5u.com热心网友回复:

我不确定你想要什么,但如果你想在 Python/OpenCV 中从 kmeans 将每种颜色保存为自己的影像,那么应该这样做。

输入:

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

import cv2
import numpy as np

# read input and convert to range 0-1
image = cv2.imread('jellyfish.png')
h, w, c = image.shape

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

# set number of colors
numcolors = 5
numiters = 10
epsilon = 1
attempts = 10

# do kmeans processing
criteria = (cv2.TERM_CRITERIA_EPS   cv2.TERM_CRITERIA_MAX_ITER, numiters, epsilon)
ret, labels, centers = cv2.kmeans(image_2d, numcolors, None, criteria, attempts, cv2.KMEANS_RANDOM_CENTERS)

# reconstitute 2D image of results
centers = np.uint8(centers)
newimage = centers[labels.flatten()]
newimage = newimage.reshape(image.shape)
cv2.imwrite("jellyfish_kmeans.png", newimage)
cv2.imshow('new image', newimage)
cv2.waitKey(0)

k = 0
for center in centers:
    # select color and create mask
    #print(center)
    layer = newimage.copy()
    mask = cv2.inRange(layer, center, center)

    # apply mask to layer 
    layer[mask == 0] = [0,0,0]
    cv2.imshow('layer', layer)
    cv2.waitKey(0)


    # save kmeans clustered image and layer 
    cv2.imwrite("jellyfish_layer{0}.png".format(k), layer)
    k = k   1

Kmeans 结果:

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

个别颜色:

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

uj5u.com热心网友回复:

我不确定您想做什么,因为根据您的描述,您似乎想要一件事,然后按标题却是完全不同的。但我已经分割了你想要的部分。

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('jelly.png') 
plt.imshow(img)
plt.show()

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 

pixel_vals =img.reshape((-1,3))

pixel_vals = np.float32(pixel_vals)

criteria = (cv2.TERM_CRITERIA_EPS   cv2.TERM_CRITERIA_MAX_ITER,100,0.85)

k= 5

retval, labels, centers = cv2.kmeans(pixel_vals,k,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)


clustered_img = labels.reshape((img.shape[0], img.shape[1]))

clusters_to_0 = [1,2,4]

for c in clusters_to_0:
    clustered_img[clustered_img == c] = -1

clustered_img[clustered_img!=-1] = 1
clustered_img[clustered_img==-1] = 0

clustered_img
plt.imshow(clustered_img)
plt.show()

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

uj5u.com热心网友回复:

我建议另一种方法,将影像转换为 HSV 通道,然后对色调通道进行阈值处理,因为它包含有关颜色色调的信息:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('jelly.png') 

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

(_, th) = cv2.threshold(h, 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

plt.subplot(131)
plt.imshow(img)
plt.title('Original image')
plt.subplot(132)
plt.imshow(h)
plt.title('Hue channels of the HSV color-space')
plt.subplot(133)
plt.imshow(th)
plt.title('Thresholded image')
plt.show()

k表示影像分割后,有什么功能或方法可以显示影像簇吗?

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *