我想从大图片中搜索小图片,我的算法是:
- 搜索第一行
- 如果第一行匹配,则比较其余的
我想使用 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/