OpenCV?圖像分割實現(xiàn)Kmean聚類的示例代碼
1 Kmean圖像分割
按照Kmean原理,對圖像像素進行聚類。
優(yōu)點:此方法原理簡單,效果顯著。
缺點:實踐發(fā)現(xiàn)對于前景和背景顏色相近或者顏色區(qū)分度差的圖像效果不顯著。
本文對圖像進行濾波,主要是為了消除樹枝顏色的影響(濾波為非Keman圖像分割的必要操作)。
2 流程
(1)讀入圖片,把圖片轉(zhuǎn)化為二維。
(2)根據(jù)Kmean算法對圖像分割,返回類別標簽和各類別中心點。
(3)根據(jù)類別標簽復(fù)制各類別中心點得到結(jié)果,在對結(jié)果調(diào)整到原有尺度。
3 實現(xiàn)
(1)圖像分割前添加濾波,消除噪聲
## 1 圖像分割--Keman聚類 import cv2 import numpy as np import matplotlib.pyplot as plt # 1 讀入圖片 img0 = cv2.imread('bird.png', 1) # (548,727,3) img0 = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB) img_ = cv2.GaussianBlur(img0, (13, 13), 10, 10) h, w, c = img_.shape img_blur = img_.reshape([-1, 3]) img_blur = np.float32(img_blur) # 2 分類 criteria = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) num_clusters = 2 _, label, center_color = cv2.kmeans(img_blur, num_clusters, None, criteria, num_clusters, cv2.KMEANS_RANDOM_CENTERS) center_color = np.uint8( center_color) # img_blur[398396,3],label[398396,1],center[2,3] res = center_color[label.ravel()] # [398396,3] res = res.reshape([h, w, c]) # res[668044,3]--> [548,727,3] # 3 顯示 plt.subplot(131) plt.title('origin') plt.imshow(img0) plt.subplot(132) plt.title('img_blur') plt.imshow(img_) plt.subplot(133) plt.title('result') plt.imshow(res) plt.show()
(2)顏色區(qū)分低的情況
當顏色區(qū)分低時,劃分較少的種類,可以達到滿意效果。
## 1 圖像分割--Keman聚類 import cv2 import numpy as np import matplotlib.pyplot as plt # 1 讀入圖片 img = cv2.imread('luna.png', 1) # (548,727,3) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) h, w, c = img.shape img0= img.reshape([-1, 3]) img0 = np.float32(img0) # 3 分類 criteria = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) num_clusters = 2 _, label, center_color = cv2.kmeans(img0, num_clusters, None, criteria, num_clusters, cv2.KMEANS_RANDOM_CENTERS) center_color = np.uint8( center_color) # img_blur[398396,3],label[398396,1],center[2,3] res = center_color[label.ravel()] # [398396,3] res = res.reshape([h, w, c]) # res[668044,3]--> [548,727,3] plt.subplot(121) plt.title('origin') plt.imshow(img) plt.subplot(122) plt.title('result') plt.imshow(res) plt.show()
注: 可以改變中心點的數(shù)值,調(diào)整分割后圖像的顏色。
center_color = np.uint8( center_color) ## 調(diào)整顯示顏色 center_color[0]=[0,0,255] center_color[1]=[255,0,0] res = center_color[label.ravel()]
到此這篇關(guān)于OpenCV 圖像分割實現(xiàn)Kmean聚類的示例代碼的文章就介紹到這了,更多相關(guān)OpenCV Kmean聚類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python調(diào)用百度AI接口實現(xiàn)人流量統(tǒng)計
這篇文章主要介紹了python調(diào)用百度AI接口實現(xiàn)人流量統(tǒng)計,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Python Dask庫處理大規(guī)模數(shù)據(jù)集的強大功能實戰(zhàn)
Dask是一個靈活、開源的Python庫,專為處理大規(guī)模數(shù)據(jù)集而設(shè)計,與傳統(tǒng)的單機計算相比,Dask能夠在分布式系統(tǒng)上運行,有效利用集群的計算資源,本文將深入介紹Dask的核心概念、功能和實際應(yīng)用,通過豐富的示例代碼展示其在大數(shù)據(jù)處理領(lǐng)域的強大能力2023-12-12詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件
這篇文章主要介紹了詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python實現(xiàn)將Excel內(nèi)容插入到Word模版中
前段時間因為需要處理一大堆驗收單,都是一些簡單的復(fù)制粘貼替換工作,于是就想到用python進行處理。本文分享了用python將excel文件單元格內(nèi)容插入到word模版中并保存為新文件的辦法,希望對大家有所幫助2023-03-03Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯誤問題及解決方法
這篇文章主要介紹了Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯誤,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10