您的位置:首页 > 运维架构

OpenSURF图像识别

2016-05-06 22:33 302 查看
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

     
最近工作有点闲,有时间就玩点有意思的东西------SURF 图像识别(WIKI地址),懂了图像识别,真的有很多东西可以玩。。。。。先看看本文实现的效果:







 左图不是相同的指纹,右图为相同的指纹





ShapeContext算法中典型的实验图片。。。借来用用而已。。。

本文修改后的代码可以到这里:http://download.csdn.net/source/2515577下载,本文的代码改自GoogleCode的OpenSource。

     
网上已经有很多封装好的SURF算法,这里我挑OpenSURF.OpenSURF在GoogleCode的地址http://code.google.com/p/opensurf1/,在【Source】
Tab里有C++版和C#版,C++版依赖Opencv,C#版不依赖OpenCV,我选择了C#版裸奔SURF。。。。在使用OpenSURF的C#版发现两个问题:

1。没有Match模块,所以这个就得自己写了;

2。OpenSURF for C#竟然大量使用GetPixel() ...... -. -||| ......做过图像处理的兄弟都应该知道GetPixel()的效率...........这个也得自己改改。

解决问题1:C++版包含了Match模块,所以我就参考C++版的,写成C#。。。。。以下是我自己添加的Match代码:

源码打印

using System;  

using System.Collections.Generic;  

using System.Text;  

using OpenSURFcs;  

  

class Utils  

{  

    private const float FLT_MAX = 3.402823466e+38F;        /* max value */  

  

    public static List<IPoint>[] getMatches(List<IPoint> ipts1, List<IPoint> ipts2)  

    {  

        double dist;  

        double d1, d2;  

        IPoint match = new IPoint();  

  

        List<IPoint>[] matches = new List<IPoint>[2];  

        matches[0] = new List<IPoint>();  

        matches[1] = new List<IPoint>();  

  

        for (int i = 0; i < ipts1.Count; i++)  

        {  

            d1 = d2 = FLT_MAX;  

  

            for (int j = 0; j < ipts2.Count; j++)  

            {  

                dist = GetDistance(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 < 0.77) //越小Match点越少  

            {  

                matches[0].Add(ipts1[i]);  

                matches[1].Add(match);  

            }  

        }  

        return matches;  

    }  

  

    private static  double GetDistance(IPoint ip1, IPoint ip2)  

    {  

        float sum = 0.0f;  

        for (int i = 0; i < 64; ++i)  

            sum += (ip1.descriptor[i] - ip2.descriptor[i]) * (ip1.descriptor[i] - ip2.descriptor[i]);  

        return Math.Sqrt(sum);  

    }  

}  

解决问题2:参考网上的代码,把IntegralImage.cs的FromImage(Bitmap
image)函数改成:

源码打印

 public static IntegralImage FromImage(Bitmap image)  

  {  

      IntegralImage pic = new IntegralImage(image.Width, image.Height);  

      float rowsum = 0;  

/*      for (int y = 0; y < image.Height; y++) 

      { 

          rowsum = 0; 

          for (int x = 0; x < image.Width; x++) 

          { 

              Color c = image.GetPixel(x, y); 

              rowsum += (cR * c.R + cG * c.G + cB * c.B) / 255f; 

 

              // integral image is rowsum + value above    

              if(y==0) 

                  pic[0, x] = rowsum; 

              else 

                  pic[y, x] = rowsum + pic[y - 1, x]; 

          } 

      }*/  

       

      BitmapData dataIn = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  

        

      unsafe  

      {  

          byte* pIn = (byte*)(dataIn.Scan0.ToPointer());   

          for (int y = 0; y < dataIn.Height; y++)  

          {  

              rowsum = 0;  

              for (int x = 0; x < dataIn.Width; x++)  

              {  

                  int cb = (byte)( pIn[0]);  

                  int cg = (byte)(pIn[1]);  

                  int cr = (byte)(pIn[2]);  

  

                  // 0 1 2代表的次序是B G R  

                  rowsum += (cR * cr + cG * cg + cB * cb) / 255f;  

                  // integral image is rowsum + value above     

                  if (y == 0)  

                      pic[0, x] = rowsum;  

                  else  

                      pic[y, x] = rowsum + pic[y - 1, x];  

  

                  pIn += 3;  

              }  

              pIn += dataIn.Stride - dataIn.Width * 3;   

          }  

      }  

      image.UnlockBits(dataIn);   

      return pic;  

  }  

OK,解决了以上两个问题,OpenSURF的C#版功能算是跟C++差不多了。。。。当然,C#跟C++的速度还有点差距的。。。。接下来就说说如何提高识别率,有两个关键的系数要微调的:

1。FastHessian.getIpoints(0.0001f,
5, 2, iimg);的第一个参数决定了特征点的数量,越小则特征点越多;

2。在Utils.cs里面的if
(d1 / d2 < 0.77) //越小匹配的点越少,但误判断点也越少;越大匹配的点越多,但误判断点也越多。

以上两个要微调的函数就要具体情况具体微调了。。。。

原文链接:http://blog.csdn.net/hellogv/article/details/5712394
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv c#