| 1 | #ifndef LIBCVD_OPENCV_H |
| 2 | #define LIBCVD_OPENCV_H |
| 3 | |
| 4 | #include <cvd/byte.h> |
| 5 | #include <cvd/image.h> |
| 6 | #include <cvd/rgb.h> |
| 7 | #include <cvd/rgba.h> |
| 8 | #include <opencv2/imgproc/imgproc.hpp> |
| 9 | #include <opencv2/opencv.hpp> |
| 10 | |
| 11 | namespace CVD |
| 12 | { |
| 13 | namespace Internal |
| 14 | { |
| 15 | |
| 16 | template <class C> |
| 17 | struct opencv_traits |
| 18 | { |
| 19 | static const int depth = cv::DataDepth<C>::value; |
| 20 | static const int channels = 1; |
| 21 | }; |
| 22 | |
| 23 | template <class C> |
| 24 | struct opencv_traits<Rgb<C>> : public opencv_traits<C> |
| 25 | { |
| 26 | static const int channels = 3; |
| 27 | }; |
| 28 | |
| 29 | template <class C> |
| 30 | struct opencv_traits<Rgba<C>> : public opencv_traits<C> |
| 31 | { |
| 32 | static const int channels = 4; |
| 33 | }; |
| 34 | |
| 35 | template <class C> |
| 36 | struct opencv_type |
| 37 | { |
| 38 | static const int type = CV_MAKETYPE(opencv_traits<C>::depth, opencv_traits<C>::channels); |
| 39 | }; |
| 40 | }; |
| 41 | |
| 42 | template <class T> |
| 43 | cv::Mat toMat(const CVD::BasicImage<T>& img) |
| 44 | { |
| 45 | return cv::Mat(img.size().y, img.size().x, Internal::opencv_type<T>::type, (void*)img.data(), img.row_stride() * sizeof(T)); |
| 46 | } |
| 47 | |
| 48 | template <class T> |
| 49 | void equalizeHist(const CVD::BasicImage<T>& in, CVD::BasicImage<T>& out) |
| 50 | { |
| 51 | equalizeHist(toMat(in), toMat(out)); |
| 52 | } |
| 53 | |
| 54 | namespace OpenCV |
| 55 | { |
| 56 | struct ContourHierarchy |
| 57 | { |
| 58 | int next_index; |
| 59 | int previous_index; |
| 60 | int first_child_index; |
| 61 | int parent_index; |
| 62 | }; |
| 63 | |
| 64 | namespace Internal |
| 65 | { |
| 66 | inline void convert_hierarchy(const std::vector<cv::Vec4i>& cv_hierarchy, std::vector<ContourHierarchy>& hierarchy) |
| 67 | { |
| 68 | hierarchy.reserve(cv_hierarchy.size()); |
| 69 | std::transform( |
| 70 | cv_hierarchy.begin(), |
| 71 | cv_hierarchy.end(), |
| 72 | std::back_inserter(hierarchy), |
| 73 | [](const cv::Vec4i& p) { return ContourHierarchy { p[0], p[1], p[2], p[3] }; }); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | void resize(const CVD::BasicImage<T>& in, CVD::BasicImage<T>& out, int interpolation = cv::INTER_LINEAR) |
| 79 | { |
| 80 | cv::resize(toMat(in), toMat(out), cv::Size(out.size().x, out.size().y), 0, 0, interpolation); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | #endif |