我有一个想要分成重叠块的图像。
我将框设置为8行8列,重叠因子为4行/列。
这就是我为解决这个问题所写的:
img = imread('a03-017-05.png');
overlap = 4
count = 1;
for i = 1:overlap:size(img,1)
for j = 1:overlap:size(img,2)
new{count} = img(i:i+8,j:j+8);
count = count+1;
end
end
这可以直到它到达图像的末尾,其中j+8
或i+8
将超出图像的尺寸。有没有办法以最小的数据丢失来避免这种情况?
由于
答案 0 :(得分:2)
如果您只想忽略位于完整子块之外的列/行,则只需从相应的循环范围中减去子块的宽度/高度:
overlap = 4
blockWidth = 8;
blockHeight = 8;
count = 1;
for i = 1:overlap:size(img,1) - blockHeight + 1
for j = 1:overlap:size(img,2) - blockWidth + 1
...
让我们说你的形象是16x16。从列8
开始的块将考虑列的其余部分,因此具有9到16之间的起始索引是没有意义的。
另外,我认为你的例子错误地计算了块大小...你得到了9x9的块。我想你想做的事:
new{count} = img(i:i+blockHeight-1,j:j+blockWidth-1);
例如,在带有上述代码的13x13图像中,您的行索引将为[1, 5]
,并且块的行范围将为1:8
和5:12
。行/列13将被省略。
答案 1 :(得分:1)
我不确定你想要他们如何安排,但我做了一个棋盘图案,即
x [] x [] ...
[] x [] x ...
所以要做到这一点
%The box size
k = 8;
%The overlap
overlap = 4;
%One layer of the checker board
new1 = mat2cell(img, [k*ones(1, floor(size(img,1)/k)), mod(size(img,1), k)], [k*ones(1, floor(size(img,2)/k)), mod(size(img,2), k)]);
%Get the overlap cells
img2 = img(overlap + 1:end - overlap, overlap + 1:end - overlap);
%Create the other part of the checker board
new2 = mat2cell(img2, [k*ones(1, floor(size(img2,1)/k)), mod(size(img2,1), k)], [k*ones(1, floor(size(img2,2)/k)), mod(size(img2,2), k)]);
%They will all end up in new
new = cell(size(new1) + size(new2));
new(1:2:end, 1:2:end) = new1;
new(2:2:end, 2:2:end) = new2;