Matlab troubles with recursive functions

Matlab troubles with recursive functions

Post by Gerrit Van de Veld » Sat, 08 Nov 2003 01:14:00



Hi,
I have some problems with Matlab 6R12. When i execute my function,
matlab crashes when the recursion is too deep (too much recursions).
It's not the 500-limit, I have set it much higher and the crash
occurs before the limit was reached.

If someone is eager to look at the code, all tips are appreciated,
I'm quite desperate and I'm thinking to migrate to coding in C
instead of Matlab if i can't get it fixed... But that'd be a shame :\

The function is designed to label every white region in a BW image
with a label (1,2,3...) until no more regions are discovered that are
not connected to another pixel.

Here are the source codes:
easiest to experiment with it is to type these lines after you saved
everything in the right m.files:

Quote:>> a = repmat(1, w, h); % w = width in pixels, h = height in
pixels
>> b = find_components(a);

It'll crash whenever the region of white pixels get too big :/

find_components.m:

function result = find_components(input)
set(0,'RecursionLimit',20000)
global img;
img = negate(double(+input));
global label;
label = 0;
img(1,1:size(img,2)) = 0;
img(size(img,1),1:size(img,2)) = 0;
img(1:size(img,1),1) = 0;
img(1:size(img,1),size(img,2)) = 0;
for i=2:size(img, 1)-1
    for j=2:size(img, 2)-1
        if img(i,j) == -1
            label = label + 1;
            search(i, j);
        end
    end
end
result = uint8(img);

search.m:

function search(i, j)
global label;
global img;
img(i,j) = label;
Nset = neighbors(i, j);
for k=1:size(Nset, 1)
    if img(Nset(k,1), Nset(k, 2)) == -1
        search(Nset(k,1), Nset(k,2));
    end
end

neighbors.m:

function Nset = neighbors(i, j)
Nset = [
    i-1 j-1
    i-1 j
    i-1 j+1
    i j-1
    i j+1
    i+1 j-1
    i+1 j
    i+1 j+1
];

negate.m:

function a = negate(a)

a(1,1:size(a, 2)) = 0;
a(size(a,1),1:size(a,2)) = 0;
a(1:size(a,1),1) = 0;
a(1:size(a,1),size(a,2)) = 0;

for i=1:size(a,1)
    for j=1:size(a,2)
        if a(i,j) == 1
            a(i,j) = -1;
        else
            a(i,j) = a(i,j);
        end
    end
end

also, if you want some help understanding this code, don't hesitate
to mail me if you may have a clue why this doesn't work.

 
 
 

1. Recursive Functions in Matlab?

Hi,

is it possible to use recursion in Matlab-functions?
I am using the student edition of Matlab 5.0.

I got syntax errors while trying it.
Can somebody give me a minimal working example, if this is a supported
feature in Matlab?

Any help is greatly appreciated.

Best Regards,   Fred

--

===========================================================
Dipl.-Inform. Fred Rapp

http://barolo.ipc.uni-tuebingen.de
===========================================================

2. Embedding Fonts

3. trouble with calling matlab functions from C

4. williams.fnt

5. Compiling recursive functions

6. Can't view TIF files in Imaging "document page file cannot be found"

7. Matcom compiler & recursive functions

8. Ridgeley, W.Va. - No Dialing Changes Found

9. Integrating a Recursive function

10. Trouble getting to dll functions

11. Recursive calls in Matlab?

12. Recursive data structures in MATLAB?

13. MATLAB & Recursive Programming