ipunished
ipunished

Reputation: 694

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

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 :(

Upvotes: 3

Views: 1222

Answers (1)

Divakar
Divakar

Reputation: 221564

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

Upvotes: 3

Related Questions