我想将图像划分为重叠的块,并单独处理这些块,并将每个矩阵的输出存储到一个矩阵中。
我尝试过使用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
之前的图像。
有没有有效的方法将我的图像分成重叠的块?
附:我无法为每个图像创建一个 for 图像,因为每个图像的大小都不同:(
接下来列出的是一种实现方法im2col http://in.mathworks.com/help/images/ref/im2col.html加上额外的overlapping
能力。它是建立在另一个 Stackoverflow 答案之上的Efficient Implementation ofim2colandcol2im` https://stackoverflow.com/a/25454746/3293881。这是实现 -
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)