0

I am working on a project where i have used a image whose size is (512x512)then i have divided the whole image by 8 so that there will be 64x64 block then i have to rearrange each 8x8 image patch into a single column so that new size would be 64x4069. unable to understand how to do it.please help. Here is my code

enter code here

a=imread('lena.png');
b=double(a);
[r,c]=size(b);
bl=8;
br=r/bl;
bc=r/bl;

It will arrange in such a order that first column would be image patch of (1:8,1:8)next column would be(9:16,9:16)likewise.

P.Bisoyi
  • 123
  • 1
  • 2
  • 10

1 Answers1

0

If reshape is allowed, permute is definitely allowed.

Assuming both the original and block sub-matrix are square matrices

Here is one approach

out = permute(reshape(A,blSz,size(A,1)/blSz,blSz,[]),[1 3 2 4]);
out = reshape(out,size(out,1)*size(out,1),[]);

Sample inputs:

A = randi(50,8);    %// Change it with your original `512x512` matrix
blSz = 2;           %// Change it to 8 for your problem

Results:

>> A

A =

31    17    18    10    33    31    43    16
20    40    31    15    34    23    42     6
46    24    10     5    32    23    13    47
 1     2    37    29    48    34    31    33
24     9    13    35    11    39    30    24
22    37    46    28    36    18    28    32
24    24    14    22    12    34    44    28
39     8    39    33     6    21    14    33

>> out

out =

31    46    24    24    18    10    13    14    33    32    11    12    43    13    30    44
20     1    22    39    31    37    46    39    34    48    36     6    42    31    28    14
17    24     9    24    10     5    35    22    31    23    39    34    16    47    24    28
40     2    37     8    15    29    28    33    23    34    18    21     6    33    32    33

Using loops as OP requested

A = randi(50,8);
blSz = 2;
nBl = size(A,1)/2;

out = zeros(size(reshape(A,blSz*blSz,[])));
count = 1;
for ii = 1:nBl
    for jj= 1:nBl
        block = A((jj-1)*blSz + 1:(jj-1)*blSz + blSz, (ii-1)*blSz + 1:(ii-1)*blSz + blSz);
        out(:,count) = block(:);
        count = count + 1;
    end
end

Gives the same result as above!

Alternative for im2col using vectorized approach

newOut = mat2cell(reshape(out,blSz,[]),blSz,repmat(blSz,size(out,2),1));
newOut = cell2mat(reshape(newOut,nBl,[]));
Santhan Salai
  • 3,888
  • 19
  • 29
  • your answer helped me a lot thanks. – P.Bisoyi Jun 03 '15 at 09:14
  • @P.Bisoyi if you found this answer helpful, Consider marking it as accepted. This can be done by clicking the checkmark below the up and down arrows to the left of my answer – Santhan Salai Jun 03 '15 at 09:20
  • can you help me how to get back 512x512 size from 64x4069 size using for loop. – P.Bisoyi Jun 03 '15 at 09:51
  • @P.Bisoyi Let me add the solution – Santhan Salai Jun 03 '15 at 09:55
  • ok sir,thanks for your help. – P.Bisoyi Jun 03 '15 at 10:14
  • @P.Bisoyi added vectorized approach instead. i find it difficult implementing using for loop, if you want to do that in for loop alone, please post it explicitly as a new question stating you want to replicate `col2im` using for loop – Santhan Salai Jun 03 '15 at 11:00
  • here in the program the output size is 64x4096 but i want new size would be 48x4096,how to remove last 16 rows. – P.Bisoyi Jun 11 '15 at 06:37
  • @P.Bisoyi I thought you wanted to replicate `im2col`. If you do it with `im2col` you would end up with `64x4096` only. If you just wanted to remove last 16 rows, then do `out(end-15:end,:) = []` this will remove last 16 rows from the already produced output. – Santhan Salai Jun 12 '15 at 08:42