From: Henry Mei on
I'm basically trying to recursively navigate through all pixels of a line and assign it a height value (double), by converting the grayscale uint8 array to double and then replacing the pixels with a height. I only need to do a 4-neighbor search, and to avoid recurring through already changed pixel, I just use a boolean array to keep track of traversed pixels.

I tested my code using nxn arrays filled with the line intensity I want (i.e. the function should replace all elements of the array); however, MATLAB locks up and crashes when exceed a ~50x50 array. I'd appreciate it if anybody could tell me if I made an error in my code or if there's an iterative solution.

Below is code:

function output = assignHeight(arr, pr, pc, height, thresh)

[m,n] = size(arr);
grid = false(m,n);

function assignH(pr, pc, m, n, height, thresh)

if (pr<=m) && (pc<=n) && (pr>0) && (pc>0) && (arr(pr,pc)>255*thresh) && (grid(pr,pc)==false)
arr(pr,pc) = height;
grid(pr,pc) = true;
assignH(pr,pc+1,m,n,height,thresh);
assignH(pr,pc-1,m,n,height,thresh);
assignH(pr+1,pc,m,n,height,thresh);
assignH(pr-1,pc,m,n,height,thresh);
end
end

assignH(pr, pc, m, n, height, thresh);
output = arr;
end