我有一张尺寸为1704x2272的图片。我用matlab将图像分成128x128个小块。输出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发布于 2017-01-30 20:28:05
大小为1704 x 2272的图像不能平均划分为128x128的块,因为尺寸不是128的倍数。
您可能必须忽略它,或者为它形成一个单独的补丁。
但是,如果图像是128的精确倍数,您可以简单地将mat2cell与repmat一起使用,如下所示:
GrabPieces = @(X, chunks ) mat2cell( X, ...
repmat(chunks,[1 size(X,1)/chunks]), ...
repmat(chunks,[1 size(X,2)/chunks]) ...
);那就这么做,
P = GrabPieces ( img, 128 );https://stackoverflow.com/questions/41935638
复制相似问题