我正在处理数字信号,尝试检测指尖,类似于此处提供的解决方案:使用 JavaCV 进行手和手指检测。
但是,我没有使用 JavaCV,而是使用 Android 版 OpenCV,这略有不同。 我已经成功完成了教程中介绍的所有步骤,但过滤了凸包和凸性缺陷。这就是我的图像的样子:
这是另一种分辨率的图像:
正如你可以清楚地看到的,有很多黄点(凸包),也有很多红点(凸缺陷)。有时2个黄点之间没有红点,这很奇怪(凸包是如何计算的?)
我需要的是创建类似的过滤功能,如之前提供的链接,但使用 OpenCV 的数据结构。
凸包是 MatOfInt 的类型... 凸性缺陷是 MatOfInt4 的类型...
我还创建了一些额外的数据结构,因为愚蠢的 OpenCV 以不同的方法使用包含相同数据的不同类型的数据......
convexHullMatOfInt = new MatOfInt();
convexHullPointArrayList = new ArrayList<Point>();
convexHullMatOfPoint = new MatOfPoint();
convexHullMatOfPointArrayList = new ArrayList<MatOfPoint>();
这是我到目前为止所做的,但效果不佳。问题可能在于以错误的方式转换数据:
创建凸包和凸性缺陷:
public void calculateConvexHulls()
{
convexHullMatOfInt = new MatOfInt();
convexHullPointArrayList = new ArrayList<Point>();
convexHullMatOfPoint = new MatOfPoint();
convexHullMatOfPointArrayList = new ArrayList<MatOfPoint>();
try {
//Calculate convex hulls
if(aproximatedContours.size() > 0)
{
Imgproc.convexHull( aproximatedContours.get(0), convexHullMatOfInt, false);
for(int j=0; j < convexHullMatOfInt.toList().size(); j++)
convexHullPointArrayList.add(aproximatedContours.get(0).toList().get(convexHullMatOfInt.toList().get(j)));
convexHullMatOfPoint.fromList(convexHullPointArrayList);
convexHullMatOfPointArrayList.add(convexHullMatOfPoint);
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Calculate convex hulls failed.", "Details below");
e.printStackTrace();
}
}
public void calculateConvexityDefects()
{
mConvexityDefectsMatOfInt4 = new MatOfInt4();
try {
Imgproc.convexityDefects(aproximatedContours.get(0), convexHullMatOfInt, mConvexityDefectsMatOfInt4);
if(!mConvexityDefectsMatOfInt4.empty())
{
mConvexityDefectsIntArrayList = new int[mConvexityDefectsMatOfInt4.toArray().length];
mConvexityDefectsIntArrayList = mConvexityDefectsMatOfInt4.toArray();
}
} catch (Exception e) {
Log.e("Calculate convex hulls failed.", "Details below");
e.printStackTrace();
}
}
过滤:
public void filterCalculatedPoints()
{
ArrayList<Point> tipPts = new ArrayList<Point>();
ArrayList<Point> foldPts = new ArrayList<Point>();
ArrayList<Integer> depths = new ArrayList<Integer>();
fingerTips = new ArrayList<Point>();
for (int i = 0; i < mConvexityDefectsIntArrayList.length/4; i++)
{
tipPts.add(contours.get(0).toList().get(mConvexityDefectsIntArrayList[4*i]));
tipPts.add(contours.get(0).toList().get(mConvexityDefectsIntArrayList[4*i+1]));
foldPts.add(contours.get(0).toList().get(mConvexityDefectsIntArrayList[4*i+2]));
depths.add(mConvexityDefectsIntArrayList[4*i+3]);
}
int numPoints = foldPts.size();
for (int i=0; i < numPoints; i++) {
if ((depths.get(i).intValue()) < MIN_FINGER_DEPTH)
continue;
// look at fold points on either side of a tip
int pdx = (i == 0) ? (numPoints-1) : (i - 1);
int sdx = (i == numPoints-1) ? 0 : (i + 1);
int angle = angleBetween(tipPts.get(i), foldPts.get(pdx), foldPts.get(sdx));
if (angle >= MAX_FINGER_ANGLE) // angle between finger and folds too wide
continue;
// this point is probably a fingertip, so add to list
fingerTips.add(tipPts.get(i));
}
}
结果(白点-过滤后指尖):
你能帮我编写适当的过滤函数吗?
更新 2013 年 8 月 14 日
我使用标准 openCV 函数进行轮廓逼近。我必须随着分辨率的变化和手到相机的距离来改变近似值,这是很难做到的。如果分辨率较小,则手指包含的像素较少,因此近似值应该较小。与距离相同。保持高位会导致手指完全失去。所以我认为近似并不是解决问题的好方法,但是较小的值可能有助于加快计算速度:
Imgproc.approxPolyDP(frame, frame, 2 , true);
如果我使用高值,那么结果如下图所示,只有在距离和分辨率不变的情况下才好。 另外,我很惊讶船体点和缺陷点的默认方法没有有用的参数来传递(最小角度、距离等)...
下图展示了我希望始终实现的效果,与分辨率或手到相机的距离无关。而且我不想在合上手掌时看到任何黄点...
总而言之,我想知道:
低分辨率的凸包可用于识别整个手的位置,它对手指没有用,但确实提供了感兴趣的区域和适当的比例。
然后应将更高分辨率的分析应用于您的近似轮廓,很容易跳过最后两个中未通过“长度和角度”标准的任何点,尽管您可能希望“平均”而不是“跳过”完全”。
您的代码示例是计算凸性缺陷然后将其删除的单次传递..这是一个逻辑错误..您需要边走边删除点..(a)一次性完成所有操作会更快更简单(b) 它避免了在第一次通过时删除点以及稍后必须将它们添加回来,因为任何删除都会更改之前的计算。
这个基本技术非常简单,因此适用于基本的张开手掌。但它本质上并不理解手或手势,因此调整比例、角度和长度参数只能让你“到目前为止”。
技术参考: 过滤器长度和角度“凸面缺陷” 西蒙·安德烈森博客 http://simena86.github.io/blog/2013/08/12/hand-tracking-and-recognition-with-opencv/
基于 Kinect SDK 的 C# 库,添加了手指方向检测 http://candescentnui.codeplex.com/ http://blog.candescent.ch/2011/11/improving-finger-detection.html
“自我生长和组织的神经气体”(SGONG) Nikos Papamarkos 教授http://www.papamarkos.gr/uploaded-files/Hand%20gesture%20recognition%20using%20a%20neural%20network%20shape%20fitting%20technique.pdf
商业产品 David Holz 和 Michael Buckwald “Leap Motion”创始人 http://www.engadget.com/2013/03/11/leap-motion-michael-buckwald-interview/
我认为你错过了这一点:
通过利用轮廓的低多边形近似而不是原始轮廓,可以加快船体创建和缺陷分析的速度。