简单粗糙的指尖检测方法(FingerTips Detection)

本文探讨了在人机交互领域中,通过检测指尖来提升交互丰富度与灵活性的方法。提出了两种基于手几何特征的简单指尖检测算法:重心距离法与曲率分析法。重心距离法通过计算手轮廓边缘点到重心的距离来识别手指;曲率分析法则利用手指与相邻区域的曲率差异来定位指尖。实验结果表明,这两种方法在实际应用中展现出良好的检测效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在人机交互领域,如果可以比较好的检测指尖,对于交互的丰富度、灵活性来说是有很大提升的。目前指尖检测的方法也很多,我这里稍微尝试了下简单了两种。这两种方法都借助了手的几何特征,简单但比较粗糙,鲁棒性不够。

 

方法一:重心距离法

        见下图,红色点是手的重心,那么手的边缘的所有点与重心点的距离按顺时针方向或者逆时针方向遍历,就会出现五个峰值,分别是五个手指,这样我们就可以简单找到了。如果你是只伸出一两个手指,那么就只有一两个峰值了。

简单的代码如下:

1、对图像做高斯模糊;

2、肤色分割(背景不要有类肤色,如果有,就需要加其他信息来排除干扰);

3、找到手轮廓;

4、计算矩,即重心;

5、寻找指尖。

 

[cpp]  view plain copy
  1. // Simple FingerTips Detection  
  2. // Author : Zouxy  
  3. // Date   : 2013-3-23  
  4. // HomePage : http://blog.youkuaiyun.com/zouxy09  
  5. // Email  : zouxy09@qq.com  
  6.   
  7. #include "opencv2/opencv.hpp"  
  8.   
  9. using namespace cv;  
  10. using namespace std;  
  11.   
  12. void skinExtract(const Mat &frame, Mat &skinArea);  
  13.   
  14. int main(int argc, char* argv[])  
  15. {  
  16.     Mat frame, skinArea;  
  17.     VideoCapture capture;  
  18.   
  19.     capture.open(0);  
  20.     if (!capture.isOpened())  
  21.     {  
  22.         cout<<"No camera!\n"<<endl;  
  23.         return -1;  
  24.     }  
  25.   
  26.     while (1)  
  27.     {  
  28.         capture >> frame;  
  29.         //Mat frame = imread("fingertips(1).jpg");  
  30.         if (frame.empty())  
  31.             break;  
  32.   
  33.         skinArea.create(frame.rows, frame.cols, CV_8UC1);  
  34.         skinExtract(frame, skinArea);  
  35.         Mat show_img;  
  36.         frame.copyTo(show_img, skinArea);  
  37.   
  38.         vector<vector<Point> > contours;  
  39.         vector<Vec4i> hierarchy;  
  40.   
  41.         //寻找轮廓  
  42.         findContours(skinArea, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);  
  43.   
  44.         // 找到最大的轮廓  
  45.         int index;  
  46.         double area, maxArea(0);  
  47.         for (int i=0; i < contours.size(); i++)  
  48.         {  
  49.             area = contourArea(Mat(contours[i]));  
  50.             if (area > maxArea)  
  51.             {  
  52.                 maxArea = area;  
  53.                 index = i;  
  54.             }             
  55.         }  
  56.   
  57.         //drawContours(frame, contours, index, Scalar(0, 0, 255), 2, 8, hierarchy );  
  58.           
  59.         Moments moment = moments(skinArea, true);  
  60.         Point center(moment.m10/moment.m00, moment.m01/moment.m00);  
  61.         circle(show_img, center, 8 ,Scalar(0, 0, 255), CV_FILLED);  
  62.   
  63.         // 寻找指尖  
  64.         vector<Point> couPoint = contours[index];  
  65.         vector<Point> fingerTips;  
  66.         Point tmp;  
  67.         int max(0), count(0), notice(0);  
  68.         for (int i = 0; i < couPoint.size(); i++)  
  69.         {  
  70.             tmp = couPoint[i];  
  71.             int dist = (tmp.x - center.x) * (tmp.x - center.x) + (tmp.y - center.y) * (tmp.y - center.y);  
  72.             if (dist > max)  
  73.             {  
  74.                 max = dist;  
  75.                 notice = i;  
  76.             }  
  77.   
  78.             // 计算最大值保持的点数,如果大于40(这个值需要设置,本来想根据max值来设置,  
  79.             // 但是不成功,不知道为何),那么就认为这个是指尖  
  80.             if (dist != max)  
  81.             {  
  82.                 count++;  
  83.                 if (count > 40)  
  84.                 {  
  85.                     count = 0;  
  86.                     max = 0;  
  87.                     bool flag = false;  
  88.                     // 低于手心的点不算  
  89.                     if (center.y < couPoint[notice].y )  
  90.                         continue;  
  91.                     // 离得太近的不算  
  92.                     for (int j = 0; j < fingerTips.size(); j++)  
  93.                     {  
  94.                         if (abs(couPoint[notice].x - fingerTips[j].x) < 20)  
  95.                         {  
  96.                             flag = true;  
  97.                             break;  
  98.                         }  
  99.                     }  
  100.                     if (flag) continue;  
  101.                     fingerTips.push_back(couPoint[notice]);  
  102.                     circle(show_img, couPoint[notice], 6 ,Scalar(0, 255, 0), CV_FILLED);  
  103.                     line(show_img, center, couPoint[notice], Scalar(255, 0, 0), 2);               
  104.                 }  
  105.             }  
  106.         }  
  107.   
  108.         imshow("show_img", show_img);  
  109.   
  110.         if ( cvWaitKey(20) == 'q' )  
  111.             break;  
  112.     }  
  113.   
  114.     return 0;  
  115. }  
  116.   
  117. //肤色提取,skinArea为二值化肤色图像  
  118. void skinExtract(const Mat &frame, Mat &skinArea)  
  119. {  
  120.     Mat YCbCr;  
  121.     vector<Mat> planes;  
  122.   
  123.     //转换为YCrCb颜色空间  
  124.     cvtColor(frame, YCbCr, CV_RGB2YCrCb);  
  125.     //将多通道图像分离为多个单通道图像  
  126.     split(YCbCr, planes);   
  127.   
  128.     //运用迭代器访问矩阵元素  
  129.     MatIterator_<uchar> it_Cb = planes[1].begin<uchar>(),  
  130.                         it_Cb_end = planes[1].end<uchar>();  
  131.     MatIterator_<uchar> it_Cr = planes[2].begin<uchar>();  
  132.     MatIterator_<uchar> it_skin = skinArea.begin<uchar>();  
  133.   
  134.     //人的皮肤颜色在YCbCr色度空间的分布范围:100<=Cb<=127, 138<=Cr<=170  
  135.     for( ; it_Cb != it_Cb_end; ++it_Cr, ++it_Cb, ++it_skin)  
  136.     {  
  137.         if (138 <= *it_Cr &&  *it_Cr <= 170 && 100 <= *it_Cb &&  *it_Cb <= 127)  
  138.             *it_skin = 255;  
  139.         else  
  140.             *it_skin = 0;  
  141.     }  
  142.   
  143.     //膨胀和腐蚀,膨胀可以填补凹洞(将裂缝桥接),腐蚀可以消除细的凸起(“斑点”噪声)  
  144.     dilate(skinArea, skinArea, Mat(5, 5, CV_8UC1), Point(-1, -1));  
  145.     erode(skinArea, skinArea, Mat(5, 5, CV_8UC1), Point(-1, -1));  
  146. }  


 

