matlab对图像进行均值滤波_用K均值进行图像分割
個(gè)人學(xué)習(xí)筆記:采用聚類方法對(duì)圖像進(jìn)行分割,以下內(nèi)容純粹個(gè)人理解,如有錯(cuò)誤請(qǐng)幫我指出!多謝!
圖像分割就是把圖像按照某些條件分成不同的區(qū)域,并提取出感興趣的區(qū)域。傳統(tǒng)的分割方法包括基于閾值的分割、基于區(qū)域的分割、基于邊緣的分割等。當(dāng)然,本次筆記寫的是采用K均值聚類實(shí)現(xiàn)圖像分割。(代碼在文章結(jié)尾)
一、理論分析
K均值是一種比較常用的聚類算法,由于并不是本次筆記的重點(diǎn),因此只進(jìn)行算法的流程簡(jiǎn)單記錄如下:
1、選取K個(gè)樣本作為聚類中心
2、計(jì)算各個(gè)樣本到聚類中心的距離
3、更新均值向量,重復(fù)以上步驟
分割算法核心:對(duì)圖像的像素進(jìn)行聚類,相似RGB值的像素被聚到一起,就形成了K個(gè)區(qū)域!
問題的核心在于如何使用K均值對(duì)圖像的像素進(jìn)行聚類,自己整理了以下的幾個(gè)問題:
1、 K均值使用什么做樣本?
K均值聚類就是把圖像的像素點(diǎn)按照“值的接近程度”進(jìn)行聚類,那么就會(huì)給每個(gè)像素點(diǎn)打標(biāo)簽(把每個(gè)位置R、G、B值看作是一個(gè)樣本,R、G、B就是三個(gè)特征),有多少像素值(R、G、B算一個(gè)像素值)就有多少個(gè)樣本。
2、 K均值得到的標(biāo)簽含義是什么?
K均值得到的結(jié)果標(biāo)簽就是width*height個(gè)0,1,2這種值,因?yàn)閳D像提供的樣本數(shù)就是width*height個(gè),也就是對(duì)每個(gè)位置的像素點(diǎn)打標(biāo)簽。含義就是亮度接近的像素標(biāo)簽值相同。
3、K均值聚類以后如何得到新的聚類圖像
像素點(diǎn)的標(biāo)簽值表示它屬于哪個(gè)類,后續(xù)使用標(biāo)簽值作為亮度值。如果是聚類結(jié)果為3, 那么標(biāo)簽值就有0,1,2三個(gè),標(biāo)簽的shape是width*height,那么就是用0,1,2填充一張np.zeros(width, height)圖像,就得到了最后的聚類結(jié)果。
標(biāo)簽值相同的位置因此也就亮度相同,得到的聚類圖像僅僅包含k個(gè)亮度種類,比如K=3,那么聚類圖像就只有三種亮度值
實(shí)現(xiàn)代碼里面未解決的Bug:因?yàn)闃?biāo)簽值0,1,2代表的僅僅是三個(gè)不同區(qū)域,但是沒辦反設(shè)定哪個(gè)區(qū)域是2(最亮的區(qū)域),也就是最后合成的圖像哪個(gè)區(qū)域亮沒辦反控制(比如人臉區(qū)域應(yīng)該更亮),只能多運(yùn)行幾次程序。
二:代碼實(shí)現(xiàn)
算法流程分為以下四步:
1、讀取圖片
2、將圖片像素轉(zhuǎn)為樣本形式
3、對(duì)樣本進(jìn)行聚類
4、創(chuàng)建空白圖像
5、使用聚類結(jié)果對(duì)空白圖像進(jìn)行填充
6、保存聚類得到的標(biāo)簽圖
步驟1和2:
# 1:read image上述代碼塊的#1負(fù)責(zé)讀取圖片,#2負(fù)責(zé)把(w,h,3)的圖像轉(zhuǎn)為(w*h,3)的數(shù)據(jù),每行就是一個(gè)樣本,每個(gè)樣本包含R、G、B三個(gè)特征值。
步驟3:
# 3: cluster, I thought: give every pixel (that in orignal image)# a label , so the label have same shape as image(gray)cls = KMeans(n_clusters=k_cluster).fit_predict(data)cls = cls.reshape(image.shape[0], image.shape[1])聚類就是對(duì)上述的像素值進(jìn)行聚類
步驟4:
# 4: create a image containercontainer = np.zeros(shape=(image.shape[0], image.shape[1]))創(chuàng)建的像素值全為0的空白圖像,用于存儲(chǔ)聚類標(biāo)簽。
步驟5:
# 5: use cluster labels as "gray value" # and fill it into aimage containerfor i in range(image.shape[0]):for j in range(image.shape[1]):# cls[i, j]*30 ,because label value is 0, 1, 2# the bright difference betwwen labels is to smallcontainer[i, j] = cls[i, j]*60將聚類標(biāo)簽值cls[i, j]乘以60作為亮度值(乘以70或者任何值都行,只要保證K*任何值不超過(guò)255)
步驟6:
# 6: saver the cluster imagecontainer = container.astype(np.uint8) imageio.imsave(save_name, container)保存的時(shí)候一定要轉(zhuǎn)換為uint8編碼
完整的代碼如下:
import numpy as np import imageio from sklearn.cluster import KMeansdef image_cluster(image_name, save_name, k_cluster=3):"""cluster by KMeans for RGB image"""# 1:read imageimage = imageio.imread(image_name)# 2: convert (w, h, 3) into (w*h, 3)# R,G and B combine as 3 features # data will be a 2D matrix, each row have 3 values(R/G/B),# and each column has width*height values# this operation convert 3D to 2D, like reshape image2matrix = []for i in range(image.shape[0]):for j in range(image.shape[1]):r_v, g_v, b_v = image[i, j]image2matrix.append([r_v/255.0, g_v/255.0, b_v/255.0])data = np.mat(image2matrix)# 3: cluster, I thought: give every pixel (that in orignal image)# a label , so the label have same shape as image(gray)cls = KMeans(n_clusters=k_cluster).fit_predict(data)cls = cls.reshape(image.shape[0], image.shape[1])# 4: create a image containercontainer = np.zeros(shape=(image.shape[0], image.shape[1]))# 5: use cluster labels as "gray value" # and fill it into aimage containerfor i in range(image.shape[0]):for j in range(image.shape[1]):# cls[i, j]*30 ,because label value is 0, 1, 2# the bright difference betwwen labels is to smallcontainer[i, j] = cls[i, j]*60# 6: saver the cluster imagecontainer = container.astype(np.uint8) imageio.imsave(save_name, container)return Trueimage_cluster("data/vivian.jpg", "results/cluster.jpg")對(duì)標(biāo)題上費(fèi)雯麗的照片進(jìn)行聚類結(jié)果如下:
個(gè)人認(rèn)為:K如果選擇為2,那么圖片視覺上看就應(yīng)該包含“2種”明顯不同的區(qū)域;K如果選擇為3,那么圖片中就應(yīng)該包含3種明顯不同的區(qū)域。
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的matlab对图像进行均值滤波_用K均值进行图像分割的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python调用r语言_【Python调
- 下一篇: laravel改代码没变化_推荐10个优