小玩OpenSURF图像识别

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

 

OpenSURF is a clean, unintrusive SURF feature extraction library written in C++ with OpenCV.

The OpenSURF library was originally written in Visual Studio 2005, although a linux compatible makefile has been contributed and is available in the SVN directory.

 

      最近工作有点闲,有时间就玩点有意思的东西------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代码:

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using OpenSURFcs;  
  5.   
  6. class Utils  
  7. {  
  8.     private const float FLT_MAX = 3.402823466e+38F;        /* max value */  
  9.   
  10.     public static List<IPoint>[] getMatches(List<IPoint> ipts1, List<IPoint> ipts2)  
  11.     {  
  12.         double dist;  
  13.         double d1, d2;  
  14.         IPoint match = new IPoint();  
  15.   
  16.         List<IPoint>[] matches = new List<IPoint>[2];  
  17.         matches[0] = new List<IPoint>();  
  18.         matches[1] = new List<IPoint>();  
  19.   
  20.         for (int i = 0; i < ipts1.Count; i++)  
  21.         {  
  22.             d1 = d2 = FLT_MAX;  
  23.   
  24.             for (int j = 0; j < ipts2.Count; j++)  
  25.             {  
  26.                 dist = GetDistance(ipts1[i], ipts2[j]);  
  27.   
  28.                 if (dist < d1) // if this feature matches better than current best  
  29.                 {  
  30.                     d2 = d1;  
  31.                     d1 = dist;  
  32.                     match = ipts2[j];  
  33.                 }  
  34.                 else if (dist < d2) // this feature matches better than second best  
  35.                 {  
  36.                     d2 = dist;  
  37.                 }  
  38.             }  
  39.             // If match has a d1:d2 ratio < 0.65 ipoints are a match  
  40.             if (d1 / d2 < 0.77) //越小Match点越少  
  41.             {  
  42.                 matches[0].Add(ipts1[i]);  
  43.                 matches[1].Add(match);  
  44.             }  
  45.         }  
  46.         return matches;  
  47.     }  
  48.   
  49.     private static  double GetDistance(IPoint ip1, IPoint ip2)  
  50.     {  
  51.         float sum = 0.0f;  
  52.         for (int i = 0; i < 64; ++i)  
  53.             sum += (ip1.descriptor[i] - ip2.descriptor[i]) * (ip1.descriptor[i] - ip2.descriptor[i]);  
  54.         return Math.Sqrt(sum);  
  55.     }  
  56. }  

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

[c-sharp] view plain copy print ?
  1.  public static IntegralImage FromImage(Bitmap image)  
  2.   {  
  3.       IntegralImage pic = new IntegralImage(image.Width, image.Height);  
  4.       float rowsum = 0;  
  5. /*      for (int y = 0; y < image.Height; y++) 
  6.       { 
  7.           rowsum = 0; 
  8.           for (int x = 0; x < image.Width; x++) 
  9.           { 
  10.               Color c = image.GetPixel(x, y); 
  11.               rowsum += (cR * c.R + cG * c.G + cB * c.B) / 255f; 
  12.  
  13.               // integral image is rowsum + value above    
  14.               if(y==0) 
  15.                   pic[0, x] = rowsum; 
  16.               else 
  17.                   pic[y, x] = rowsum + pic[y - 1, x]; 
  18.           } 
  19.       }*/  
  20.        
  21.       BitmapData dataIn = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  
  22.         
  23.       unsafe  
  24.       {  
  25.           byte* pIn = (byte*)(dataIn.Scan0.ToPointer());   
  26.           for (int y = 0; y < dataIn.Height; y++)  
  27.           {  
  28.               rowsum = 0;  
  29.               for (int x = 0; x < dataIn.Width; x++)  
  30.               {  
  31.                   int cb = (byte)( pIn[0]);  
  32.                   int cg = (byte)(pIn[1]);  
  33.                   int cr = (byte)(pIn[2]);  
  34.   
  35.                   // 0 1 2代表的次序是B G R  
  36.                   rowsum += (cR * cr + cG * cg + cB * cb) / 255f;  
  37.                   // integral image is rowsum + value above     
  38.                   if (y == 0)  
  39.                       pic[0, x] = rowsum;  
  40.                   else  
  41.                       pic[y, x] = rowsum + pic[y - 1, x];  
  42.   
  43.                   pIn += 3;  
  44.               }  
  45.               pIn += dataIn.Stride - dataIn.Width * 3;   
  46.           }  
  47.       }  
  48.       image.UnlockBits(dataIn);   
  49.       return pic;  
  50.   }  

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

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

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

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值