方法二:曲率分析法

       见下图,可以看到,在指尖和两指之间的凹槽的曲率是最大的。假设手的轮廓是通过一系列点{Pi}来保存的,那么我们可以通过 [Pi, Pi-k [Pi, Pi+k两个向量的内积(点乘)来寻找高曲率的点,如下图的Φ和β的计算结果,因为他们的两个向量的夹角趋向于90度,也就是它们的内积趋向于0,所以内积结果越小,曲率越大,我们只需要设置一个阈值,就可以把手的指尖和两指之间的凹槽都可以找到。

       那么如何分辨指尖和两指之间的凹槽呢?我们可以通过两个向量的叉乘来计算,我们想象下手处于3D空间中,那么就还有一个z方向,β中两个向量的叉乘是大于0的(叉乘方向垂直纸面朝向你,z轴正方向),而Φ处的叉乘是小于0的(叉乘方向垂直纸面远离你,z轴负方向)。

        综上,通过在每个点上构造两个向量,比较他们的点乘和叉乘就可以确定指尖了。

简单的代码如下:

1、对图像做高斯模糊;

2、肤色分割(背景不要有类肤色,如果有,就需要加其他信息来排除干扰);

3、找到手轮廓;

4、寻找指尖。

 

[cpp]  view plain copy
  1. // Simple FingerTips Detection Using Curvature Determination  
  2. // Author : Zouxy  
  3. // Date   : 2013-3-23  
  4. // HomePage : http://blog.youkuaiyun.com/zouxy09  
  5. // Email  : zouxy09@qq.com  
  6.   
  7. #include "opencv2/opencv.hpp"  
  8.   
  9. using namespace cv;  
  10. using namespace std;  
  11.   
  12. void skinExtract(const Mat &frame, Mat &skinArea);  
  13.   
  14. int main(int argc, char* argv[])  
  15. {  
  16.     Mat frame, skinArea;  
  17.     VideoCapture capture;  
  18.   
  19.     capture.open(0);  
  20.     if (!capture.isOpened())  
  21.     {  
  22.         cout<<"No camera!\n"<<endl;  
  23.         return -1;  
  24.     }  
  25.   
  26.     while (1)  
  27.     {  
  28.         capture >> frame;  
  29.         //Mat frame = imread("fingertips(1).jpg");  
  30.         if (frame.empty())  
  31.             break;  
  32.   
  33.         GaussianBlur(frame, frame, Size(3, 3), 0);  
  34.         skinArea.create(frame.rows, frame.cols, CV_8UC1);  
  35.         skinExtract(frame, skinArea);  
  36.         Mat show_img;  
  37.         frame.copyTo(show_img, skinArea);  
  38.   
  39.         vector<vector<Point> > contours;  
  40.         vector<Vec4i> hierarchy;  
  41.   
  42.         //寻找轮廓  
  43.         findContours(skinArea, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);  
  44.   
  45.         // 找到最大的轮廓  
  46.         int index;  
  47.         double area, maxArea(0);  
  48.         for (int i=0; i < contours.size(); i++)  
  49.         {  
  50.             area = contourArea(Mat(contours[i]));  
  51.             if (area > maxArea)  
  52.             {  
  53.                 maxArea = area;  
  54.                 index = i;  
  55.             }             
  56.         }  
  57.   
  58.         //drawContours(frame, contours, index, Scalar(0, 0, 255), 2, 8, hierarchy );  
  59.           
  60.         Moments moment = moments(skinArea, true);  
  61.         Point center(moment.m10/moment.m00, moment.m01/moment.m00);  
  62.         circle(show_img, center, 8 ,Scalar(0, 0, 255), CV_FILLED);  
  63.   
  64.         // 寻找指尖  
  65.         vector<Point> couPoint = contours[index];  
  66.         int max(0), count(0), notice(0);  
  67.         vector<Point> fingerTips;  
  68.         Point p, q, r;  
  69.         for (int i = 5; (i < (couPoint.size() - 5)) && couPoint.size(); i++)  
  70.         {  
  71.             q = couPoint[i - 5];  
  72.             p = couPoint[i];  
  73.             r = couPoint[i + 5];  
  74.             int dot = (q.x - p.x ) * (q.y - p.y) + (r.x - p.x ) * (r.y - p.y);  
  75.             if (dot < 20 && dot > -20)  
  76.             {  
  77.                 int cross = (q.x - p.x ) * (r.y - p.y) - (r.x - p.x ) * (q.y - p.y);  
  78.                 if (cross > 0)  
  79.                 {  
  80.                     fingerTips.push_back(p);  
  81.                     circle(show_img, p, 5 ,Scalar(255, 0, 0), CV_FILLED);  
  82.                     line(show_img, center, p, Scalar(255, 0, 0), 2);      
  83.                 }  
  84.             }  
  85.         }  
  86.   
  87.         imshow("show_img", show_img);  
  88.   
  89.         if ( cvWaitKey(20) == 'q' )  
  90.             break;  
  91.     }  
  92.   
  93.     return 0;  
  94. }  
  95.   
  96. //肤色提取,skinArea为二值化肤色图像  
  97. void skinExtract(const Mat &frame, Mat &skinArea)  
  98. {  
  99.     Mat YCbCr;  
  100.     vector<Mat> planes;  
  101.   
  102.     //转换为YCrCb颜色空间  
  103.     cvtColor(frame, YCbCr, CV_RGB2YCrCb);  
  104.     //将多通道图像分离为多个单通道图像  
  105.     split(YCbCr, planes);   
  106.   
  107.     //运用迭代器访问矩阵元素  
  108.     MatIterator_<uchar> it_Cb = planes[1].begin<uchar>(),  
  109.                         it_Cb_end = planes[1].end<uchar>();  
  110.     MatIterator_<uchar> it_Cr = planes[2].begin<uchar>();  
  111.     MatIterator_<uchar> it_skin = skinArea.begin<uchar>();  
  112.   
  113.     //人的皮肤颜色在YCbCr色度空间的分布范围:100<=Cb<=127, 138<=Cr<=170  
  114.     for( ; it_Cb != it_Cb_end; ++it_Cr, ++it_Cb, ++it_skin)  
  115.     {  
  116.         if (138 <= *it_Cr &&  *it_Cr <= 170 && 100 <= *it_Cb &&  *it_Cb <= 127)  
  117.             *it_skin = 255;  
  118.         else  
  119.             *it_skin = 0;  
  120.     }  
  121.   
  122.     //膨胀和腐蚀,膨胀可以填补凹洞(将裂缝桥接),腐蚀可以消除细的凸起(“斑点”噪声)  
  123.     dilate(skinArea, skinArea, Mat(5, 5, CV_8UC1), Point(-1, -1));  
  124.     erode(skinArea, skinArea, Mat(5, 5, CV_8UC1), Point(-1, -1));  
  125. }  

 

效果:

 

如要深入,我们还可以参考以下资源:

finger-detection-and-gesture-recognition [Code]

Hand and Finger Detection using JavaCV[Project]

Hand and fingers detection[Code]

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值