How can divide image with size 1704 x 2272 into 128 x 128 patches?

2 次查看(过去 30 天)
I have image with size 1704 x 2272. I divide the image into 128 x 128 patches. the output y is cell but the column 18 is empty as attached file. pls can u help me to solve that.
My code is
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 128*128 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

回答(2 个)

Walter Roberson
Walter Roberson 2017-1-30
Use mat2cell if you do not need overlap. Use blockproc with a function of @(block) {block.data} if you need overlap, and remember to turn off border trim
  7 个评论
Image Analyst
Image Analyst 2017-1-31
And why should the image size matter? As long as it will fit a 128x128 tile in there, what's the problem? If you need to handle partial tiles, then see the FAQ, like I've been suggesting, for a more general purpose/flexible solution.
Walter Roberson
Walter Roberson 2017-1-31
With no overlap and not assuming complete tiling:
s1 = size(TheImage,1);
s2 = size(TheImage,2);
mat2cell(TheImage, [128 * ones(1, floor(s1/128)), mod(s1,128)], [128 * ones(1, floor(s2/128)), mod(s2,128)], size(TheImage,3))
The expression is a little simpler if you can be sure the images are multiples of 128 on a side:
mat2cell(TheImage, 128 * ones(1, s1/128), 128 * ones(1, s2/128), size(TheImage,3))

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-1-30
It's for non-overlapping blocks. If you need the blocks to overlap instead of being tiled or moving in "jumps" then there is a parameter in blockproc() that can control overlap.

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by