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

📄 im2col.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function b=im2col(varargin)
%IM2COL Rearrange image blocks into columns.
%   B = IM2COL(A,[M N],'distinct') rearranges each distinct
%   M-by-N block in the image A into a column of B. IM2COL pads A
%   with zeros, if necessary, so its size is an integer multiple
%   of M-by-N. If A = [A11 A12; A21 A22], where each Aij is
%   M-by-N, then B = [A11(:) A12(:) A21(:) A22(:)].
%
%   B = IM2COL(A,[M N],'sliding') converts each sliding M-by-N
%   block of A into a column of B, with no zero padding. B has
%   M*N rows and will contain as many columns as there are M-by-N
%   neighborhoods in A. If the size of A is [MM NN], then the
%   size of B is (M*N)-by-((MM-M+1)*(NN-N+1). Each column of B
%   contains the neighborhoods of A reshaped as NHOOD(:), where
%   NHOOD is a matrix containing an M-by-N neighborhood of
%   A. IM2COL orders the columns of B so that they can be
%   reshaped to form a matrix in the normal way. For example,
%   suppose you use a function, such as SUM(B), that returns a
%   scalar for each column of B. You can directly store the
%   result in a matrix of size (MM-M+1)-by-(NN-N+1) using these
%   calls: 
%
%        B = im2col(A,[M N],'sliding');
%        C = reshape(sum(B),MM-M+1,NN-N+1);
%
%   B = IM2COL(A,[M N]) uses the default block type of
%   'sliding'.
%
%   B = IM2COL(A,'indexed',...) processes A as an indexed image,
%   padding with zeros if the class of A is uint8, or ones if the
%   class of A is double.
%
%   Class Support
%   -------------
%   The input image A can be of class uint8 or double. The output
%   matrix B is of the same class as the input image.
%
%   See also BLKPROC, COL2IM, COLFILT, NLFILTER.

%   Clay M. Thompson 10-6-92
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 5.8 $  $Date: 1997/11/24 15:35:12 $

[a, block, kind, padval] = parse_inputs(varargin{:});

if strcmp(kind, 'distinct')
    % Pad A if size(A) is not divisible by block.
    [m,n] = size(a);
    mpad = rem(m,block(1)); if mpad>0, mpad = block(1)-mpad; end
    npad = rem(n,block(2)); if npad>0, npad = block(2)-npad; end
    if (isa(a,'uint8'))
        if (padval == 1)
            aa = repmat(uint8(1), m+mpad, n+npad);
        else
            aa = repmat(uint8(0), m+mpad, n+npad);
        end
    else
        if (padval == 1)
            aa = ones(m+mpad,n+npad);
        else
            aa = zeros(m+mpad,n+npad);
        end
    end
    aa(1:m,1:n) = a;
    
    [m,n] = size(aa);
    mblocks = m/block(1);
    nblocks = n/block(2);
    
    if (isa(a,'uint8'))
        b = repmat(uint8(0), prod(block), mblocks*nblocks);
        x = repmat(uint8(0), prod(block), 1);
    else
        b = zeros(prod(block),mblocks*nblocks);
        x = zeros(prod(block),1);
    end
    rows = 1:block(1); cols = 1:block(2);
    for i=0:mblocks-1,
        for j=0:nblocks-1,
            x(:) = aa(i*block(1)+rows,j*block(2)+cols);
            b(:,i+j*mblocks+1) = x;
        end
    end
    
elseif strcmp(kind,'sliding')
    [ma,na] = size(a);
    m = block(1); n = block(2);
    
    % Create Hankel-like indexing sub matrix.
    mc = block(1); nc = ma-m+1; nn = na-n+1;
    cidx = (0:mc-1)'; ridx = 1:nc;
    t = cidx(:,ones(nc,1)) + ridx(ones(mc,1),:);    % Hankel Subscripts
    tt = zeros(mc*n,nc);
    rows = [1:mc];
    for i=0:n-1,
        tt(i*mc+rows,:) = t+ma*i;
    end
    ttt = zeros(mc*n,nc*nn);
    cols = 1:nc;
    for j=0:nn-1,
        ttt(:,j*nc+cols) = tt+ma*j;
    end
    b = a(ttt);
else
    error([deblank(kind),' is an unknown block type']);
end

%%%
%%% Function parse_inputs
%%%
function [a, block, kind, padval] = parse_inputs(varargin)

switch nargin
case 0
    error('Too few inputs to IM2COL');

case 1
    error('Too few inputs to IM2COL');
    
case 2
    if (strcmp(varargin{2},'indexed'))
        error('Too few inputs to IM2COL');
    else
        % IM2COL(A, [M N])
        a = varargin{1};
        block = varargin{2};
        kind = 'sliding';
        padval = 0;
    end
    
case 3
    if (strcmp(varargin{2},'indexed'))
        % IM2COL(A, 'indexed', [M N])
        a = varargin{1};
        block = varargin{3};
        kind = 'sliding';
        padval = 1;
        
    else
        % IM2COL(A, [M N], 'kind')
        a = varargin{1};
        block = varargin{2};
        kind = varargin{3};
        padval = 0;
        
    end
    
case 4
    % IM2COL(A, 'indexed', [M N], 'kind')
    a = varargin{1};
    block = varargin{3};
    kind = varargin{4};
    padval = 1;
    
otherwise
    error('Too many input arguments to IM2COL');
end

matchStrings = ['sliding '
                'distinct'];
idx = strmatch(kind, matchStrings);
if (isempty(idx))
    error('Block type must be either ''distinct'' or ''sliding''');
end
kind = deblank(matchStrings(idx(1),:));

if (isa(a,'uint8'))
    padval = 0;
end

⌨️ 快捷键说明

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