Vector.push_back(pair <int,int>(x1,x2));不起作用

时间:2019-06-17 08:48:31

标签: c++ dlib

我使用了DLIB parallel_for循环进行一些处理,并将坐标添加到已在循环外部声明的向量中。但是我不能从循环中使用vector.push_back()函数。

  

验证是否存在任何声明问题。   将向量指针传递给parallel_for循环lambda函数。

    //Store cordinates of respective face_image
    std::vector<pair<int,int>> xy_coords;

    //Create a dlib image window
    window.clear_overlay();
    window.set_image(dlib_frame);

    auto detections = f_detector(dlib_frame);

    dlib::parallel_for(0, detections.size(), [&,detections,xy_coords](long i)
        {
            auto det_face = detections[i];

            //Display Face data to the user
            cout << "Face Found! " << "Area: " << det_face.area() << "X: " <<det_face.left() << "Y: " << det_face.bottom() << endl;

            //Get the Shape details from the face
            auto shape = sp(dlib_frame, det_face);


            //Extract Face Image from frame
            matrix<rgb_pixel> face_img;
            extract_image_chip(dlib_frame, get_face_chip_details(shape, 150, 0.25), face_img);
            faces.push_back(face_img);
            //Add the coordinates to the coordinates vector

            xy_coords.push_back(std::pair<int,int>((int)det_face.left(),(int)det_face.bottom()));

            //Add face to dlib image window
            window.add_overlay(det_face);

        });

1 个答案:

答案 0 :(得分:1)

您的Lambda正在通过复制捕获xy_coords,而您在Lambba中推入的Lambda与外部的Lambda不同。尝试像这样[&,&xy_coords,detections]或仅[&,detections]那样通过引用捕获它。

有关更多信息,请参见此: https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture