matlab - How to rearrange matrix column into block using for loop? -
i working on project have used image size (512x512)then have divided whole image 8 there 64x64 block have rearranged each 8x8 image patch single column , new size 64x4069. want original size i.e 512x512,please me how using loop instead of 'col2im'
a=imread('lena.png'); b=double(a); [m,n] = size(b); bl=8; br=m/bl; bc=n/bl; out = zeros(size(reshape(b,bl*bl,[]))); count = 1; = 1:br j= 1:bc block = b((j-1)*bl + 1:(j-1)*bl + bl, (i-1)*bl + 1:(i-1)*bl + bl); out(:,count) = block(:); count = count + 1; end end
this script written backwards. assumptions have made, because not every rectangular matrix can arise process described, , because dimensions of original matrix cannot uniquely determined set of blocks. so, assume original matrix square one, , blocks squares well.
by way, in code use j in formula row indices , columns; assumed mistake, , amended below.
out = kron((0:3)', 1:16); % testing; have 64x4096 matrix here [m,n] = size(out); osize = sqrt(n*m); bl = sqrt(m); br = osize/bl; bc = br; original = zeros(osize); count = 1; = 1:br j = 1:bc block = zeros(bl); block(:) = out(:,count); original(1+(i-1)*bl : i*bl, 1+(j-1)*bl : j*bl) = block; count = count + 1; end end input testing:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 output:
0 2 0 4 0 6 0 8 1 3 2 6 3 9 4 12 0 10 0 12 0 14 0 16 5 15 6 18 7 21 8 24 0 18 0 20 0 22 0 24 9 27 10 30 11 33 12 36 0 26 0 28 0 30 0 32 13 39 14 42 15 45 16 48
Comments
Post a Comment