拳皇99超能力队:小玩OpenSURF图像识别

来源:百度文库 编辑:偶看新闻 时间:2024/05/03 15:19:48

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

view plaincopy to clipboardprint?

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

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

view plaincopy to clipboardprint?

  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.               // integral image is rowsum + value above  
  13.               if(y==0)
  14.                   pic[0, x] = rowsum;
  15.               else
  16.                   pic[y, x] = rowsum + pic[y - 1, x];
  17.           }
  18.       }*/
  19.       BitmapData dataIn = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); 
  20. unsafe
  21.       { 
  22. byte* pIn = (byte*)(dataIn.Scan0.ToPointer());  
  23. for (int y = 0; y < dataIn.Height; y++) 
  24.           { 
  25.               rowsum = 0; 
  26. for (int x = 0; x < dataIn.Width; x++) 
  27.               { 
  28. int cb = (byte)( pIn[0]); 
  29. int cg = (byte)(pIn[1]); 
  30. int cr = (byte)(pIn[2]); 
  31. // 0 1 2代表的次序是B G R
  32.                   rowsum += (cR * cr + cG * cg + cB * cb) / 255f; 
  33. // integral image is rowsum + value above  
  34. if (y == 0) 
  35.                       pic[0, x] = rowsum; 
  36. else
  37.                       pic[y, x] = rowsum + pic[y - 1, x]; 
  38.                   pIn += 3; 
  39.               } 
  40.               pIn += dataIn.Stride - dataIn.Width * 3;  
  41.           } 
  42.       } 
  43.       image.UnlockBits(dataIn);  
  44. return pic; 
  45.   } 

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

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

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

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