繁体   English   中英

C ++查找两个std :: map之间匹配项的有效方法

[英]C++ efficient way to find matches between two std::map

我有一个使用RGB-D相机获取的数据集和一个文本文件,其中为该数据集的每个图像存储了时间戳和文件名。 我要做的是解析此文件并填充两个std :: map,一个用于rgb图像,另一个用于深度图像。 现在,由于时间戳不相交,因此我必须编写一个例程来根据时间戳查找匹配的图像。 这是我到目前为止写的:

typedef map<double,string> StampImageMap;

...

vector<string> &vstrImageFilenamesRGB;
vector<string> &vstrImageFilenamesD;
vector<double> &vTimestampsRGB;
vector<double> &vTimestampsDPT;

double tolerance = 0.02;

for(StampImageMap::iterator it=rgb_images.begin(); it != rgb_images.end(); it++) {
        bool found = false;
        StampImageMap::iterator jt=depth_images.begin();
        while(found == false && jt!=depth_images.end()) {
            if(fabs(it->first - jt->first) < tolerance) {
                found = true;
                vstrImageFilenamesRGB.push_back(it->second);
                vstrImageFilenamesD.push_back(jt->second);
                vTimestampsRGB.push_back(it->first);
                vTimestampsDPT.push_back(jt->first);
            }
            jt++;
        }
    }

我想知道是否有更有效的方法来执行此任务!

现在编写代码时,复杂度为Θ(nm) ,其中nm是序列的大小。 至少有两种方法可以改善此问题(第二种方法效率更高,但更难编写代码)。

  • 在外部循环的主体中,不要通过while(found == false && jt!=depth_images.end())第二张地图中的所有元素。 而是使用std::map::lower_boundstd::map::upper_bound分别搜索it->first - toleranceit->first + tolerance 仅在这两个调用的结果之间循环。

    因此,代码变成了这样的东西:

     for(StampImageMap::iterator it=rgb_images.begin(); it != rgb_images.end(); it++) { StampImageMap::const_iterator lower = depth_images.lower_bound(it->first - tolerance); StampImageMap::const_iterator upper = depth_images.lower_bound(it->first + tolerance); // Now just search between lower and upper. } 

    这会将每次迭代减少到Θ(log(m))+ p ,其中p是此范围的大小。

  • 由于地图的键是已排序的,因此您可以修改一种标准方法,以查找这种情况下两个已排序数组的交集 这会将运行时间缩短至Θ(m + n) 请注意,该修改有些棘手,因为您不是要查找精确元素的交集,而是要找到“足够接近”的元素的交集。

    这是这种情况的伪代码

      it = rgb_image.begin(); jt = depth_image.begin(); while(it != rgb_image.end() && jt != depth_image.end()) { if(fabs(it->first - jt->first) < tolerance) { // Match found! ++it; ++jt; continue; } if(it.first > jt.first + tolerance) { ++jt; continue; } ++it; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM