Matlab:将图像分割成重叠块



我有一个大小为[M,N]的图像,我想将其分成大小为[rr,cc]overlapping块。每个块被yyxx像素移动。下面的代码完成了这项工作。有更有效的方法吗?例如,避免for循环?我找到的解决方案方法#1或方法#2主要用于non-overlapping块。

1 <<p> 溶胶/strong>
Im = imread('cameraman.tif');
[M,N,~] = size(Im);
rr = 64; cc = 64; xx = 32; yy = 32;
numBlocksYY = numel(1:rr-xx:(M-(rr-1)));
numBlocksXX = numel(1:cc-yy:(N-(cc-1)));
[numBlocksYY, numBlocksXX]
C = cell(numBlocksYY*numBlocksXX,1);
counter = 1;
for ii=1:rr-xx:(M-(rr-1))
    for jj=1:cc-yy:(N-(cc-1))
        fprintf('[%d:%d, %d:%d]n',ii,ii+rr-1,jj,jj+cc-1);
        C{counter} =  Im(ii:(ii+rr-1), jj:(jj+cc-1), : );
        counter = counter + 1;
    end
    fprintf('n');
end
figure;
for ii=1:numBlocksYY*numBlocksXX
    subplot(numBlocksYY,numBlocksYY,ii), imagesc( C{ii} ); axis image; colormap gray;
end

溶胶2 受到本文中提出的一些解决方案的启发,我试图使用ndgrid来解决这个问题,但是我后来如何使用XXYY索引来填充输出cell C和访问子图像呢?我也很好奇是否有其他的解决方案:-)?

[YY,XX]=ndgrid( 1:(rr-xx):(M-(rr-1)) , 1:(cc-yy):(N-(cc-1)));

SOL 2中给出的XXYY可以得到cell C的输出:

% indices of the first rr x cc block
IDX1 = bsxfun(@plus, (1:rr)', ((1:cc)-1)*M);
% offset in indices for each block
offset = (XX(:)-1) + (YY(:)-1)*M;
% indices of each block, the block is indexed with the 3rd dimension
IDX = bsxfun(@plus, IDX1, reshape(offset, [1 1 numel(offset)]));
% convert to cell of blocks
C = mat2cell(Im(IDX), rr, cc, ones(1, numel(XX)));