无法通过线程并行化加快特征检测器的速度

时间:2019-03-23 02:46:17

标签: c++ multithreading c++11 concurrency locks

我想同时在多个图像上获得冲浪功能。实施后,我看不到任何加速。

这是该过程的普通版本。

prev = high_resolution_clock::now();
    cout<<"Number of Images : "<<images.size()<<endl;

    for (int i = 0 ; i < images.size(); i++)
    {
        surf->detectAndCompute(bwImages[i], Mat(), keypoints, descriptors); 
    }

    current = high_resolution_clock::now(); 
    duration = duration_cast<microseconds>( current - prev ).count();
    cout<<"Time taken for SURF detector "<<duration<<" microseconds"<<endl;

输出为

  

SURF检测器花费的时间3058657微秒

这是我的并行版本

void getDescriptor(Mat bwImage,int i, map<int,vector<KeyPoint>> &keyPointMap,map<int,Mat> &descriptorMap)
{
    Mat descriptors;
    std::vector<KeyPoint> keypoints;
    Ptr<Feature2D> surf = SURF::create(SURF_HESSIAN_THRESHOLD);

    surf->detectAndCompute(bwImage, Mat(), keypoints, descriptors);

    std::lock_guard<std::mutex> guard(mut);
    keyPointMap[i] = keypoints;
    descriptorMap[i] = descriptors;
    return;
}

void getDescriptorsParallel(vector < Mat > &bwImages,map<int,vector<KeyPoint>> &keyPointMap,map<int,Mat> &descriptorMap)
{
    std::thread t[25];

    for (int i = 0 ; i < bwImages.size(); i++)
        t[i] = std::thread(getDescriptor,bwImages[i],i,
                std::ref(keyPointMap),std::ref(descriptorMap));

    for (int i = 0 ; i < bwImages.size(); i++)
        t[i].join();

}

它比以前的版本花费更多的时间。 getDescriptorsParallel(bwImages,keyPointMap,descriptorMap);花费的时间是

  

3435328微秒

我在这里想念什么?

0 个答案:

没有答案