我想将图像划分为重叠的块并单独处理这些块并将每个矩阵的输出存储到矩阵中。
我尝试过使用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,因为每个图像都有不同的大小:(
答案 0 :(得分:2)
下一步列出了一种实现im2col
并具有额外overlapping
功能的方法。它基于Efficient Implementation of
im2coland
col2im`上的另一个Stackoverflow答案。这是实施 -
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