Page 1 of 1

Measuring curve lengths and irregular areas

Posted: 2012-10-31T02:18:44-07:00
by Alan Hadley
I'm not sure if this should go here or in the image processing section, so apologies if I'm in the wrong place.

I was tinkering with some of the tools I am adding to my windows interface for IM. One of these tools lets you lay out a bezier curve or polyline (open or closed) etc. on top of an image, it then produces an MVG string for the shape. Then it hit me if I Draw this shape with a white Stroke and Stroke-Width 1 on a black canvas then I could use Statistics or a Histogram to count the white pixels and get the length of the curve. I could use this method to measure any object in the original image. The same idea would work for areas just draw the curve with a white fill.

But before you get carried away there are potential snags, especially with the curve length mainly to do with antialiasing. If you turn antialiasing off (stroke-antialias 0) you get a chunky curve which is two pixels wide in some places but is pure black and white. On the other hand if you leave antialiasing on you get a smoother looking curve which is no longer black and white but shades of grey as well.

So I have a few questions someone may be able to answer:-

1. Is it better to draw the curve with antialiasing on and use statistics, or off and use histogram to add up/count whiteness?

2. What width should the curve be, would you get a more accurate result with a wider curve and deviding the result by the stroke width?

3. Is there a best shape to draw as a test curve to scale the results with, e.g. I could draw a circle of known circumferance and use the result to correct other curve lengths?

4. How accurate is the greyness of antialias pixels, if a pixel wide curve cuts a canvas pixel in half will the colour be half quantum range?

5. Would any Morphology technique work on a non-antialiased curve to correct for the curve being two pixels wide in places?

Alan Hadley

Re: Measuring curve lengths and irregular areas

Posted: 2012-10-31T06:41:37-07:00
by snibgo
Will that work for diagonal lines? Eg a line from (0,0) to (10,10) is longer than one from (0,0) to (0,10), even if they have the same number of non-aliased pixels.

I would have thought the best way to estimate the length of an arbitrary curve would be to split it into segments such that each segment is close to a straight line.

Re: Measuring curve lengths and irregular areas

Posted: 2012-10-31T07:41:34-07:00
by Alan Hadley
Image

Notice the grey pixels in the diagonal line above, they would cause the sum to be greater than 10. I will have to do more tests to quantify this. You are right about the line segments method, but that's not always convenient.

P.S. I hope the image above works, it's my first try at including one here.

Alan

Re: Measuring curve lengths and irregular areas

Posted: 2012-10-31T10:58:19-07:00
by fmw42
I would suggest that you not use -stroke or -strokewidth as that makes the curve thicker. Just specify -fill white on a black background and turn anti-aliasing off. Then the mean*width*height computed from -format "%[fx:mean*w*h]" will be the number of white pixels on the black background for any shape.

Re: Measuring curve lengths and irregular areas

Posted: 2012-12-13T17:12:13-07:00
by anthony
You can measure curves using a 'constrained distance function. It will handle diagonals and even some gaps in the line, depending on the kernel radius used. Finding the end of a line may however be a little harder :-)

See IM examples, Morphology, Constrained Distance
http://www.imagemagick.org/Usage/morpho ... nstrainted

After applying you can look for the brightest, or most distant point from the 'seed' point, and thus the line length regardless of how it curves around the image.