SURF比较代码给出问题

时间:2015-07-07 21:15:52

标签: computer-vision

我正在进行SURF比较,通过计算desriptors之间的欧氏距离来识别图像中的物体。但以下代码无效。 IPoint是一个SURF特征点,任何帮助都是apreciated。

 List<IPoint> ipts = new List<IPoint>();
Dictionary<string, List<IPoint>> objs = new Dictionary<string, List<IPoint>>();
double distance(IPoint a, IPoint b)
{
    double dis = 0;
    for (int i = 0; i < 64; i++)
    {

        dis += Math.Sqrt(Math.Pow((a.descriptor[i] - b.descriptor[i]), 2));
    }
    return (dis);
}
bool matchpoint(IPoint a, List<IPoint> l, out string e)
{
    e = "";
    double smallest = double.MaxValue;
    string s = string.Empty;
    for (int i = 0; i < l.Count; i++)
    {
        var d = distance(a, l[i]);
        if (d < smallest)
        {
            smallest = d;
            s = i.ToString();
        }
    }
    if (smallest < 0.5)
    {
        e = s;
        return true;
    }
    else
    {
        return false;// null;
    }
    return false;
}
string match(out double per)
{
    string h;
    Dictionary<string, double> torn = new Dictionary<string, double>();
    foreach (string s in objs.Keys.ToList())
    {
        int count = 0;
        for (int i = 0; i < objs[s].Count; i++)
        {
            if (matchpoint(objs[s][i], ipts,out h))
            {
                count++;
            }
        }
        torn[s] = count / objs[s].Count;
        count = 0;
    }
    string smalln = "";
    double smallest = double.MaxValue;
    foreach (string s in torn.Keys.ToList())
    {
        if (torn[s] < smallest)
        {
            smallest = torn[s];
            smalln = s;
        }
    }
    per = smallest;
    return smalln;
}
private void button1_Click(object sender, EventArgs e)
{
    double d;
    match(out d);
    MessageBox.Show(match(out d) + " " + d.ToString());
}

2 个答案:

答案 0 :(得分:0)

应该是:

double distance(IPoint a, IPoint b)
{
    double dis = 0;
    for (int i = 0; i < 64; i++)
    {

        dis += Math.Pow((a.descriptor[i] - b.descriptor[i]), 2);
    }
    return Math.Sqrt(dis);
}

你正在平方,然后取每个差异的根,这基本上是做一个绝对值。试着记住毕达哥拉斯的简单例子:C=SQRT(A*A+B*B)而不是C=SQRT(A*A)+SQRT(B*B)

答案 1 :(得分:0)

感谢您的帮助,同时也让两个距离的比例完美无缺。我在这里发布工作代码,因为你无法在其他任何地方找到这个问题的答案。

void getMatches(List<IPoint> ipts1, List<IPoint> ipts2,out List<IPoint> mats)
{
    List<IPoint> matches = new List<IPoint>();
    float dist, d1, d2;
    IPoint match;
    matches.Clear();

    for(int i = 0; i < ipts1.Count; i++)
    {
        d1 = d2 = float.MaxValue;
        for(int j = 0; j < ipts2.Count; j++) 
        {
            dist = (float)distance(ipts1[i], ipts2[j]);//ipts1[i] - ipts2[j];  
            if(dist<d1)  // if this feature matches better than current best
            {
                d2 = d1;
                d1 = dist;
                match = ipts2[j];
            }
            else if(dist<d2) // this feature matches better than second best
            {
                d2 = dist;
            }
        }

        // If match has a d1:d2 ratio < 0.65 ipoints are a match
        if(d1/d2 < Convert.ToSingle(textBox2.Text)) 
        {
            matches.Add(ipts1[i]);
        }
    }

    mats = matches;  
}