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:

It'll crash whenever the region of white pixels get too big :/Quote:>> a = repmat(1, w, h); % w = width in pixels, h = height in

pixels

>> b = find_components(a);

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.