【问题标题】:use boost::boyer_moore with boost::gil使用 boost::boyer_moore 和 boost::gil
【发布时间】:2014-08-13 07:02:06
【问题描述】:

我想从大图中搜索小图,我的算法是:

  1. 搜索第一行
  2. 如果第一行匹配,则比较其余行

我想使用 boost::algorithm::boyer_moore 来进行行搜索,它与 std::string: 一起工作正常:

#include <string>
using namespace std;
#include "boost/algorithm/searching/boyer_moore.hpp"
using namespace boost::algorithm;

int main() {
    string s;

    boyer_moore<string::iterator> bm(s.begin(), s.end()); // it compiles
}

代码可以编译,但这个没有:

#include "boost/mpl/vector.hpp"
using namespace boost;
#include "boost/gil/gil_all.hpp"
using namespace boost::gil;

#include "boost/algorithm/searching/boyer_moore.hpp"
using namespace boost::algorithm;

int main() {
    typedef rgba8_image_t image_t;
    typedef image_t::view_t view_t;

    view_t vw;

    boyer_moore<view_t::x_iterator> bm(vw.row_begin(0), vw.row_end(0)); // compile error
}

两个都是迭代器,第二个有什么问题?

谢谢。

【问题讨论】:

  • 在包含它们之前通过 using namespace 扭曲包含是非常不寻常的,尤其是对于 TMP-heavy 库(因为它们通常依赖 ADL 进行名称查找)
  • 顺便提一下,标签gil与Boost.GIL无关。你想要的标签是boost-gil。您应该更改它,以最大限度地提高您的问题到达图书馆专家的概率。

标签: c++ boost boost-gil boyer-moore


【解决方案1】:

根据docs,该算法使用称为skip_table 的辅助数据结构。默认情况下(当迭代器的value_type 不是字符或无符号字符时)此表使用tr1::unordered_map,这要求gil::pixel 是可散列的。所以你有两个选择:你要么通过为你的迭代器专门化BM_traits来更改默认的skip_table(这很遗憾没有记录),或者你让gil::pixel可以散列。对于后者,您可以在namespace boost::gil 内创建一个std::size_t hash_value(pixel&lt;ChannelValue,Layout&gt; const&amp; val)。以下compiles 带有 g++ 4.9.0 和 Visual Studio 2013(什么都不做):

#include <boost/functional/hash.hpp> //ADDED
#include <boost/mpl/vector.hpp>
#include <boost/gil/gil_all.hpp>
#include <boost/algorithm/searching/boyer_moore.hpp>

using namespace boost;
using namespace boost::gil;
using namespace boost::algorithm;

namespace boost {
    namespace gil
    {
        template <typename ChannelValue, typename Layout>
        std::size_t hash_value(pixel<ChannelValue, Layout> const& b)
        {
            std::size_t seed = 0;
            for (int c = 0; c<num_channels<pixel<ChannelValue, Layout> >::value; ++c)
                hash_combine(seed, b[c]);
            return seed;
        }
    }
}

namespace std { //ADDED
    template <typename ChannelValue, typename Layout>
    struct hash<boost::gil::pixel<ChannelValue,Layout> > {
        size_t operator ()(boost::gil::pixel<ChannelValue, Layout> const& value) const {
            return hash_value(value);
        }
    };
}

int main() {
    typedef rgba8_image_t image_t;
    typedef image_t::view_t view_t;

    view_t vw;

    boyer_moore<view_t::x_iterator> bm(vw.row_begin(0), vw.row_end(0)); // compile error
}

【讨论】:

  • 谢谢,但是代码还是不能用 vs2010 编译,输出如下:link
  • thisthis 之后,我已经编辑了代码。它现在也可以用 vs2013 编译,希望它也适用于你。
  • 适用于 vs2013。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2012-10-29
  • 1970-01-01
相关资源
最近更新 更多