3

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

  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
}

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

谢谢。

4

1 回答 1

4

根据文档,该算法使用一种名为skip_table. 默认情况下(当value_type迭代器的 不是 char 或 unsigned char 时)该表使用 a tr1::unordered_map,这要求它gil::pixel是可散列的。因此,您有两个选择:您可以通过专门BM_traits针对您的迭代器来更改默认的 skip_table(遗憾的是,这是未记录的),或者您可以进行gil::pixel哈希处理。对于后者,您可以std::size_t hash_value(pixel<ChannelValue,Layout> const& val)namespace boost::gil. 以下使用 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
}
于 2014-06-23T10:32:58.657 回答