| 1 | #ifndef LIBCVD_OPENCV_EIGEN_H |
| 2 | #define LIBCVD_OPENCV_EIGEN_H |
| 3 | |
| 4 | #include "opencv.h" |
| 5 | |
| 6 | #include <Eigen/Core> |
| 7 | #include <opencv2/core/eigen.hpp> |
| 8 | #include <opencv2/imgproc.hpp> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <cstdint> |
| 12 | #include <iterator> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace CVD |
| 16 | { |
| 17 | namespace OpenCV |
| 18 | { |
| 19 | namespace Internal |
| 20 | { |
| 21 | void convert_contours(const std::vector<std::vector<cv::Point2i>>& cv_contours, std::vector<std::vector<Eigen::Vector2i>>& contours) |
| 22 | { |
| 23 | for(const auto& cv_contour : cv_contours) |
| 24 | { |
| 25 | auto& contour = contours.emplace_back(); |
| 26 | contour.reserve(cv_contour.size()); |
| 27 | std::transform( |
| 28 | cv_contour.begin(), |
| 29 | cv_contour.end(), |
| 30 | std::back_inserter(contour), |
| 31 | [](const cv::Point2i& p) { return Eigen::Vector2i(p.x, p.y); }); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | template <typename T, typename U, int Options, int MaxRows, int MaxCols> |
| 37 | void warpAffine(const CVD::BasicImage<T>& in, CVD::BasicImage<T>& out, const Eigen::Matrix<U, 2, 3, Options, MaxRows, MaxCols>& M, int flags = cv::INTER_LINEAR, int borderMode = cv::BORDER_CONSTANT, const T& borderValue = T()) |
| 38 | { |
| 39 | cv::Mat m_mat; |
| 40 | cv::eigen2cv(M, m_mat); |
| 41 | cv::warpAffine(toMat(in), toMat(out), m_mat, cv::Size(out.size().x, out.size().y), flags, borderMode, borderValue); |
| 42 | } |
| 43 | |
| 44 | template <typename T, typename U, int Options, int MaxRows, int MaxCols> |
| 45 | void warpPerspective(const CVD::BasicImage<T>& in, CVD::BasicImage<T>& out, const Eigen::Matrix<U, 3, 3, Options, MaxRows, MaxCols>& M, int flags = cv::INTER_LINEAR, int borderMode = cv::BORDER_CONSTANT, const T& borderValue = T()) |
| 46 | { |
| 47 | cv::Mat m_mat; |
| 48 | cv::eigen2cv(M, m_mat); |
| 49 | cv::warpPerspective(toMat(in), toMat(out), m_mat, cv::Size(out.size().x, out.size().y), flags, borderMode, borderValue); |
| 50 | } |
| 51 | |
| 52 | inline void findContours(const CVD::BasicImage<std::uint8_t>& in, std::vector<std::vector<Eigen::Vector2i>>& contours, cv::RetrievalModes mode, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) |
| 53 | { |
| 54 | std::vector<std::vector<cv::Point2i>> cv_contours; |
| 55 | cv::findContours(toMat(in), cv_contours, mode, method, { offset.x(), offset.y() }); |
| 56 | CVD::OpenCV::Internal::convert_contours(cv_contours, contours); |
| 57 | } |
| 58 | |
| 59 | inline void findContours(const CVD::BasicImage<std::int32_t>& in, std::vector<std::vector<Eigen::Vector2i>>& contours, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) |
| 60 | { |
| 61 | std::vector<std::vector<cv::Point2i>> cv_contours; |
| 62 | cv::findContours(toMat(in), cv_contours, cv::RETR_FLOODFILL, method, { offset.x(), offset.y() }); |
| 63 | CVD::OpenCV::Internal::convert_contours(cv_contours, contours); |
| 64 | } |
| 65 | |
| 66 | inline void findContours(const CVD::BasicImage<std::uint8_t>& in, std::vector<std::vector<Eigen::Vector2i>>& contours, std::vector<ContourHierarchy>& hierarchy, cv::RetrievalModes mode, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) |
| 67 | { |
| 68 | std::vector<std::vector<cv::Point2i>> cv_contours; |
| 69 | std::vector<cv::Vec4i> cv_hierarchy; |
| 70 | cv::findContours(toMat(in), cv_contours, cv_hierarchy, mode, method, { offset.x(), offset.y() }); |
| 71 | CVD::OpenCV::Internal::convert_contours(cv_contours, contours); |
| 72 | CVD::OpenCV::Internal::convert_hierarchy(cv_hierarchy, hierarchy); |
| 73 | } |
| 74 | |
| 75 | inline void findContours(const CVD::BasicImage<std::int32_t>& in, std::vector<std::vector<Eigen::Vector2i>>& contours, std::vector<ContourHierarchy>& hierarchy, cv::ContourApproximationModes method, const Eigen::Vector2i& offset = { 0, 0 }) |
| 76 | { |
| 77 | std::vector<std::vector<cv::Point2i>> cv_contours; |
| 78 | std::vector<cv::Vec4i> cv_hierarchy; |
| 79 | cv::findContours(toMat(in), cv_contours, cv_hierarchy, cv::RETR_FLOODFILL, method, { offset.x(), offset.y() }); |
| 80 | CVD::OpenCV::Internal::convert_contours(cv_contours, contours); |
| 81 | CVD::OpenCV::Internal::convert_hierarchy(cv_hierarchy, hierarchy); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #endif |