有没有更有效的方法将我的图像分成重叠的块?

Is there a more efficient way to to divide my image into overlapping blocks?

我想将我的图像分成重叠的块并分别处理这些块并将每个矩阵的输出存储到一个矩阵中。

我试过使用 im2col 但它一点也不实用。我的代码是:

kek = im2col(images_m{1}, [64 64], 'sliding');
for i = 1: size(kek, 2)
    new = reshape(kek(:,i), [64 64]);
    %Extract features from the new block and save it in a concatenating
    %matrix
end

这样做有两个问题,首先没有办法控制块的重叠。

其次,该过程非常缓慢并且非常耗费内存。我基本上 运行 在我的计算机上第三张图片内存不足,即使我 clear 以前的图片。

有什么有效的方法可以将我的图像分成重叠的块吗?

P.S。我无法为每张图片创建一个 for image,因为每张图片的大小都不同 :(

接下来列出的是实现 im2col with additional overlapping capability. It's built upon another Whosebug answer on Efficient Implementation ofim2colandcol2im` 的一种方法。这是实现 -

function out = im2col_sliding_overlap(A,blocksize,overlap)

%// Get size of A for later usages
[M,N] = size(A);

%// Number of blocks to be formed along X-Y directions
num_blks_x = (M - overlap)/(blocksize(1)-overlap);
num_blks_y = (N - overlap)/(blocksize(2)-overlap);

%// Store blocksize as number of rows and columns information
nrows_blk = blocksize(1);
ncols_blk = blocksize(2);

%// Start indices for each block
start_ind = bsxfun(@plus,[0:num_blks_x-1]'*(nrows_blk-overlap)+1,...
                       [0:num_blks_y-1]*(ncols_blk - overlap)*M); %//'

%// Block offset indices
blkoffset_idx = bsxfun(@plus,[0:nrows_blk-1]',[0:ncols_blk-1]*M); %//'

%// Indices for all blocks
idx = bsxfun(@plus,blkoffset_idx(:),start_ind(:).');              %//'

%// Index into A to have the final output
out = A(idx);

return;

样本运行-

>> A
A =
    0.6293    0.3797    0.8972    0.4471    0.1332    0.7758
    0.0207    0.6994    0.8347    0.6550    0.7619    0.6992
    0.5167    0.0107    0.9401    0.4059    0.7560    0.3019
    0.9483    0.1728    0.0323    0.8118    0.5423    0.3186
    0.6692    0.7135    0.5497    0.5216    0.9695    0.3097
    0.8801    0.1210    0.0402    0.7342    0.1006    0.4542

>> out = im2col_sliding_overlap(A,[4 4],2) %// Blocksize=[4 4], Overlap =2
out =
    0.6293    0.5167    0.8972    0.9401
    0.0207    0.9483    0.8347    0.0323
    0.5167    0.6692    0.9401    0.5497
    0.9483    0.8801    0.0323    0.0402
    0.3797    0.0107    0.4471    0.4059
    0.6994    0.1728    0.6550    0.8118
    0.0107    0.7135    0.4059    0.5216
    0.1728    0.1210    0.8118    0.7342
    0.8972    0.9401    0.1332    0.7560
    0.8347    0.0323    0.7619    0.5423
    0.9401    0.5497    0.7560    0.9695
    0.0323    0.0402    0.5423    0.1006
    0.4471    0.4059    0.7758    0.3019
    0.6550    0.8118    0.6992    0.3186
    0.4059    0.5216    0.3019    0.3097
    0.8118    0.7342    0.3186    0.4542