matlab - How to rearrange each image patch into number of column vector -


this question has answer here:

i working on project have used image size (512x512)then have divided whole image 8 there 64x64 block have rearrange each 8x8 image patch single column new size 64x4069. unable understand how it.please help. here code

enter code here

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

it arrange in such order first column image patch of (1:8,1:8)next column be(9:16,9:16)likewise.

if reshape allowed, permute allowed.

assuming both original , block sub-matrix square matrices

here 1 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 original `512x512` matrix blsz = 2;           %// change 8 problem 

results:

>>  =  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 op requested

a = randi(50,8); blsz = 2; nbl = size(a,1)/2;  out = zeros(size(reshape(a,blsz*blsz,[]))); count = 1; ii = 1:nbl     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 same result above!

alternative im2col using vectorized approach

newout = mat2cell(reshape(out,blsz,[]),blsz,repmat(blsz,size(out,2),1)); newout = cell2mat(reshape(newout,nbl,[])); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -