Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用K-Means聚类并重塑成彩色图像_Python_Image_Cluster Analysis_K Means_Cv2 - Fatal编程技术网

Python 用K-Means聚类并重塑成彩色图像

Python 用K-Means聚类并重塑成彩色图像,python,image,cluster-analysis,k-means,cv2,Python,Image,Cluster Analysis,K Means,Cv2,我在灰度图像上应用K-Means聚类,希望得到一幅彩色图像,其中每种颜色都分配给一个唯一的聚类。我该怎么做 我的代码是: import numpy as np import cv2 from sklearn.cluster import KMeans import matplotlib.pyplot as plt # this not work it show a black image image = cv2.imread('/Users/myname/Downloads/under1.pn

我在灰度图像上应用K-Means聚类,希望得到一幅彩色图像,其中每种颜色都分配给一个唯一的聚类。我该怎么做

我的代码是:

import numpy as np
import cv2
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# this not work it show a black image
image = cv2.imread('/Users/myname/Downloads/under1.png')

median = cv2.medianBlur(image,3)

x,y,z = image.shape
xm,ym,zm = median.shape

image1 = median.reshape((median.shape[0]*median.shape[1],3))

# For shapefile
xs, ys, zs = np.meshgrid(
    np.linspace(0, 1, xm), # x
    np.linspace(0, 1, ym), # y
    np.linspace(0, 1, zm) # z
)

data_with_coordinates = np.column_stack([
    median.flatten(),
    xs.flatten(),
    ys.flatten()
])


n_cluster = 4

clt = KMeans(n_clusters=n_cluster)
clt.fit(image1)

cluster_centers = clt.cluster_centers_
cluster_labels = clt.labels_

labels = clt.predict(data_with_coordinates)

x,y,z = image.shape

clustered = (cluster_centers[cluster_labels]).astype(np.uint8).reshape(x, y, z)

plt.imshow(labels.reshape(median.shape))

# vedere i cluster come sono:

cv2.imwrite("ReshapedLabelRaster.png",cluster_labels.reshape(x,y))

plt.imsave('BatimetryClusteredColor' + str(n_cluster) + 'C.png',cluster_labels.reshape(x,y), cmap=plt.cm.nipy_spectral)

plt.show()
编辑: 我试图使用您的代码为我的项目,但我没有得到好的结果。


现在,我将尝试了解是否有可能获得一个形状文件或一个边缘位于集群之间的图像。

首先,您需要学习opencv python

输入

输出

这是我的代码:

import numpy as np
import cv2
from matplotlib import pyplot as mp
from sklearn.cluster import KMeans

# 0 means read gray-scale image
img = cv2.imread("1.jpg", 0)
cv2.imwrite("input_gray.png", img)
save_name="output.png"
h, w = img.shape
trans_img = [[i, j, img[i, j]] for i in range(h) for j in range(w)]

# 300 iters * pixels, very slow
kmeans = KMeans(n_clusters=12).fit(trans_img) 

trans_img_tag = kmeans.predict(trans_img)

print(kmeans.cluster_centers_)

img_process = np.zeros((h,w,3),dtype="uint8")

for i,e in enumerate(trans_img_tag):
    x, y = divmod(i, w)
    r,g,b = (e&4)/4,(e&2)/2,e&1
    if e&8:
        r,g,b = 0.5, g, b/2
    img_process[x, y]=r*255,g*255,b*255

cv2.imwrite(save_name,img_process,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) #quality 100
cv2.imshow(save_name,img_process)
k = cv2.waitKey(0)
if k==ord('\x1b'): #esc exit
    cv2.destroyAllWindows()

您的模块cv2从哪里来?