matlab对图像进行均值滤波_用K均值进行图像分割
個人學習筆記:采用聚類方法對圖像進行分割,以下內容純粹個人理解,如有錯誤請幫我指出!多謝!
圖像分割就是把圖像按照某些條件分成不同的區域,并提取出感興趣的區域。傳統的分割方法包括基于閾值的分割、基于區域的分割、基于邊緣的分割等。當然,本次筆記寫的是采用K均值聚類實現圖像分割。(代碼在文章結尾)
一、理論分析
K均值是一種比較常用的聚類算法,由于并不是本次筆記的重點,因此只進行算法的流程簡單記錄如下:
1、選取K個樣本作為聚類中心
2、計算各個樣本到聚類中心的距離
3、更新均值向量,重復以上步驟
分割算法核心:對圖像的像素進行聚類,相似RGB值的像素被聚到一起,就形成了K個區域!
問題的核心在于如何使用K均值對圖像的像素進行聚類,自己整理了以下的幾個問題:
1、 K均值使用什么做樣本?
K均值聚類就是把圖像的像素點按照“值的接近程度”進行聚類,那么就會給每個像素點打標簽(把每個位置R、G、B值看作是一個樣本,R、G、B就是三個特征),有多少像素值(R、G、B算一個像素值)就有多少個樣本。
2、 K均值得到的標簽含義是什么?
K均值得到的結果標簽就是width*height個0,1,2這種值,因為圖像提供的樣本數就是width*height個,也就是對每個位置的像素點打標簽。含義就是亮度接近的像素標簽值相同。
3、K均值聚類以后如何得到新的聚類圖像
像素點的標簽值表示它屬于哪個類,后續使用標簽值作為亮度值。如果是聚類結果為3, 那么標簽值就有0,1,2三個,標簽的shape是width*height,那么就是用0,1,2填充一張np.zeros(width, height)圖像,就得到了最后的聚類結果。
標簽值相同的位置因此也就亮度相同,得到的聚類圖像僅僅包含k個亮度種類,比如K=3,那么聚類圖像就只有三種亮度值
實現代碼里面未解決的Bug:因為標簽值0,1,2代表的僅僅是三個不同區域,但是沒辦反設定哪個區域是2(最亮的區域),也就是最后合成的圖像哪個區域亮沒辦反控制(比如人臉區域應該更亮),只能多運行幾次程序。
二:代碼實現
算法流程分為以下四步:
1、讀取圖片
2、將圖片像素轉為樣本形式
3、對樣本進行聚類
4、創建空白圖像
5、使用聚類結果對空白圖像進行填充
6、保存聚類得到的標簽圖
步驟1和2:
# 1:read image上述代碼塊的#1負責讀取圖片,#2負責把(w,h,3)的圖像轉為(w*h,3)的數據,每行就是一個樣本,每個樣本包含R、G、B三個特征值。
步驟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])聚類就是對上述的像素值進行聚類
步驟4:
# 4: create a image containercontainer = np.zeros(shape=(image.shape[0], image.shape[1]))創建的像素值全為0的空白圖像,用于存儲聚類標簽。
步驟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將聚類標簽值cls[i, j]乘以60作為亮度值(乘以70或者任何值都行,只要保證K*任何值不超過255)
步驟6:
# 6: saver the cluster imagecontainer = container.astype(np.uint8) imageio.imsave(save_name, container)保存的時候一定要轉換為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")對標題上費雯麗的照片進行聚類結果如下:
個人認為:K如果選擇為2,那么圖片視覺上看就應該包含“2種”明顯不同的區域;K如果選擇為3,那么圖片中就應該包含3種明顯不同的區域。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的matlab对图像进行均值滤波_用K均值进行图像分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python调用r语言_【Python调
- 下一篇: matlab 工业相机 曝光时间_机器视