步驟3,計算直方圖相似度度量
這一步驟的法寶在於這個:
Sim(G,S)= 其中G,S為直方圖,N 為顏色空間樣點數
為了大家少敲兩行字兒,也給出一堆亂七八糟的代碼:
//計算相減後的絕對值
private float GetAbs(int firstNum, int secondNum)
{
float abs = Math.Abs((float)firstNum - (float)secondNum);
float result = Math.Max(firstNum, secondNum);
if (result == 0)
result = 1;
return abs / result;
}
//最終計算結果
public float GetResult(int[] firstNum, int[] scondNum)
{
if (firstNum.Length != scondNum.Length)
{
return 0;
}
else
{
float result = 0;
int j = firstNum.Length;
for (int i = 0; i < j; i++)
{
result += 1 - GetAbs(firstNum[i], scondNum[i]);
Console.WriteLine(i + "----" + result);
}
return result/j;
}
}