一些摄像头程序实现了用差分方法来选择菜单,就是你不断用运动的物体干扰监测目标,它会出现进度条,直到选中,例如camgoo,实际上它实现起来却非常的简单。不过它的作用却是很大。
下图绿色表示选择的进度,
第一步准备
-
- cvCvtColor( image, grey, CV_BGR2GRAY );
-
- cvAbsDiff( grey,prev_grey, abs_img );
-
-
- cvThreshold( abs_img, abs_img, 30, 255, CV_THRESH_BINARY);
第二步:
-
- typedef struct hit_chooser
- {
- float Perc_x;
- float Perc_y;
- float Perc_width;
- float Perc_height;
- int hit_count;
- bool isUse;
- } hit_chooser;
-
-
- hit_chooser Hit_Objects[WW_MAX_HIT_COUNT];
- Hit_Objects[0].isUse =true;
- Hit_Objects[0].Perc_x = 0.7;
- Hit_Objects[0].Perc_y = 0.6;
- Hit_Objects[0].Perc_width = 0.1;
- Hit_Objects[0].Perc_height = 0.1;
- Hit_Objects[0].hit_count = 0;
第三步,在需要检测的地方加入 getHitCounts(Hit_Objects);
- WW_RETURN HumanMotion::getHitCounts(hit_chooser *Hit_Objects)
-
-
-
-
-
-
-
-
-
-
- {
- const int w = abs_img->width;
- const int h = abs_img->height;
- float counter = 0;
-
- for(int i=0;i<WW_MAX_HIT_COUNT;i++)
- {
- if(Hit_Objects[i].isUse)
- {
- if(Hit_Objects[i].hit_count<100)
- {
- int x = Hit_Objects[i].Perc_x * w;
- int y = Hit_Objects[i].Perc_y * h;
- int Perc_width = Hit_Objects[i].Perc_width * w;
- int Perc_height = Hit_Objects[i].Perc_height * h;
- cvSetImageROI( abs_img, cvRect( x, y,Perc_width,Perc_height));
- int num = cvCountNonZero(abs_img);
- cvResetImageROI(abs_img);
- if(num > 10)
- {
- Hit_Objects[i].hit_count+=2;
- }
- else
- {
- if(Hit_Objects[i].hit_count>0)
- {
- Hit_Objects[i].hit_count --;
- }
- }
- }
- }
- }
- return WW_OK;
- }
第四步显示出来
- void showHitObject(void)
- {
-
- for(int i=0;i<WW_MAX_HIT_COUNT;i++)
- {
- if(Hit_Objects[i].isUse)
- {
-
- glPushMatrix();
- glTranslatef(Hit_Objects[i].Perc_x*room_size/3 , 0.0,Hit_Objects[i].Perc_y*room_size/2);
- glRotatef(90,1,0,0);
- glColor3f(1,1,1);
- dsDrawTours(0.2,0.3,45,45,false);
-
- if(Hit_Objects[i].hit_count>0 && Hit_Objects[i].hit_count<100)
- {
-
- int hit = (float)Hit_Objects[i].hit_count/100*45;
- glTranslatef(0.0, 0.0,0.1);
- dsDrawTours(0.2,0.3,45,hit,true);
- }
-
- if(Hit_Objects[i].hit_count>=100)
- {
-
- glColor3f(0,1,0);
- glTranslatef(0.0, 0.0,0.1);
- dsDrawTours(0.2,0.3,45,45,false);
- }
- glPopMatrix();
- }
- }
-
- }
-
-
- void dsDrawTours(float inRadio,float ExRadio,int numc,int endnumc,bool needcolor)
- {
- int i;
- double s, twopi,sinA,cosA,x,y;
-
- twopi = 2 * 3.14;
- for (i = 0; i <endnumc; i++)
- {
- glBegin(GL_QUAD_STRIP);
- {
-
- s = i;
- if(needcolor) glColor3f(0,s/numc/2+0.2,0);
-
- s= s/numc*twopi;
- sinA = sin(s);
- cosA = cos(s);
- x = inRadio * sinA;
- y = inRadio * cosA;
- glVertex2f(x, y);
-
- x = ExRadio * sinA;
- y = ExRadio * cosA;
- glVertex2f(x, y);
-
- s = i+1;
- s= s/numc*twopi;
- sinA = sin(s);
- cosA = cos(s);
- x = inRadio * sinA;
- y = inRadio * cosA;
- glVertex2f(x, y);
-
- x = ExRadio * sinA;
- y = ExRadio * cosA;
- glVertex2f(x, y);
-
- }
- glEnd();
- }
- }
(hardVB) |