基於k-均值聚類的影象分割
1.知識儲備
1.0 window系統截圖(當前視窗)
Alt + PrintScreen
1.1 Matlab中 K-means聚類函式
[Idx,Ctrs,SumD,D] = kmeans(X,K,'Replicates',3,'Options',opts); %呼叫Kmeans函式 %X N*P的資料矩陣 % K:聚類數目 %Idx N*1的向量,儲存的是每個點的聚類標號 %Ctrs K*P的矩陣,儲存的是K個聚類質心位置 %SumD 1*K的和向量,儲存的是類間所有點與該類質心點距離之和 %D N*K的矩陣,儲存的是每個點與所有質心的距離; [Idx,Ctrs,SumD,D] = kmeans(X,K,'Replicates',3,'Options',opts);
1.2註釋
在MATLAB中可以在行首部加%來進行註釋,加%%+空格來進行劃分不同的程式段。
但在進行多行註釋時,針對單行進行操作過於麻煩,可以利用快捷鍵進行多行註釋。
可以先選中需要註釋的行,然後按“ctrl+R”進行註釋,
如果要取消多行註釋,則在選中的基礎上,按“ctrl+T”。
1.3 matlab判斷資料的型別
用class(var)指令獲取矩陣型別
1.4 matlab讀取.mat檔案matlab將struct和cell轉換成matrices
之前將陣列或者矩陣儲存為一個mat格式的檔案,在進行load命令讀取時:
bank = load('filterBank.mat');
得到的bank是struct型別的資料,而想要的是一個矩陣或者陣列。
將命令:
bank = load('filterBank.mat');
改寫為:
bank = cell2mat(struct2cell(load('filterBank.mat')));
得到的bank即為矩陣。
1.5matlab清空控制檯
命令語句: clc
1.6matlab二維矩陣輸出彩色影象
imagesc(colorLabelIm)
colorbar
1.7matlab畫圖
data = randn(4,100); figure(1); subplot(221);plot(data(1,:)); subplot(222);plot(data(2,:)); subplot(223);plot(data(3,:)); subplot(224);plot(data(4,:)); suptitle('總圖示題');
2. segmentMain.m
origIm4 =imread('E:\桌面\1研究生相關\研一上\機器視覺基礎\Proj1-Texture\Texture\a2-code-data\dress.jpg'); origIm1 =imread('E:\桌面\1研究生相關\研一上\機器視覺基礎\Proj1-Texture\Texture\a2-code-data\circle-im-1.jpg'); origIm2 =imread('E:\桌面\1研究生相關\研一上\機器視覺基礎\Proj1-Texture\Texture\a2-code-data\butterfly.jpg'); origIm3 =imread('E:\桌面\1研究生相關\研一上\機器視覺基礎\Proj1-Texture\Texture\a2-code-data\circle-im-2.jpg'); origIm =imread('E:\桌面\1研究生相關\研一上\機器視覺基礎\Proj1-Texture\Texture\a2-code-data\gumballs.jpg'); %[width,height] = size(origIm); [height,width] = size(origIm); subplot(1,3,1); imshow(origIm) title('原影象'); %bank = load('filterBank.mat'); bank = cell2mat(struct2cell(load('filterBank.mat'))); str =class(bank);%判斷bank型別disp(str); imStack = {rgb2gray(origIm)} ;% %imStack = {rgb2gray(origIm),rgb2gray(origIm1),rgb2gray(origIm2),rgb2gray(origIm3),rgb2gray(origIm4)}; % disp(bank); %列印過濾器 [m,n,l] =size(bank); bankNum1 = size(bank, 1); bankNum2 = size(bank, 2); bankNum = size(bank, 3); disp(bankNum); % disp(bankNum1); % disp(m); % disp(n); % disp(l); winSize = 10; numColorRegions =10;% 聚類數 numTextureRegions = 50 ;% 聚類數 %bank =ones(5,6,7); %textons_x = 600; %textons =ones(textons_x,bankNum); k = 100;%紋理基元的紋理編碼集個數 textons = createTextons(imStack, bank, k); %height = 100 ; %winSize =ones(height,height); %colorLabelIm = ones(height, width) ; %textureLabelIm =ones(height, width); [colorLabelIm, textureLabelIm] = compareSegmentations(origIm, bank, textons, winSize, numColorRegions, numTextureRegions); % disp(colorLabelIm); [mc,nc,lc] =size(colorLabelIm); [mt,nt,lt] =size(textureLabelIm); fprintf('row is %d,col is %d,%d ',mc,nc,lc); fprintf('row is %d,col is %d,%d ',mt,nt,lt); subplot(1,3,2); %imshow(uint8(colorLabelIm));% 輸出為黑白圖 x2=[150+width,350+2*width]; y2=[50+height,50+2*height]; imagesc(x2,y2,colorLabelIm) colorbar %colorbar('position',[0.32 0.01 0.3 0.5]) title('顏色分割'); subplot(1,3,3); %imshow(uint8(textureLabelIm));% 輸出為黑白圖 x3=[250+2*width,450+3*width]; y3=[50+2*height,50+3*height]; imagesc(x3,y3,textureLabelIm) %imagesc(textureLabelIm) colorbar%定義圖例位置大小 title('紋理分割'); %suptitle('影象名:gumballs.jpg濾波器: filterBank條件:winSize=%dnColor=%dnTexture=%dtexttonsNum=%d ',winSize,numColorRegions,numTextureRegions,k); suptitle('影象名:gumballs.jpg濾波器: filterBank條件:winSize=nColor=nTexture=texttonsNum= ') % imshow(colorLabelIm,[]);% 輸出為黑白圖
2.1 精簡改良版
3.錯誤解析
3.1 Error using conv2 N-D arrays are not supported
For color images, imread returns a 3D array where the 3rd dimension has 3 elements for R, G, B. You need to convert the image to grey-value first. Or do the convolution on the color image using convn.
rgb2gray(origIm);
參考資料
[0]ofollow,noindex">基於k-均值聚類的影象分割
[3]matlab—load命令讀的資料為struct型別的資料的處理方法
[4]struct2cell