c++ - 使用 boost::boyer_moore 和 boost::gil

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

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

  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
}

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

谢谢。

最佳答案

根据docs该算法使用称为 skip_table 的辅助数据结构.默认情况下(当迭代器的 value_type 不是 char 或 unsigned char 时)此表使用 tr1::unordered_map ,这需要 gil::pixel可以散列。所以你有两个选择:你要么通过专门化 BM_traits 来更改默认的 skip_table对于你的迭代器(遗憾的是没有记录),或者你做 gil::pixel哈希能力。对于后者,您可以创建一个 std::size_t hash_value(pixel<ChannelValue,Layout> const& val)namespace boost::gil里面.以下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
}

关于c++ - 使用 boost::boyer_moore 和 boost::gil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362533/

相关文章:

c++ - 函数参数的 Visual Studio 缩进

c++ - 将 size_t 转换为 vector<unsigned char>

c++ - 在 C++ 程序中使用 Cheat Engine 基地址

c++ - 无法使用 Boost GIL 检测图像文件类型(未捕获异常)

c++ - 是否可以在 C++ 中不使用堆来创建类 String?

c++ - 抽象树与解析器树

c++ - 使用 boost::optional boost program_options

c++ - 无法在 Boost 线程中使用 message_queue 接收消息

c++ - 在 boost::gil 中读取图像和访问字节