Kmeans

作者: ForgetThatNight | 来源:发表于2018-07-07 10:47 被阅读14次
    # -*- coding: utf-8 -*-
    
    from skimage import io
    from sklearn.cluster import KMeans
    import numpy as np
    
    image = io.imread('test2.jpg')
    io.imshow(image)
    io.show()
    
    rows = image.shape[0]
    cols = image.shape[1]
     
    image = image.reshape(image.shape[0]*image.shape[1],3)
    kmeans = KMeans(n_clusters = 128, n_init=10, max_iter=200)
    kmeans.fit(image)
    
    clusters = np.asarray(kmeans.cluster_centers_,dtype=np.uint8) 
    labels = np.asarray(kmeans.labels_,dtype=np.uint8 )  
    labels = labels.reshape(rows,cols);
    
    print (clusters.shape)
    np.save('codebook_test.npy',clusters)    
    io.imsave('compressed_test.jpg',labels)
    

    输出:(128, 3)

    image = io.imread('compressed_test.jpg')
    io.imshow(image)
    io.show()
    

    相关文章

      网友评论

          本文标题:Kmeans

          本文链接:https://www.haomeiwen.com/subject/cifiuftx.html