织梦CMS - 轻松建站从此开始!

罗索

如何利用差分方法实现虚拟选择菜单

落鹤生 发布于 2010-04-25 16:52 点击:次 
一些摄像头程序实现了用差分方法来选择菜单,就是你不断用运动的物体干扰监测目标,它会出现进度条,直到选中,例如camgoo,实际上它实现起来却非常的简单。不过它的作用却是很大。
TAG:

一些摄像头程序实现了用差分方法来选择菜单,就是你不断用运动的物体干扰监测目标,它会出现进度条,直到选中,例如camgoo,实际上它实现起来却非常的简单。不过它的作用却是很大。

下图绿色表示选择的进度,

 


第一步准备

 

  1. //颜色转化 
  2. cvCvtColor( image, grey, CV_BGR2GRAY );     
  3. //差分 
  4. cvAbsDiff( grey,prev_grey, abs_img ); 
  5.  
  6. //清理低的值,这样抗干扰强 
  7. cvThreshold( abs_img, abs_img, 30, 255, CV_THRESH_BINARY); 


第二步:

  1. //定义命中器的结构 
  2. typedef struct hit_chooser 
  3. {  
  4.     float Perc_x; 
  5.     float Perc_y; 
  6.     float Perc_width; 
  7.     float Perc_height; 
  8.     int hit_count; 
  9.     bool isUse; 
  10. } hit_chooser; 
  11.  
  12. //初始化数据,定义一个监视区域 
  13. hit_chooser Hit_Objects[WW_MAX_HIT_COUNT]; 
  14. Hit_Objects[0].isUse  =true
  15. Hit_Objects[0].Perc_x   = 0.7;  
  16. Hit_Objects[0].Perc_y   = 0.6; 
  17. Hit_Objects[0].Perc_width = 0.1; 
  18. Hit_Objects[0].Perc_height = 0.1; 
  19. Hit_Objects[0].hit_count = 0; 

第三步,在需要检测的地方加入 getHitCounts(Hit_Objects);

 

  1. WW_RETURN HumanMotion::getHitCounts(hit_chooser *Hit_Objects) 
  2. /************************************************* 
  3.   Function: 
  4.   Description:  利用差分统计命中数目,返回该区域的命中次数      
  5.   Date:   2006-7-24 
  6.   Author:   
  7.   Input:                        
  8.   Output:         
  9.   Return:         
  10.   Others:          
  11. *************************************************/ 
  12.  const int w = abs_img->width; 
  13.  const int h = abs_img->height; 
  14.  float counter = 0; 
  15.  
  16.  for(int i=0;i<WW_MAX_HIT_COUNT;i++) 
  17.  { 
  18.   if(Hit_Objects[i].isUse) 
  19.   { 
  20.    if(Hit_Objects[i].hit_count<100) 
  21.    { 
  22.     int x = Hit_Objects[i].Perc_x * w; 
  23.     int y = Hit_Objects[i].Perc_y * h; 
  24.     int Perc_width = Hit_Objects[i].Perc_width * w; 
  25.     int Perc_height = Hit_Objects[i].Perc_height * h; 
  26.     cvSetImageROI( abs_img, cvRect( x, y,Perc_width,Perc_height)); 
  27.     int num  = cvCountNonZero(abs_img); 
  28.     cvResetImageROI(abs_img); 
  29.     if(num > 10) 
  30.     {      
  31.      Hit_Objects[i].hit_count+=2; //升值比降值快一倍 
  32.     } 
  33.     else 
  34.     { 
  35.      if(Hit_Objects[i].hit_count>0) 
  36.      { 
  37.       Hit_Objects[i].hit_count --; 
  38.      } 
  39.     } 
  40.    } 
  41.   } 
  42.  } 
  43.  return WW_OK; 

 
第四步显示出来

 

  1. void showHitObject(void
  2.  
  3.   for(int i=0;i<WW_MAX_HIT_COUNT;i++) 
  4.   { 
  5.    if(Hit_Objects[i].isUse) 
  6.    { 
  7.     //draw a circle 
  8.     glPushMatrix(); 
  9.     glTranslatef(Hit_Objects[i].Perc_x*room_size/3 , 0.0,Hit_Objects[i].Perc_y*room_size/2); 
  10.     glRotatef(90,1,0,0); 
  11.     glColor3f(1,1,1); 
  12.     dsDrawTours(0.2,0.3,45,45,false);    
  13.  
  14.     if(Hit_Objects[i].hit_count>0 && Hit_Objects[i].hit_count<100) 
  15.     { 
  16.      //show hit   
  17.      int hit = (float)Hit_Objects[i].hit_count/100*45;     
  18.      glTranslatef(0.0, 0.0,0.1); 
  19.      dsDrawTours(0.2,0.3,45,hit,true); 
  20.     } 
  21.  
  22.     if(Hit_Objects[i].hit_count>=100) 
  23.     { 
  24.      //show full 
  25.      glColor3f(0,1,0); 
  26.      glTranslatef(0.0, 0.0,0.1); 
  27.      dsDrawTours(0.2,0.3,45,45,false); 
  28.     } 
  29.     glPopMatrix(); 
  30.    } 
  31.   } 
  32.  
  33.  
  34. //显示圆环 
  35. void dsDrawTours(float inRadio,float ExRadio,int numc,int endnumc,bool needcolor) 
  36.    int i; 
  37.    double s, twopi,sinA,cosA,x,y; 
  38.  
  39.    twopi = 2 * 3.14; 
  40.    for (i = 0; i <endnumc; i++) 
  41.    {   
  42.       glBegin(GL_QUAD_STRIP);     
  43.    { 
  44.         
  45.    s = i; 
  46.    if(needcolor) glColor3f(0,s/numc/2+0.2,0); 
  47.  
  48.    s= s/numc*twopi; 
  49.             sinA = sin(s); 
  50.             cosA = cos(s); 
  51.             x = inRadio * sinA; 
  52.             y = inRadio * cosA; 
  53.             glVertex2f(x, y); 
  54.  
  55.             x = ExRadio * sinA; 
  56.             y = ExRadio * cosA; 
  57.             glVertex2f(x, y); 
  58.  
  59.    s = i+1; 
  60.    s= s/numc*twopi; 
  61.             sinA = sin(s); 
  62.    cosA = cos(s); 
  63.             x = inRadio * sinA; 
  64.             y = inRadio * cosA; 
  65.             glVertex2f(x, y); 
  66.  
  67.             x = ExRadio * sinA; 
  68.             y = ExRadio * cosA; 
  69.             glVertex2f(x, y);   
  70.  
  71.       } 
  72.       glEnd(); 
  73.    } 

 

(hardVB)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201004/9258.html]
本文出处:CSDN博客 作者:hardVB
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容