#ifndef LIBCVD_OPENCV_EIGEN_H #define LIBCVD_OPENCV_EIGEN_H #include "opencv.h" #include #include #include #include #include #include #include namespace CVD { namespace OpenCV { namespace Internal { void convert_contours(const std::vector>& cv_contours, std::vector>& contours) { for(const auto& cv_contour : cv_contours) { auto& contour = contours.emplace_back(); contour.reserve(cv_contour.size()); std::transform( cv_contour.begin(), cv_contour.end(), std::back_inserter(contour), [](const cv::Point2i& p) { return Eigen::Vector2i(p.x, p.y); }); } } } template void warpAffine(const CVD::BasicImage& in, CVD::BasicImage& out, const Eigen::Matrix& M, int flags = cv::INTER_LINEAR, int borderMode = cv::BORDER_CONSTANT, const T& borderValue = T()) { cv::Mat m_mat; cv::eigen2cv(M, m_mat); cv::warpAffine(toMat(in), toMat(out), m_mat, cv::Size(out.size().x, out.size().y), flags, borderMode, borderValue); } template void warpPerspective(const CVD::BasicImage& in, CVD::BasicImage& out, const Eigen::Matrix& M, int flags = cv::INTER_LINEAR, int borderMode = cv::BORDER_CONSTANT, const T& borderValue = T()) { cv::Mat m_mat; cv::eigen2cv(M, m_mat); cv::warpPerspective(toMat(in), toMat(out), m_mat, cv::Size(out.size().x, out.size().y), flags, borderMode, borderValue); } inline void findContours(const CVD::BasicImage& in, std::vector>& contours, cv::RetrievalModes mode, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) { std::vector> cv_contours; cv::findContours(toMat(in), cv_contours, mode, method, { offset.x(), offset.y() }); CVD::OpenCV::Internal::convert_contours(cv_contours, contours); } inline void findContours(const CVD::BasicImage& in, std::vector>& contours, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) { std::vector> cv_contours; cv::findContours(toMat(in), cv_contours, cv::RETR_FLOODFILL, method, { offset.x(), offset.y() }); CVD::OpenCV::Internal::convert_contours(cv_contours, contours); } inline void findContours(const CVD::BasicImage& in, std::vector>& contours, std::vector& hierarchy, cv::RetrievalModes mode, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) { std::vector> cv_contours; std::vector cv_hierarchy; cv::findContours(toMat(in), cv_contours, cv_hierarchy, mode, method, { offset.x(), offset.y() }); CVD::OpenCV::Internal::convert_contours(cv_contours, contours); CVD::OpenCV::Internal::convert_hierarchy(cv_hierarchy, hierarchy); } inline void findContours(const CVD::BasicImage& in, std::vector>& contours, std::vector& hierarchy, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) { std::vector> cv_contours; std::vector cv_hierarchy; cv::findContours(toMat(in), cv_contours, cv_hierarchy, cv::RETR_FLOODFILL, method, { offset.x(), offset.y() }); CVD::OpenCV::Internal::convert_contours(cv_contours, contours); CVD::OpenCV::Internal::convert_hierarchy(cv_hierarchy, hierarchy); } } } #endif