将一个OpenCV OutputArrayOfArrays对象复制到另一个对象

时间:2019-04-04 06:39:48

标签: c++ opencv

我想将一个OutputArrayOfArrays对象复制到另一个。像这样:

void function(OutputArrayOfArrays contour) {
    std::vector<std::vector<cv::Point>> contours;
    OutputArrayOfArrays _contour(contours);

    ....Doing something....

    contour = _contour;
}

但是我遇到以下错误:

no viable overloaded '='
    contour = _contour;

1 个答案:

答案 0 :(得分:0)

我认为您无法针对各种类型执行此操作。您将必须专门处理类型。查看public void newspaper() { Scanner in = new Scanner(System.in); System.out.println("Question 4 \n"); double avg = 0; int sum = 0; int[] youths = new int[5]; // The loop for calculating the average for (int i = 0; i < youths.length; i++) { System.out.println("Youth " + (i + 1) + " How many was delivered?"); youths[i] = in.nextInt(); sum = sum + youths[i]; } // Note that the average can be calculated once, not every iteration avg = sum / youths.length; System.out.println("Average is: " + avg + "\n"); // The loop for checking below of above average for (int i = 0; i < youths.length; i++) { if (youths[i] > avg) { System.out.println("Youth " + (i + 1) + " is above average"); } else { System.out.println("Youth " + (i + 1) + " below average"); } } } 源中使用此类型的函数,您会发现它们是以特定方式处理的:例如opencvfindContours函数。

对于split的特定情况,您可以按照std::vector<std::vector<cv::Point>>的方式进行操作。下面我写了一个简单的函数来演示这一点。

findContours

输出看起来像

void testfunction(OutputArrayOfArrays contour) {
    std::vector<std::vector<cv::Point>> contours;

    // fill some data
    std::vector<cv::Point> v1;
    v1.push_back(cv::Point(10, 1));
    v1.push_back(cv::Point(11, 2));
    v1.push_back(cv::Point(12, 3));

    std::vector<cv::Point> v2;
    v2.push_back(cv::Point(20, 10));
    v2.push_back(cv::Point(21, 20));

    contours.push_back(v1);
    contours.push_back(v2);

    // output
    contour.create(contours.size(), 1, 0, -1, true);
    for (size_t i = 0; i < contours.size(); i++) {
        std::vector<cv::Point>& v = contours[i];
        contour.create(v.size(), 1, CV_32SC2, i, true);
        Mat m = contour.getMat(i);
        for (size_t j = 0; j < v.size(); j++) {
            m.at<int>(2*j) = v[j].x;
            m.at<int>(2*j+1) = v[j].y;
        }
        std::cout << m << std::endl;
    }
}

使用代码

[10, 1, 11, 2, 12, 3]
[20, 10, 21, 20]
2
3
[10, 1]
[11, 2]
[12, 3]
2
[20, 10]
[21, 20]