help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

computation speed and coding style


From: Jeff Abrahamson
Subject: computation speed and coding style
Date: Tue, 3 May 2005 09:13:25 -0400
User-agent: Mutt/1.5.6+20040907i

For a course I'm teaching I assigned a problem to code an edge
detector.  I coded my solution in octave because it was easy to code,
but I was appalled that it ran orders of magnitude slower than any
student solution, including the python and perl submissions.

I'm wondering if I just did something really dumb in my octave coding.
Here's what I did:

I call edge_detect with the name of the edge detector to use.  That
edge detector calls nlfilter with the appropriate convolution filter.
So most everything probably happens inside nlfilter.  Here's the
important code:

    # smooth is the smoothed image (as an array)
    edge_detect( img_name, smooth, "roberts_detect")


    function edge_detect ( img_name, smooth, detector_name )

            edges = feval(detector_name, smooth);
            # then output the edge-detected image "edges"

    endfunction


    function ret = sobel_detect ( image )

            ret = nlfilter(image, [3,3], "sobel_kernel");

    endfunction


    function ret = sobel_kernel ( B )

            Mx = [-1, 0, 1; -2, 0, 2; -1, 0, 1];
            My = Mx';

            fx = sum((B .* Mx)(:));
            fy = sum((B .* My)(:));

            magnitude = fx + fy;
            if(fx == 0)
                    ret = 0;
            else
                    dirn = atan(fy / fx);
                    if(magnitude > dirn)
                            ret = 255;
                    else
                            ret = 0;
                    endif
            endif

    endfunction

Thanks for any tips.

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]