⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reorder.m

📁 Sparse Signal Representation using Overlapping Frames (matlab toolbox)
💻 M
字号:
function X=Reorder(Y,ImSize,BlkSize,Ord)
% Reorder    Reorder distinct image blocks into columns, or vice versa 
%            Y and X will be of the same type (uint8, uint16 or double)
% If Ord>0 first argument is assumed to be the image, and columns are returned
% for this case Reorder is similar (but not identical) to im2col.m
% If Ord<0 first argument is assumed to be the columns, and image is returned
% for this case Reorder is similar (but not identical) to col2im.m
% Examples:
%
% X=Reorder(Y,ImSize,BlkSize,Ord); 
% X=Reorder(Y,ImSize,BlkSize,1);    % take image to columns (im2col)
% Y=Reorder(X,ImSize,BlkSize,-1);   % take columns to image (col2im)  
% ---------------------------------------------------------------------------% arguments:
%  Y        the image (or coefficients of the image), size MxN
%  X        the vectors of the blocks, size (Mb*Nb)xL,  where L=(M*N)/(Mb*Nb)
%  ImSize   [M,N] should be the size of the image (coefficients)
%  BlkSize  [Mb,Nb] size of the image blocks
%  Ord      an integer that gives the direction and type
%             +- 1  : columnwise ordering of elements
%             +- 2  : zig-zag ordering of elements (like JPEG)
%             +- 3  : tree-like ordering of elements (like wavelets)
% ---------------------------------------------------------------------------
% ---------------------------------------------------------------------------% Copyright (c) 1999.  Karl Skretting.  All rights reserved.
% Hogskolen in Stavanger (Stavanger University), Signal Processing Group
% Mail:  karl.skretting@tn.his.no   Homepage:  http://www.ux.his.no/~karlsk/
% 
% HISTORY:
% Ver. 1.0  15.08.2000 Karl Skretting, function made 
% Ver. 1.1  27.11.2002  KS: moved from ..\Frames to ..\FrameTools
% ---------------------------------------------------------------------------
Mfile='Reorder';
Display=1;
% check input and output arguments, and assign values to arguments
if (nargin < 4); 
   error([Mfile,': function must have 4 input arguments, see help.']); 
end
if (nargout ~= 1); 
   error([Mfile,': function must have one output arguments, see help.']); 
end

t=prod(size(ImSize));
if t~=2
   error([Mfile,': ImSize is not correct given.']);
end
t=prod(size(BlkSize));
if t~=2
   error([Mfile,': BlkSize is not correct given.']);
end
if rem(ImSize(1),BlkSize(1)) | rem(ImSize(2),BlkSize(2))
   error([Mfile,': Image (',int2str(ImSize(1)),'x',int2str(ImSize(2)),...
         ') can not be divided into blocks of size (',...
         int2str(BlkSize(1)),'x',int2str(BlkSize(2)),').']); 
end
M=ImSize(1);N=ImSize(2);
Mb=BlkSize(1);Nb=BlkSize(2);

% the zig-zag order
if abs(Ord)==2
   i=1;j=1;ii=1;
   zz=zeros(Mb,Nb);
   for n=1:(Mb*Nb)
      zz(i,j)=n;
      i=i-ii;j=j+ii;   % prepare for next
      if (i>Mb); i=Mb; j=j+2; ii=1; end;
      if (j>Nb); j=Nb; i=i+2; ii=(-1); end;
      if (j<1); j=1;ii=1; end;
      if (i<1); i=1;ii=(-1); end;
   end
   zz=zz(:);
end
if abs(Ord)==3
   k=ceil(log2(max([Mb,Nb])));
   zz=ones(2^k);
   for kk=1:k
      k2=2^(kk-1)+1;k3=2^kk;
      n2=2^(2*kk);
      zz(k2:k3,k2:k3)=reshape((n2*3/4+1):(n2),k2-1,k2-1)';
      zz(k2:k3,1:(k2-1))=reshape((n2*2/4+1):(n2*3/4),k2-1,k2-1)';
      zz(1:(k2-1),k2:k3)=reshape((n2*1/4+1):(n2*2/4),k2-1,k2-1)';
   end
   zz=zz(1:Mb,1:Nb);
   zz=zz(:);
   if k>floor(log2(min([Mb,Nb])))
      [temp,zz]=sort(zz);   
      [temp,zz]=sort(zz);   
   end
end
if Ord>1
   [temp,zz]=sort(zz);   % the inverse order of zz
end

if Ord>0
   % this is im2col
   [m,n]=size(Y);
   if ((m~=M) | (n~=N))
      error([Mfile,': ImSize and size of Y (image) are not equal.']);
   end
   L=(M*N)/(Mb*Nb);
   NNb=N/Nb;                   % number of blocks in each row (of blocks)
   X=reshape(Y,(Mb*Nb),L);     % just make X the right size
   for m=1:(M/Mb)              % for each row of blocks
      X(:,(m-1)*NNb+(1:NNb))=reshape(Y((m-1)*Mb+(1:Mb),:),Mb*Nb,NNb);
   end
   if Ord>1
      X=X(zz,:);
   end
else
   % this is col2im
   L=(M*N)/(Mb*Nb);
   [mbnb,l]=size(Y);
   if ((mbnb~=(Mb*Nb)) | (l~=L))
      error([Mfile,': BlkSize and size of X (columns) do not correspond.']);
   end
   if Ord<(-1)
      Y=Y(zz,:);
   end
   NNb=N/Nb;                   % number of blocks in each row (of blocks)
   X=reshape(Y,M,N);           % just make X the right size
   for m=1:(M/Mb)
      X((m-1)*Mb+(1:Mb),:)=reshape(Y(:,(m-1)*NNb+(1:NNb)),Mb,N);
   end
end

return


% test of function
Y=reshape(1:64,8,8);
ImSize=size(Y);
BlkSize=[4,4];
X=Reorder(Y,ImSize,BlkSize,3);
Yr=Reorder(X,ImSize,BlkSize,-3);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -