Prev: Line filling and recursion
Next: Calculate the coordinates of the 3rd vertex of a spherical triangle knowing the other 2 vertices, angles and edge lengths
From: Henry Mei on 7 Aug 2010 13:54 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
From: Henry Mei on 7 Aug 2010 14:43
I've found a solution by using bwlabel/bwconncomp. Thanks all for reading. "Henry Mei" <henry.mei(a)gatech.edu> wrote in message <i3k6e8$3q$1(a)fred.mathworks.com>... > 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 |