| 1 | /* |
| 2 | This file is part of the CVD Library. |
| 3 | |
| 4 | Copyright (C) 2022 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | #include "test_utility.h" |
| 9 | |
| 10 | #include <cvd/opencv.h> |
| 11 | #include <cvd/opencv_eigen.h> |
| 12 | #include <cvd/opencv_toon.h> |
| 13 | |
| 14 | #include <cstdlib> |
| 15 | #include <iostream> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | using CVD::Testing::assert_equal; |
| 20 | using CVD::Testing::assert_image_equal; |
| 21 | using CVD::Testing::assert_vector_equal; |
| 22 | using CVD::Testing::init; |
| 23 | |
| 24 | template <typename T, int Rows, int Cols, int Options> |
| 25 | TooN::Matrix<Rows, Cols, T> ToTooN(const Eigen::Matrix<T, Rows, Cols, Options>& m) |
| 26 | { |
| 27 | if constexpr(Options & Eigen::RowMajor) |
| 28 | { |
| 29 | return TooN::Matrix<Rows, Cols, T, TooN::Reference::RowMajor>(const_cast<T*>(m.data())); |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | return TooN::Matrix<Rows, Cols, T, TooN::Reference::ColMajor>(const_cast<T*>(m.data())); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | int main() |
| 39 | { |
| 40 | CVD::Image<float> in = init<float>({ { 1, 2 }, { 3, 4 } }); |
| 41 | CVD::Image<float> out({ 4, 4 }); |
| 42 | Eigen::Matrix<double, 2, 3> transformation; |
| 43 | transformation << 0, -1, 2, |
| 44 | 1, 0, 1; |
| 45 | CVD::OpenCV::warpAffine(in, out, transformation, cv::INTER_NEAREST); |
| 46 | assert_image_equal(init<float>({ |
| 47 | { 0, 0, 0, 0 }, |
| 48 | { 0, 3, 1, 0 }, |
| 49 | { 0, 4, 2, 0 }, |
| 50 | { 0, 0, 0, 0 }, |
| 51 | }), |
| 52 | out); |
| 53 | |
| 54 | CVD::Image<float> toon_out({ 4, 4 }); |
| 55 | double values[6] = { 0, -1, 2, 1, 0, 1 }; |
| 56 | TooN::Matrix<2, 3> toon_transform = ToTooN(transformation); |
| 57 | CVD::OpenCV::warpAffine(in, toon_out, toon_transform, cv::INTER_NEAREST); |
| 58 | assert_image_equal(init<float>({ |
| 59 | { 0, 0, 0, 0 }, |
| 60 | { 0, 3, 1, 0 }, |
| 61 | { 0, 4, 2, 0 }, |
| 62 | { 0, 0, 0, 0 }, |
| 63 | }), |
| 64 | toon_out); |
| 65 | |
| 66 | CVD::Image<std::uint8_t> mask = init<std::uint8_t>({ |
| 67 | { 0, 0, 0, 0, 0, 0 }, |
| 68 | { 0, 0, 1, 1, 0, 0 }, |
| 69 | { 0, 1, 1, 1, 1, 0 }, |
| 70 | { 0, 1, 1, 1, 1, 0 }, |
| 71 | { 0, 0, 1, 1, 0, 0 }, |
| 72 | { 0, 0, 0, 0, 0, 0 }, |
| 73 | }); |
| 74 | std::vector<std::vector<Eigen::Vector2i>> contours; |
| 75 | CVD::OpenCV::findContours(mask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); |
| 76 | assert_equal(static_cast<size_t>(1), contours.size()); |
| 77 | assert_vector_equal(std::vector<Eigen::Vector2i> { { 2, 1 }, { 1, 2 }, { 1, 3 }, { 2, 4 }, { 3, 4 }, { 4, 3 }, { 4, 2 }, { 3, 1 } }, contours[0]); |
| 78 | |
| 79 | std::vector<std::vector<TooN::Vector<2, int>>> toon_contours; |
| 80 | CVD::OpenCV::findContours(mask, toon_contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); |
| 81 | assert_equal(static_cast<size_t>(1), toon_contours.size()); |
| 82 | assert_vector_equal(std::vector<TooN::Vector<2, int>> { |
| 83 | TooN::makeVector(2, 1), |
| 84 | TooN::makeVector(1, 2), |
| 85 | TooN::makeVector(1, 3), |
| 86 | TooN::makeVector(2, 4), |
| 87 | TooN::makeVector(3, 4), |
| 88 | TooN::makeVector(4, 3), |
| 89 | TooN::makeVector(4, 2), |
| 90 | TooN::makeVector(3, 1) }, |
| 91 | toon_contours[0]); |
| 92 | } |