Kleber Mota
Kleber Mota

Reputation: 9065

Selecting color region by approximation

Does anyone know the name of the method used by image processors (like Photoshop or Gimp) to select a region with similar colors? Also, can anyone indicate some link for an explanation of this method (with C++ code, if possible)?

Upvotes: 1

Views: 576

Answers (1)

Pascal Neubert
Pascal Neubert

Reputation: 96

If you're interested this could be an example of checking whether or not a color is similar to another. It also uses tolerance as magic wand from gimp and paint.net does.

However this example compares the difference in value instead of the difference in color or lightness.

/*\ function to check if the color at this position is similar to the one you chose
|*  Parameters:
|*    int color       - the value of the color of choice
|*    int x           - position x of the pixel
|*    int y           - position y of the pixel
|*    float tolerance - how much this pixels color can differ to still be considered similar
\*/

bool isSimilar(int color, int x, int y, float tolerance)
{
    // calculate difference between your color and the max value color can have
    int diffMaxColor = 0xFFFFFF - color;
    // set the minimum difference between your color and the minimum value of color
    int diffMinColor = color;
    // pseudo function to get color ( could return 'colorMap[y * width + x];')
    int chkColor = getColor(x, y); 
    // now checking whether or not the color of the pixel is in the range between max and min with tolerance
    if(chkColor > (color + (diffMaxColor * tolerance)) || chkColor < ((1 - tolerance) * diffMinColor))
    {
        // the color is outside our tolerated range
        return false;
    }
    // the color is inside our tolerated range
    return true;
}

Upvotes: 1

Related Questions