我使用OpenCV和OpenNI一起从Xtion传感器生成的深度图像中提取手,然后是单个手指。当执行了手部生成器的焦点手势时,hasHand bool设置为true并运行下面的代码。 hand []是一个浮点数组,其中跟踪了手的x,y和z坐标。
if(hasHand)
{
unsigned char shade = 255 - (unsigned char)(hand[2] * 128.0f);
Scalar color(0, shade, 0);
vector<Point> handContour;
getHandContour(depthMat, hand, handContour);
bool grasp = convexity(handContour) > grabConvexity; //PROBLEM
int thickness = grasp ? CV_FILLED : 3;
circle(depthMatBgr, Point(hand[0], hand[1]), 10, color, thickness);
vector<Point> fingerTips;
detectFingerTips(handContour, fingerTips, &depthMatBgr);
}
在我收到评论的那一行之前,一切都运行正常,此时我收到了:
OpenCV Error: Bad argument (input array is not a valid matrix) in unknown function, ...
我已经暂时解决了这个问题,我不知道为什么我会这样做。 getHandContour的代码是:
bool getHandContour(const Mat &depthMat, const float *v, vector<Point> &handContour)
{
const int maxHandRadius = 128; // in px
const short handDepthRange = 200; // in mm
const double epsilon = 17.5; // approximation accuracy (maximum distance between the original hand contour and its approximation)
depth = v[2] * 1000.0f; // hand depth
nearClip = depth - 100; // near clipping plane
farClip = depth + 100; // far clipping plane
static Mat mask(frameSize, CV_8UC1);
mask.setTo(0);
// extract hand region
circle(mask, Point(v[0], v[1]), maxHandRadius, 255, CV_FILLED);
mask = mask & depthMat > nearClip & depthMat < farClip;
// DEBUG(show mask)
imshow("mask1", mask);
// assume largest contour in hand region to be the hand contour
vector<vector<Point> > contours;
findContours(mask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
int n = contours.size();
int maxI = -1;
int maxSize = -1;
for (int i=0; i<n; i++) {
int size = contours[i].size();
if (size > maxSize) {
maxSize = size;
maxI = i;
}
}
bool handContourFound = (maxI >= 0);
if (handContourFound) {
approxPolyDP( Mat(contours[maxI]), handContour, epsilon, true );
}
return maxI >= 0;
}
我不确定这是否足以让人们帮助我(对于很多这方面的新手),但是在正确的方向上的任何推动都将非常感激。
编辑:抱歉,我忘了在这个问题中包含convexity()代码:double convexity(const vector<Point> &contour) {
Mat contourMat(contour);
vector<int> hull;
convexHull(contourMat, hull);
int n = hull.size();
vector<Point> hullContour;
for (int i=0; i<n; i++) {
hullContour.push_back(contour[hull[i]]);
}
Mat hullContourMat(hullContour);
return (contourArea(contourMat) / contourArea(hullContourMat));
}
答案 0 :(得分:0)
我相信aboutPolyDP只需要一个countour,而你试图将整个列表传递给它