For a project I am working on, I need to figure out the outline (boundary) of each subject on a photo. One way to do this is, for each pixel, to calculate the difference between its color and the color of surrounding pixels. (Assume the picture is reasonably clear, I am not interested in background noise at this point.)

The first thing I tried was to calculate the difference base on RGB:

$diff = abs($r1 - $r2) + abs($g1 - $g2) + abs($b1 - $b2);

Or I can do something like:

$diff = ($r1 - $r2) ** 2 + ($g1 - $g2) ** 2 + ($b1 - $b2) ** 2; (or I can make this more std like)

Whether to use abs() or **2 does not seem to be important at this point.

At this point, I compare each point with all 8 surrounding pixels, and pick the largest difference as the difference between this pixel and its surrounding. (obviously I can compare with more surrounding pixels, or give different weight to pixels base on their distance to this pixel.)

This actually works for me to a level.

Then I started to think whether RGB is indeed the right way to describe color for what I am doing, so the next thing I tried is to use

my $ll = 0.212671 * $r + 0.715160 * $g + 0.072169 * $b;

as the "color", and calculate the difference.

Again this worked to a level.

There must be more alternative ways, and I am interested in hearing them and trying them out, and see what the best way is, or best ways are. Thanks.


In reply to the difference between two colors, and how to describe a color by pg

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.