OpenCV矢量访问错误

时间:2011-01-19 20:34:41

标签: c++ opencv

我正在使用项目FingertipTuio3D

中的代码
std::vector<cv::Point2i> detectFingertips(cv::Mat1f z, float zMin = 0.0f, float zMax = 0.75f, cv::Mat1f& debugFrame = cv::Mat1f()) {
    using namespace cv;
    using namespace std;    
    bool debug = !debugFrame.empty();

    vector<Point2i> fingerTips;

    Mat handMask = z < zMax & z > zMin;

    std::vector<std::vector<cv::Point>> contours;
    findContours(handMask.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); // we are cloning here since method will destruct the image

    if (contours.size()) {
        for (int i=0; i<contours.size(); i++) {
            vector<Point> contour = contours[i];
            Mat contourMat = Mat(contour);
            double area = cv::contourArea(contourMat);

            if (area > 3000)  { // possible hand
                Scalar center = mean(contourMat);
                Point centerPoint = Point(center.val[0], center.val[1]);

                vector<Point> approxCurve;
                cv::approxPolyDP(contourMat, approxCurve, 20, true);

                vector<int> hull;
                cv::convexHull(Mat(approxCurve), hull);

                // find upper and lower bounds of the hand and define cutoff threshold (don't consider lower vertices as fingers)
                int upper = 640, lower = 0;
                for (int j=0; j<hull.size(); j++) {
                    int idx = hull[j]; // corner index
                    if (approxCurve[idx].y < upper) upper = approxCurve[idx].y;
                    if (approxCurve[idx].y > lower) lower = approxCurve[idx].y;
                }
                float cutoff = lower - (lower - upper) * 0.1f;

                // find interior angles of hull corners
                for (int j=0; j<hull.size(); j++) {
                    int idx = hull[j]; // corner index
                    int pdx = idx == 0 ? approxCurve.size() - 1 : idx - 1; //  predecessor of idx
                    int sdx = idx == approxCurve.size() - 1 ? 0 : idx + 1; // successor of idx

                    Point v1 = approxCurve[sdx] - approxCurve[idx];
                    Point v2 = approxCurve[pdx] - approxCurve[idx];

                    float angle = acos( (v1.x*v2.x + v1.y*v2.y) / (norm(v1) * norm(v2)) );

                    // low interior angle + within upper 90% of region -> we got a finger
                    if (angle < 1 && approxCurve[idx].y < cutoff) {
                        int u = approxCurve[idx].x;
                        int v = approxCurve[idx].y;

                        fingerTips.push_back(Point2i(u,v));

                        if (debug) {
                            cv::circle(debugFrame, approxCurve[idx], 10, Scalar(1), -1);
                        }
                    }
                }

                if (debug) {
                    // draw cutoff threshold
                    cv::line(debugFrame, Point(center.val[0]-100, cutoff), Point(center.val[0]+100, cutoff), Scalar(1.0f));

                    // draw approxCurve
                    for (int j=0; j<approxCurve.size(); j++) {
                        cv::circle(debugFrame, approxCurve[j], 10, Scalar(1.0f));
                        if (j != 0) {
                            cv::line(debugFrame, approxCurve[j], approxCurve[j-1], Scalar(1.0f));
                        } else {
                            cv::line(debugFrame, approxCurve[0], approxCurve[approxCurve.size()-1], Scalar(1.0f));
                        }
                    }

                    // draw approxCurve hull
                    for (int j=0; j<hull.size(); j++) {
                        cv::circle(debugFrame, approxCurve[hull[j]], 10, Scalar(1.0f), 3);
                        if(j == 0) {
                            cv::line(debugFrame, approxCurve[hull[j]], approxCurve[hull[hull.size()-1]], Scalar(1.0f));
                        } else {
                            cv::line(debugFrame, approxCurve[hull[j]], approxCurve[hull[j-1]], Scalar(1.0f));
                        }
                    }
                }
            }
        }
    }

    return fingerTips;
}

当代码到达此点vector<Point> contour = contours[i];时,它失败并带有AccessViolation:

  

0x00b85039处的未处理异常   FingertipTuio3d.exe:0xC0000005:   访问违规阅读位置   0x00000008。

该位置位于std :: vector的size_type size() const函数中。

知道导致问题的原因,以及如何修复问题?

4 个答案:

答案 0 :(得分:0)

你的findContours实际上是否在每个轮廓中放置了任何东西?

您正在检查数组是否存在,但不是单个元素有效

答案 1 :(得分:0)

您可以通过迭代器访问元素吗? IOW,如果您在调用loadContours后立即添加以下代码,您会得到什么输出?

int i = 0;
for (std::vector<std::vector<cv::Point> >::iterator it = contours.begin();
     it != contours.end();
     ++it)
{
  std::cout << "contours[" << i << "].size() == " << it->size() << std::endl;
}

答案 2 :(得分:0)

因为轮廓是矢量的矢量(std :: vector&gt; contours;)

和轮廓是点矢量(矢量轮廓)

所以它不能做矢量轮廓=轮廓[i];

如果你想复制轮廓[i],你需要采用另一个矢量矢量。

答案 3 :(得分:-2)

这是构建链接错误。看看这个:

--#ifdef _DEBUG 
--#pragma comment(lib,"opencv_core249d.lib")
--#else
--#pragma comment(lib,"opencv_core249.lib")    <<---- your checked!!
--#endif  

: - )