分享

基于Aforge的手势识别之二~~~单点手势识别

 jicemoon 2015-04-28

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

       本文把Aforge的运动识别前面介绍的手写识别融合在一起,实现单个手指的手势识别。下图演示了本文代码运行的结果,图片有点大,请稍候。。。

       我预先让程序学习了B和C这两个字母,然后通过手指的手势识别向程序绘画图形,所以点击recorgize时,就自动把图形的特征对应的字母给识别出来了。

      这个例子关键部分在于如何灵活运用Aforge的运动识别,如何判断是要画图,还是普通的移来移去呢?在这里,我判断移动对象的大小,当突然面积增大(即两个指套合并)则开始绘图(手势识别的开始),当拆开再合并则为解除绘图(手势识别的结束),说白了就是用一个当前状态=!当前状态去做。

      本文的代码可以到这里下载:http://download.csdn.net/source/2313846

      下面贴出运动判断的核心代码:

  1. private void videoSourcePlayer1_NewFrame( object sender, ref Bitmap image )  
  2. {  
  3.     nowImg = (Bitmap)image.Clone();  
  4.   
  5.     Bitmap objectImage = colorFilter.Apply( image );  
  6.   
  7.     // lock image for further processing  
  8.     BitmapData objectData = objectImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),  
  9.         ImageLockMode.ReadOnly, image.PixelFormat );  
  10.   
  11.     // grayscaling  
  12.     UnmanagedImage grayImage = grayFilter.Apply( new UnmanagedImage( objectData ) );  
  13.   
  14.     // unlock image  
  15.     objectImage.UnlockBits( objectData );  
  16.   
  17.     // locate blobs   
  18.     blobCounter1.ProcessImage( grayImage );  
  19.     List<Rectangle> rects = new List<Rectangle>();  
  20.     rects.AddRange(blobCounter1.GetObjectsRectangles());  
  21.   
  22.     if ( rects.Count >0 )  
  23.     {  
  24.         #region 去掉内部和黏在一起的对象  
  25.         for (int i = 0; i < rects.Count - 1; i++)  
  26.         {  
  27.             //true表示X轴上不能相交,false表示相交  
  28.             Boolean isNoTouchX = Math.Max(rects[i + 1].Right , rects[i].Right) - Math.Min(rects[i + 1].Left ,rects[i].Left) > (rects[i].Width + rects[i + 1].Width);  
  29.             //true表示Y轴上不能相交,false表示相交  
  30.             Boolean isNoTouchY = Math.Max(rects[i + 1].Bottom, rects[i].Bottom) - Math.Min(rects[i + 1].Top, rects[i].Top) > (rects[i].Height + rects[i + 1].Height);  
  31.             if (isNoTouchX == false && isNoTouchY == false)//如果两个对象相交  
  32.             {  
  33.                 Rectangle rect = new Rectangle(Math.Min(rects[i].Left, rects[i + 1].Left),  
  34.                     Math.Min(rects[i].Top, rects[i + 1].Top),  
  35.                     Math.Max(rects[i].Right, rects[i + 1].Right) - Math.Min(rects[i].Left, rects[i + 1].Left),  
  36.                     Math.Max(rects[i].Bottom, rects[i + 1].Bottom) - Math.Min(rects[i].Top, rects[i + 1].Top));  
  37.                 rects.RemoveAt(i + 1);  
  38.                 rects.RemoveAt(i);  
  39.   
  40.                 rects.Add(rect);  
  41.                 i = 0;  
  42.             }  
  43.         }  
  44.         #endregion  
  45.  
  46.         #region 画出表示点  
  47.         Rectangle objectRect = rects[0];  
  48.   
  49.         int oldSize=oldRect.Width+oldRect.Height;  
  50.         int nowSize=rects[0].Width+rects[0].Height;  
  51.   
  52.         if (nowSize > (oldSize * 1.2))//如果突然变大,即两个指套合并  
  53.         {  
  54.             isCapture =!isCapture;  
  55.             clsHandWrite.Clear();  
  56.         }  
  57.   
  58.         Graphics g = Graphics.FromImage(image);  
  59.   
  60.         if (isCapture)//如果捕捉到对象  
  61.         {  
  62.             Pen pen = new Pen(Color.FromArgb(255, 0, 0), 3);  
  63.             g.DrawRectangle(pen, objectRect);  
  64.             int x = (objectRect.Left + objectRect.Width / 2) * pbDraw.Width / videoSourcePlayer1.Width;  
  65.             int y = (objectRect.Top + objectRect.Height / 2) * pbDraw.Height / videoSourcePlayer1.Height;  
  66.                     clsHandWrite.Draw(x,y );  
  67.         }  
  68.         else//如果没有捕捉到对象  
  69.         {  
  70.             Pen pen = new Pen(Color.FromArgb(160, 255, 160), 3);  
  71.             g.DrawRectangle(pen, objectRect);  
  72.         }  
  73.   
  74.         g.Dispose();  
  75.      
  76.         #endregion  
  77.   
  78.         oldRect = rects[0];  
  79.           
  80.     }  
  81.   
  82.     UpdateObjectPicture(objectImage );  
  83.       
  84. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多