Sunday 15 September 2013

matlab - How to count the number of overlapping blocks in an Image -



matlab - How to count the number of overlapping blocks in an Image -

i have 512x512 size of image , have made 4x4 overlapping blocks entire image.how can count number of overlapping blocks , save in array in matlab. have done below 4x4 overlapping blocks. how count no of blocks , store using array.

[e f] = size(outimg); l=0; i=2:e-2 j=2:f-2 h =double(outimg((i-1:i+2),(j-1:j+2))); eval(['out_' num2str(l) '=h']); l=l+1 end; end;

from understand question, want know how many blocks of 4x4 can fit in image, , store them.

calculating number of blocks trivial, in code give example, l number of element counted. of course, value deterministic (determined f , e). no need loop on them value of count.

count = (f-3)*(e-3);

if want save values in array (assuming mean here matrix , not cell array) need decide how represent it, can store 4d e-3 x f-3 x 4 x 4 matrix (as @steffen suggested), or 3d 4 x 4 x count matrix, think later more intuitive. in case should assign memory matrix in advance , not on fly:

[e f] = size(outimg); count = (f-3)*(e-3); outmat = zeros(4,4,count); % assign memory matrix l = 0; i=2:e-2 j=2:f-2 l = l + 1; outmat(:,:,l) = double(outimg((i-1:i+2),(j-1:j+2))); end; end;

the number of blocks stored both count , l, calculating count in advance allows assign needed memory in advance, i block stored outmat(:,:,i).

an implementation using 4d matrix be:

[e f] = size(outimg); count = (f-3)*(e-3); outmat = zeros((f-3),(e-3),4,4); % assign memory matrix i=2:e-2 j=2:f-2 outmat(i,j,:,:) = double(outimg((i-1:i+2),(j-1:j+2))); end; end;

in case, l isn't needed , each block (indexed i,j) located @ outmat(i,j,:,:)

regarding cell array vs. matrix, since matrix requires continuous place in memory, may want consider using cell array instead of matrix. 512x512x4 matrix of doubles requires (assuming 8 byte representation) 8mb (512*512*8*4 = 8*1024*1024). if dimensions bigger, or if strapped (continuous) memory cell array may improve solution. can read more difference @ difference between cell , matrix in matlab?.

the implementation similar.

[e f] = size(outimg); count = (f-3)*(e-3); outarray = cell(1,count); l = 0; i=2:e-2 j=2:f-2 l = l + 1; outarray{1,l} = double(outimg((i-1:i+2),(j-1:j+2))); end; end;

matlab

No comments:

Post a Comment