Skip to content

Instantly share code, notes, and snippets.

@alexkreimer
Created May 16, 2014 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkreimer/a06f1634e7889a664d44 to your computer and use it in GitHub Desktop.
Save alexkreimer/a06f1634e7889a664d44 to your computer and use it in GitHub Desktop.
typedef std::pair<cv::Mat&,cv::Mat&> image_pair;
class image_pair_iterator
: public boost::iterator_facade<image_pair_iterator,
image_pair,
boost::forward_traversal_tag>
{
public:
image_pair_iterator() : m_index(-1) {}
explicit image_pair_iterator(const std::string& mask0,
const std::string& mask1,
int index_begin=0,
int index_end=-1)
: m_mask0(mask0),
m_mask1(mask1),
m_index(index_begin-1),
m_index_end(index_end),
m_do_alloc(true)
{
this->increment();
}
private:
friend class boost::iterator_core_access;
void increment() {
std::cout << "image_pair_iterator created" << std::endl;
m_index++;
m_do_alloc = true;
std::string file_name0 = str(boost::format(m_mask0) % m_index),
file_name1 = str(boost::format(m_mask1) % m_index);
if (!boost::filesystem::exists(file_name0) ||
!boost::filesystem::exists(file_name1)) {
m_index = -1;
} else {
m_image0 = std::shared_ptr<cv::Mat>(new cv::Mat(imread(file_name0, IMREAD_GRAYSCALE)));
m_image1 = std::shared_ptr<cv::Mat>(new cv::Mat(imread(file_name1, IMREAD_GRAYSCALE)));
m_pair = std::shared_ptr<image_pair>(new image_pair(*m_image0, *m_image1));
}
}
bool equal(image_pair_iterator const &other) const {
std::cout << "image_pair_iterator equal()" << std::endl;
return m_index == -1 || other.m_index == -1 ? m_index == other.m_index :
m_index == other.m_index &&
m_mask0 == other.m_mask0 &&
m_mask1 == other.m_mask1;
}
image_pair& dereference() const {
std::cout << "image_pair_iterator dereference()" << std::endl;
assert(m_index >= 0);
return *m_pair;
}
int m_index, m_index_end;
bool m_do_alloc;
std::string m_mask0, m_mask1;
std::shared_ptr<cv::Mat> m_image0, m_image1;
std::shared_ptr<image_pair> m_pair;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment