尝试从深度图像中提取手部轮廓时出现无效矩阵错误

Invalid matrix error when trying to extract hand contours from a depth image

本文关键字:无效 错误 轮廓 手部 深度 深度图 图像 提取      更新时间:2023-10-16

我使用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));
}

我相信约xpolydp只需要一个国家,而你试图传递整个列表