20
$\begingroup$

I start with a photograph of a shape (physically made by the flow of a liquid into another), of which I can extract the border, manually or using Mathematica's feature detect feature :

Example

Using the method described here, I extract some points of the shape, in a standard list form ({{x,y},{x,y}, ...}). Here is the example plot of such data :

A plot of the obtained data

How to :

  1. Obtain the shape's area (area of the enclosed zone)
  2. How to get an algebraic fit of the shape (and/or part of it)?
  3. How to compare one shape against another, on their respective "jaggedness".

Question 3 is more of a mathematical question, but I'm just searching for an approximate comparison tool, more in the spirit of the following example than something absolute:

this example

I expected CornerFilter to work, but it seems to give no result whatsoever. As for 2, I can fit small part of the curve using Fit[], but the general shape has multiple point with the same x, which forbid this.

$\endgroup$
14
  • 3
    $\begingroup$ For the first: here's a short routine for computing the signed area, assuming your points have already been ordered either clockwise or anticlockwise: PolygonSignedArea[pts_?MatrixQ] := Total[Det /@ Partition[pts, 2, 1, {1, 1}]]/2. If the points aren't already sorted, Sjoerd says to look at ListCurvePathPlot[] (and the related function FindCurvePath[]). $\endgroup$ May 22, 2012 at 18:58
  • 2
    $\begingroup$ For "respective jaggedness" you could use Box Counting method used on fractal shapes: this en.wikipedia.org/wiki/Box_counting or this en.wikipedia.org/wiki/Fractal_dimension_on_networks $\endgroup$ May 22, 2012 at 19:35
  • $\begingroup$ @J.M. are you sure your routine sorts them anticlockwise (or clockwise)? Also, even if you sort by the polar angle, that does not guarantee that a ray coming out of the centre of the shape will not intersect it twice. $\endgroup$
    – acl
    May 22, 2012 at 19:46
  • $\begingroup$ @acl: hmm, yes; that is indeed a caveat of sorting by polar angle. I don't have a better approach at the moment. $\endgroup$ May 22, 2012 at 19:49
  • 3
    $\begingroup$ Could you tell us your requirements regarding your "algebraic fit"? $\endgroup$ May 22, 2012 at 20:20

1 Answer 1

24
$\begingroup$

You can use Mathematica's image processing functions for questions 1 and 3. Here's how:

1: Area

img = Binarize@Import["https://i.stack.imgur.com/I2gkK.jpg"] ~Erosion~ 1;
(m = MorphologicalComponents[img]) // Colorize

enter image description here

To get the area of the pink part in sq. pixels, use ComponentMeasurements:

2 /. ComponentMeasurements[{m, img}, "Area"]
(* 25168.1 *)

3: Jaggedness

Given two shapes with a similar area, the more jagged one will have a larger perimeter. So a simple way to approximate "jaggedness" would be to use the same function, ComponentMeasurements and compare the perimeters (provided the areas are similar)

2 /. ComponentMeasurements[{m, img}, "PerimeterLength"]
(* 1352 *)

If the areas are not the same, you could find the "EquivalentDiskRadius" (which gives you the radius of the circle with the same area as the shape) for each shape and see by what percentage the corresponding perimeter lengths are off from that of the respective circles.

$\endgroup$
5
  • 3
    $\begingroup$ +1, though I'm not sure about "Given two shapes with a similar area, the more jagged one will have a larger perimeter." What about two rectangles with the same area but different circumference? Intuitively I'd say both have the same jaggedness. $\endgroup$ May 22, 2012 at 20:55
  • 1
    $\begingroup$ @SjoerdC.deVries I see your point. I took the spirit of the question to be along the lines of "Here's this irregular blob and here's another irregular blob. How can I tell which is approximately smoother, for some definition of 'smooth'." $\endgroup$
    – rm -rf
    May 22, 2012 at 21:01
  • 1
    $\begingroup$ +1, and I think ComponentMeasurements can answer question 2, too, with the Length/Width/SemiAxis/Orientation/Elongation/Eccentricity measurements $\endgroup$ May 23, 2012 at 9:23
  • $\begingroup$ How to mark the number of each area if I have multiple shapes in a picture? When I do the similar thing, I can't tell the correspondence between different area and shape. $\endgroup$
    – BNHSX
    Jul 12, 2016 at 18:16
  • $\begingroup$ +1 I get slightly different results with 10.4 $\endgroup$
    – bobbym
    Aug 8, 2016 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.