help-octave
[Top][All Lists]
Advanced

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

Re: computation speed and coding style


From: Søren Hauberg
Subject: Re: computation speed and coding style
Date: Tue, 03 May 2005 20:45:19 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050404)

Hi Jeff
I never used nlfilter, so I don't have any experience with that. Usualy function calls are pretty slow in Octave, so I guess a function like nlfilter that calls a user suplied function over and over will be slow. Since you have nlfilter, I asume you have octave-forge, so you'll also have conv2 (2-dimensional convolution).

Here's how I've implemented the Sobel edge detector (im is the gray-scale image):

Mx = [-1, 0, 1; -2, 0, 2; -1, 0, 1];
My = Mx';
strength = sqrt( conv2(im, Mx, "same").^2 + ...
                 conv2(im, My, "same").^2 );
bw = (strength > threshold);

It's not quite what you do, but the moral is that conv2 is your friend.
If you want a complete implemtation (with simple thinning), you can see an implementation of several edge detectors I did a while back at
http://hauberg.org/share/edge.m
(the auxilary functions are at http://hauberg.org/share)

Hope that helps,
Søren


Jeff Abrahamson wrote:
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.




-------------------------------------------------------------
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]