3

I want to divide my image into overlapping blocks and process those blocks individually and store the output of each matrix into a matrix.

I have tried using im2col but it is not at all practical. My code is:

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

There are two problems with this, first there is no way to control the overlapping of blocks.

Second the process is very slow and very very memory hungry. I basically ran out of memory on my computer on the third image, even if I clear the previous images.

is there any efficient way to divide my images into overlapping blocks?

P.S. I cannot create a for image for every image as every image is of varying size :(

ipunished
  • 684
  • 2
  • 6
  • 22
  • 2
    Look into [Efficient Implementation of `im2col` and `col2im`](http://stackoverflow.com/a/25454746/3293881) if it helps. Also, what sizes of images are you dealing with? Basically, that neighborhood matrix with `im2col` is huge in itself, so it would make more sense to perform the extraction of features locally and then saving rather than creating the neighborhood matrix and then processing for feature extraction. – Divakar Mar 18 '15 at 19:29
  • @Divakar Thank you, but the question you linked to is the implementation of ``im2col``, I already have the image processing toolbox, also if I am to use that function, how would I control the ``sliding`` overlap window? I agree with you regarding this being very inefficient. But I am trying to find alternatives here. ``blockproc`` would be ideal here but the problem is with output of ``blockproc`` which this question explains my problem exactly: http://stackoverflow.com/questions/29109241/is-there-any-way-to-control-the-concatenation-of-the-blockproc-output – ipunished Mar 18 '15 at 19:42

1 Answers1

3

Listed next is one approach to achieve im2col with additional overlapping capability. It's built upon another Stackoverflow answer on Efficient Implementation ofim2colandcol2im`. Here's the implementation -

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;

Sample run -

>> 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
Community
  • 1
  • 1
Divakar
  • 218,885
  • 19
  • 262
  • 358