2014-01-24 49 views
1

有誰知道圖像處理器(如Photoshop或Gimp)用於選擇具有相似顏色的區域的方法的名稱?另外,任何人都可以指出一些鏈接來解釋這種方法(如果可能的話,用C++代碼)?近似選擇顏色區域

+1

它理應使用一個簡單的算法FloodFill像這裏http://www.dailyfreecode.com/code/flood-fill-algorithm-698.aspx與計算相似度作爲RGB空間兩種顏色之間的距離。 – SGrebenkin

+0

非常感謝,這就是我所尋找的。 –

回答

1

如果您有興趣,這可能是一個檢查顏色是否與另一個顏色相似的示例。 它也使用寬容作爲來自gimp和paint.net的魔杖。

然而,這個例子比較了價值的差異,而不是顏色或亮度的差異。

/*\ 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; 
} 
+0

感謝代碼示例。我只有一個問題:如果我有三種顏色成分(紅色,綠色,藍色),我如何評估第一個參數(int color)的值? –

+0

現在我明白你的意思了。參數int color只是您想要與當前像素進行比較的顏色。用魔杖這個參數等於你點擊的第一個像素的顏色。 –