Python實現K-means演算法的顏色量化
The aim of color clustering is to produce a small set of representative colors that capture the color properties of an image. Using the small set of color found by the clustering, a quantization process can be applied to the image to find a new version of the image that has been "simplified," both in colors and shapes.
In this post we will see how to use the K-Means algorithm to perform. color clustering and how to apply the quantization. Let's see the code:
02.from numpy import reshape,uint8,flipud
03.from scipy.cluster.vq import kmeans,vq
04.
05.img = imread('clearsky.jpg')
06.
07.# reshaping the pixels matrix
08.pixel = reshape(img,(img.shape[0]*img.shape[1],3))
09.
10.# performing the clustering
11.centroids,_ = kmeans(pixel,6) # six colors will be found
12.# quantization
13.qnt,_ = vq(pixel,centroids)
14.
15.# reshaping the result of the quantization
16.centers_idx = reshape(qnt,(img.shape[0],img.shape[1]))
17.clustered = centroids[centers_idx]
18.
19.figure(1)
20.subplot(211)
21.imshow(flipud(img))
22.subplot(212)
23.imshow(flipud(clustered))
24.show()
In this post we will see how to use the K-Means algorithm to perform. color clustering and how to apply the quantization. Let's see the code:
CODE:
01.from pylab import imread,imshow,figure,show,subplot02.from numpy import reshape,uint8,flipud
03.from scipy.cluster.vq import kmeans,vq
04.
05.img = imread('clearsky.jpg')
06.
07.# reshaping the pixels matrix
08.pixel = reshape(img,(img.shape[0]*img.shape[1],3))
09.
10.# performing the clustering
11.centroids,_ = kmeans(pixel,6) # six colors will be found
12.# quantization
13.qnt,_ = vq(pixel,centroids)
14.
15.# reshaping the result of the quantization
16.centers_idx = reshape(qnt,(img.shape[0],img.shape[1]))
17.clustered = centroids[centers_idx]
18.
19.figure(1)
20.subplot(211)
21.imshow(flipud(img))
22.subplot(212)
23.imshow(flipud(clustered))
24.show()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-739423/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 實現隨機顏色隨機
- 交替顏色列表實現
- Swift 實現更改圖片的顏色Swift
- 利用顏色實現的口令程式(轉)
- 機器學習之k-means聚類演算法(python實現)機器學習聚類演算法Python
- K-means 在 Python 中的實現Python
- python實現之 K-means演算法簡單介紹Python演算法
- K-Means演算法的程式碼實現(Java)演算法Java
- js實現的十六進位制顏色和RGB顏色值的相互轉換JS
- css文字顏色漸變的3種實現CSS
- python 輸出顏色Python
- 聚類演算法與K-means實現聚類演算法
- js實現的點選連結<a>實現切換背景顏色JS
- canvas實現動態替換人物的背景顏色Canvas
- 滑鼠移動到button顏色改變的實現
- CSS實現的背景圖片替代顏色程式碼CSS
- 美顏SDK一鍵美顏的演算法實現流程演算法
- python帶顏色輸出Python
- 【模型推理】量化實現分享三:詳解 ACIQ 對稱量化演算法實現模型演算法
- 得到一張圖片或logo的主要顏色(顏色趨向)python版GoPython
- app直播原始碼,xml實現由上而下的顏色漸變APP原始碼XML
- android關鍵字特殊顏色顯示的實現Android
- 在asp.net中ListView的交替背景顏色實現ASP.NETView
- 如何快速實現一個顏色選擇器
- 殘缺棋盤 android實現顏色填充Android
- 自繪按鈕實現顏色選擇器
- 設定toast的字型顏色和背景顏色AST
- Python中使用K-means演算法Python演算法
- 兩行 CSS 程式碼實現圖片任意顏色賦色技術CSS
- [譯] Android 實現顏色漸變的一個小 tipAndroid
- Android 沉浸式狀態列 漸變顏色的實現Android
- 美顏SDK更換髮色、染髮功能的實現流程
- echart使用自定義單個柱狀顏色實現
- Flutter 實現“斑馬紋”背景(需要變換顏色)Flutter
- 0x04_My-OS實現自定義顏色
- [SVG]修改固定顏色為填充顏色SVG
- HTML 顏色色號HTML
- python--顏色的RGB轉BGR(opencv)PythonOpenCV