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