我使用 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;
}
我不确定这是否足以让人们帮助我(这对很多人来说都是新的),但任何朝着正确方向的轻推将不胜感激。
编辑:对不起,我忘了在这个问题中包含凸性()代码:
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));
}