Once again, hello !!
In this post, we are going to cover Hand Gesture Tracking. It’s one of my favorite and fun topic. It is often considered as a tough task to perform Hand or finger tracking due to the limitations of the Background scenes. One such condition also exists in this program. You need to have a clear background in order for the program to successful be able to run.
Here is how we are going to perform it:
1) Convert the video frame from BGR to Gray
2) Perform a Threshold OTSU
3) Perform a GBlur
4) Find the Biggest Contour(this will be our hand)
5) Perform a convexHull and mark the ROI
6) Count the Items
7) Display it
Here is how we recognize the gestures/fingers.
If 1 finger on screen –> we’ll display “Single”
If 2 finger on screen –> we’ll display “Victory”
If 3 finger on screen –> we’ll display “Trio Fingers”
If 4 finger on screen –> we’ll display “Four Fingers”
If 5 finger on screen –> we’ll display “High Five”
You can change there text by editing the below code:
if(count==1)
strcpy(a,"Single");
else if(count==2)
strcpy(a,"Victory");
else if(count==3)
strcpy(a,"Trio Fingers");
else if(count==4)
strcpy(a,"Four Fingers");
else if(count==5)
strcpy(a,"High Five");
else
strcpy(a,"Hello There");
A look at the demo:
Once, again, we’ll start off by opening visual studio 2015 and creating a new project.
Loading all the required OpenCV includes and Dll’s and creating a new main.cpp file.
Copy and paste the below code and you can enjoy hand tracking.
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
using namespace cv;
using namespace std;
int main( int argc, const char** argv)
{
VideoCapture cam(0);
if(!cam.isOpened()){
cout >contours;
vectorhierarchy;
findContours(img_threshold,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,Point());
if(contours.size()>0){
size_t indexOfBiggestContour = -1;
size_t sizeOfBiggestContour = 0;
for (size_t i = 0; i sizeOfBiggestContour){
sizeOfBiggestContour = contours[i].size();
indexOfBiggestContour = i;
}
}
vector >hull(contours.size());
vector >hullPoint(contours.size());
vector >defects(contours.size());
vector >defectPoint(contours.size());
vector >contours_poly(contours.size());
Point2f rect_point[4];
vectorminRect(contours.size());
vector boundRect(contours.size());
for(size_t i=0;i5000){
convexHull(contours[i],hull[i],true);
convexityDefects(contours[i],hull[i],defects[i]);
if(indexOfBiggestContour==i){
minRect[i]=minAreaRect(contours[i]);
for(size_t k=0;k13*256){
/* int p_start=defects[i][k][0]; */
int p_end=defects[i][k][1];
int p_far=defects[i][k][2];
defectPoint[i].push_back(contours[i][p_far]);
circle(img_roi,contours[i][p_end],3,Scalar(0,255,0),2);
count++;
}
}
if(count==1)
strcpy(a,"Single");
else if(count==2)
strcpy(a,"Victory");
else if(count==3)
strcpy(a,"Trio Fingers");
else if(count==4)
strcpy(a,"Four Fingers");
else if(count==5)
strcpy(a,"High Five");
else
strcpy(a,"Hello There");
putText(img,a,Point(70,70),CV_FONT_HERSHEY_SIMPLEX,3,Scalar(255,0,0),2,8,false);
drawContours(img_threshold, contours, i,Scalar(255,255,0),2, 8, vector(), 0, Point() );
drawContours(img_threshold, hullPoint, i, Scalar(255,255,0),1, 8, vector(),0, Point());
drawContours(img_roi, hullPoint, i, Scalar(0,0,255),2, 8, vector(),0, Point() );
approxPolyDP(contours[i],contours_poly[i],3,false);
boundRect[i]=boundingRect(contours_poly[i]);
rectangle(img_roi,boundRect[i].tl(),boundRect[i].br(),Scalar(255,0,0),2,8,0);
minRect[i].points(rect_point);
for(size_t k=0;k
Happy Coding !!!
You have been posting really awesome tutorial man. Great job.