If the border is formed by segments and you can code it as the sequence of edges
( (P0x, P0y), (P1x, P1y), ... , (P0x, P0y) ),
then there is a simple way to compute the inside area:

my @border = ( [2,1], [1,4], [4,5], [5,2], [2,1] ); my $area = 0; for ( 0 .. $#border-1 ) { $area += ( $border[$_+1][0] - $border[$_][0] ) * ( $border[$_][1] + $border[$_+1][1] ) / 2; } print "$area\n";
This is simply the integral of the segments (the area of the underlying trapeziums), with a positive sign when going right and negative when going left. In order for it to give the correct (positive) result, the points of the border must be stored clockwise. If there are holes in your image, you have to compute their area separately and subtract it from the area of the outer image (or you could use other tricks, like inserting a "cut" from the outer border to the inner one and counting it in both directions).

Cheers

Antonio

Update: Slight change in code and comment to make them better fit together

The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket

In reply to Re: pixel counting by abell
in thread pixel counting by redbeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.