2017-01-30 58 views
0

我有大小爲1704 x 2272的圖像。我通過matlab將圖像分成128 x 128個補丁。輸出y是單元格,但列18作爲附加文件是空的。請問你能幫我解決這個問題嗎?如何將大小爲1704 x 2272的圖像分成128 x 128個補丁?

我的代碼是

 img = imread(filename); 

     img = rgb2gray(img); 

     img=size8cut(img); 
     y=preprocess5(img); 

.................................... .................................................. .................

function Y=size8cut(X) % cut the rim of the image, to make it more fit to be cut into 256*256 patches 

    [m,n] = size(X); 

    if mod(m,8)~=0 

    X=X(1:m-mod(m,8),:); 
    end 

    if mod(n,8)~=0 
    X=X(:,1:n-mod(n,8)); 
    end 
    Y=X; 
end 

.......................... .................................................. ...

function blkim=preprocess5(im) % cut the image into image patch with 128*128 size 

[m n]=size(im); 
i=ceil(m/128); 
j=ceil(n/128); 
blkim=cell(i,j); 
if i-1 ==0     %% no zero dividsion 
overlap_m=0; 
else 
overlap_m=(i*128-m)/(i-1);%compute the overlap between the patches 
end 

    if j-1 ==0 
    overlap_n=0; 
    else 
    overlap_n=(j*128-n)/(j-1); %%% 
    end 
    if mod(overlap_m,8)~=0 
    for count=1:32 
     m=m-8; 
     i=ceil(m/128); 
     overlap_m=(i*128-m)/(i-1); 
     if mod(overlap_m,8)==0 
      break; 
     end 
      end 
     end 
    if mod(overlap_n,8)~=0 
    for count=1:32 
     n=n-8; 
     j=ceil(n/128); 
     overlap_n=(j*128-n)/(j-1); 
     if mod(overlap_n,8)==0 
      break; 
      end 
    end 
end 
im=im(1:m,1:n); 
    for ii=1:i 
    for jj=1:j 
     blkim{ii,jj}=im((128-overlap_m)*(ii-1)+1:(128-overlap_m)*(ii-1)+128, (128-overlap_n)*(jj-1)+1:(128-overlap_n)*(jj-1)+128); 
    end 
end 
return; 
end 
+0

我們不想讀你的代碼*猜測*它應該做什麼。請解釋一下。您如何處理128不能均勻分割圖像尺寸的事實? – Lumen

回答

3

圖片的大小1704 x 2272不能等分爲128x128的塊,因爲尺寸不是128的倍數。

您可能必須忽略或爲其構建單獨的補丁

然而,如果圖像是128整數倍,你可以簡單地使用mat2cellrepmat如下:

GrabPieces = @(X, chunks) mat2cell(X, ... 
     repmat(chunks,[1 size(X,1)/chunks]), ... 
     repmat(chunks,[1 size(X,2)/chunks]) ... 
     ); 

然後做,

P = GrabPieces (img, 128); 
+1

好的變量名! –

相關問題