Re: zigzag scan: Image processing function

Muthiah Annamalai <[email protected]> Fri, 06 Oct 2006 17:05:19 -0500
Newsgroups gmane.comp.gnu.octave.sources
Message-ID <1160172319.17086.4.camel@localhost>
--=-rMjMrZZ+5hTjGQHiV4nq
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by mail.cae.wisc.edu id k96M647T000060

Hello Fredrik,
I think your code does zigzag on a transposed matrix than the one I am
speaking about (I have in my zigzag function).

Your code does a zigzag of matrix =3D reshape(1:9,3,3) as follows,
##   1   4- 7
##   | /  /=20
##   2   5  8
##     /   /|
##   3 - 6  9
but I would expect the output to be as [1 4 2 3 5 7 8 6 9]; instead.
This is the expected behaviour of a DCT frequency coeffients walk-order.

I have also added some documentation to your code. Please see that,
and also adjust the code.

One way  I did adjust your code was to add one extra line=20

 mtrx=3Dmtrx'

before the if statement. Your code however is not modified, as I figured
you may have another way to do the same without a transpose.

-Muthu


On Fri, 2006-10-06 at 16:40 +0200, Fredrik B=C3=BClow wrote:
> Hi Muthiah!
>=20
> I took the liberty of writing my own quicker version of zigzag.  I've
> also added support for mxn matrices (where m!=3Dn).
>=20
> Code is attached.
>=20
> Cheers
> Fredrik B=C3=BClow
>=20
> On 10/5/06, Muthiah Annamalai <[email protected]> wrote:
> >
> >  Hello there,
> >  I have a function zigzag() which walks-off a (square)matrix and read=
s its
> > elements in a zigzag fashion. It is useful for image processing.
> >
> >  Example:
> >  octave  --eval
> > "x=3D[1:4]'*[1:4];eval('x');tic;printf('%d\n',zigzagscan(x));toc()"
> > -q
> >  x =3D
> >
> >      1    2    3    4
> >      2    4    6    8
> >      3    6    9   12
> >      4    8   12   16
> >
> >  1
> >  2
> >  2
> >  3
> >  4
> >  3
> >  4
> >  6
> >  6
> >  4
> >  8
> >  9
> >  8
> >  12
> >  12
> >  16
> >  Elapsed time is 0.177223 seconds.
> >
> >
> >  However I dont know where I should commit it to in Octave-forge.
> >  Someone please suggest the right place.
> >
> >  Code is attached.
> >
> >  Cheers
> >  Muthu
> >
> >
> > _______________________________________________
> > Octave-sources mailing list
> > [email protected]
> > https://www.cae.wisc.edu/mailman/listinfo/octave-sources
> >
> >
> >
> >

--=-rMjMrZZ+5hTjGQHiV4nq
Content-Disposition: attachment; filename=zigzag.m
Content-Type: text/x-octave; name=zigzag.m; charset=UTF-8
Content-Transfer-Encoding: 7bit

## Copyright (C) 2006, October, Fredrik Bulow, <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##

## -*- texinfo -*-
## @deftypefn {Function File} {} zigzag (@var{mtrx})
## Returns zigzag walk-off of the elements of @var{mtrx}.
## Essentially it walks the matrix in a Z-fashion.
##  
## mat = 
##   1   4   7
##   2   5   8
##   3   6   9
## then zigzag(mat) gives the output,
## [1   2   4   7   5   3   6   8   9], by walking as
## shown in the figure from pt 1 in that order of output.
## The argument @var{mtrx} should be a matrix 
##
## @example
## @group
## mat = reshape(1:9,3,3);
## zigzag(mat)
## ans =[1   2   4   7   5   3   6   8   9]
##
## @end group
## @end example
##
## @end deftypefn
##

## Author:   Fredrik Bulow, <[email protected]>

function rval = zigzag(mtrx)
  if nargin < 1
    error('usage: zigzag(matrix); see help zigzag');
  end
  n=size(mtrx);
  
  if(issquare(mtrx)) #Square matrix (quick case)

    ##We create a matrix of the same size as mtrx where odd elements are
    ##1, others 0.
    odd=kron(ones(ceil(n/2)),eye(2))((1:n(1)),(1:n(2)));

    ##We transpose even elements only.
    mtrx = mtrx.*odd + (mtrx.*(1-odd))';

    ##Now we mirror the matrix. The desired vector is now the
    ##concatenation of the diagonals.
    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));

    ##Picking out the diagonals.
    rval  = [];
    for i = n(2)-1:-1:1-n(1)
      rval=[rval diag(mtrx,i)'];
    endfor

  else #Not square (Slow cases)
    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));

    ##Picking out the diagonals and reversing odd ones manually.
    rval  = [];
    for i = n(2)-1:-1:1-n(1)
      new = diag(mtrx,i);
      if(floor(i/2)==i/2) ##Even?
	rval=[rval new'];
      else                ##Odd!
	rval=[rval new((1+length(new))-(1:length(new)))'];
      endif
    endfor
  endif
endfunction

--=-rMjMrZZ+5hTjGQHiV4nq
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

_______________________________________________
Octave-sources mailing list
[email protected]
https://www.cae.wisc.edu/mailman/listinfo/octave-sources

--=-rMjMrZZ+5hTjGQHiV4nq--