Measuring curve lengths and irregular areas

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Alan Hadley
Posts: 48
Joined: 2010-03-31T12:17:55-07:00
Authentication code: 8675308

Measuring curve lengths and irregular areas

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Measuring curve lengths and irregular areas

Post 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.
snibgo's IM pages: im.snibgo.com
Alan Hadley
Posts: 48
Joined: 2010-03-31T12:17:55-07:00
Authentication code: 8675308

Re: Measuring curve lengths and irregular areas

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Measuring curve lengths and irregular areas

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Measuring curve lengths and irregular areas

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply