I've got some image data that I've binarized. The image contains two curves, from which I'd like to compute the distance between (eventually computing an average radius for each point in the picture). I've done the computation, but it's done in C programmer style, and fairly ugly
data = ImageData[ b ] ;
{h, w} = Dimensions[data] ;
min = Table[0, {w}] ;
max = Table[0, {w}] ;
averageRadiusList = {} ;
scale = 0.1 / w ;
For [ column = 1, column < w + 1, column++,
For [ row = 1, row < h/2, row++,
If [
data[[row, column]] != 1,
min[[column]] = row ; Break
]
]
For [ row = h, row > h/2, row--,
If [
data[[row, column]] != 1,
max[[column]] = row ; Break
]
]
If [ min[[column]] != 0 && max[[column]] != 0,
AppendTo[
averageRadiusList, {column * scale,
scale * (max[[column]] - min[[column]])/2} ]
]
]
I was wondering if there's a more natural "Mathematica way" of doing things ... something that is presumably more elegant, efficient, compact, and more general?


http://blog.wolfram.com/data/uploads/2010/12/NavigatingtheBlenheimMaze.nb
That notebook is explained, to an extent, in the corresponding company blog from a while back.
http://blog.wolfram.com/2010/12/07/navigating-the-blenheim-maze/
– Daniel Lichtblau Apr 18 '12 at 23:34AppendToyou could use a Bag. – Apr 19 '12 at 07:41Internal`Bagis an undocumented function. You can find some information plus links to other sources in this question – Heike Apr 19 '12 at 09:38coland replace any instance of that with the number 1. Anything else gets replaced with zero. The rule delayed :> is used because we don't know ahead of time what other things might need replaced. We've manually binarized the image.Replaceis done at level 2 only, hence the{2}. This means we will replace the elements in the image data matrix. If I had used {1} it would have operated on the rows and replaced them with 0 since none of the rows matchcol[[3]]. – Andy Ross Apr 19 '12 at 05:26