如何将尺寸为1704 x 2272的图像分成128 x 128个补丁?

时间:2017-01-30 12:11:46

标签: matlab

我的图像大小为1704 x 2272.我通过matlab将图像分成128 x 128个图像。输出y是单元格,但列18作为附加文件是空的。你可以帮我解决这个问题。

我的代码是

       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

1 个答案:

答案 0 :(得分:3)

尺寸为promise的图片不能平均分为128x128的块,因为尺寸不是128的倍数。

您可能必须忽略或为其形成单独的补丁

但是,如果图片是128的完全倍数,您只需将1704 x 2272mat2cell一起使用,如下所示:

repmat

然后呢,

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