From a27ba5234d2bae8cfd1e138dde6afbe7a6556b70 Mon Sep 17 00:00:00 2001 From: thibault allenet <thibault.allenet@cea.fr> Date: Tue, 20 Feb 2024 09:22:56 +0000 Subject: [PATCH] Update tests for opencv version compatibility with function countNonZero --- unit_tests/Test_Stimulus.cpp | 30 ++++++++++++++++--- .../Test_StimulusImpl_opencv_imread.cpp | 15 ++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/unit_tests/Test_Stimulus.cpp b/unit_tests/Test_Stimulus.cpp index bdfe453..a02a711 100644 --- a/unit_tests/Test_Stimulus.cpp +++ b/unit_tests/Test_Stimulus.cpp @@ -58,10 +58,21 @@ TEST_CASE("Stimulus creation", "[Stimulus][OpenCV]") { // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get()); + auto mat_tensor = tImpl_opencv->data(); - REQUIRE((tImpl_opencv->data().total() * tImpl_opencv->data().channels()) == (true_mat.total() * true_mat.channels())); - REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0); + // Check the dimensions + REQUIRE((mat_tensor.total() * mat_tensor.channels()) == (true_mat.total() * true_mat.channels())); + // Split it in channels + std::vector<cv::Mat> channels_tensor; + cv::split(mat_tensor, channels_tensor); + + REQUIRE(channels_tensor.size() == channels.size()); + + // Check the elements + for (size_t i = 0; i < channels_tensor.size(); ++i) { + REQUIRE(cv::countNonZero(channels_tensor[i] != channels[i]) == 0); + } // This time the tensor is already loaded in memory std::shared_ptr<Tensor> tensor_load_2; @@ -69,9 +80,20 @@ TEST_CASE("Stimulus creation", "[Stimulus][OpenCV]") { // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv_2 = dynamic_cast<TensorImpl_opencv_*>(tensor_load_2->getImpl().get()); + auto mat_tensor_2 = tImpl_opencv_2->data(); - REQUIRE((tImpl_opencv_2->data().total() * tImpl_opencv_2->data().channels()) == (true_mat.total() * true_mat.channels())); - REQUIRE(cv::countNonZero(tImpl_opencv_2->data() != true_mat) == 0); + // Check the dimensions + REQUIRE((mat_tensor_2.total() * mat_tensor_2.channels()) == (true_mat.total() * true_mat.channels())); + // Split it in channels + std::vector<cv::Mat> channels_tensor_2; + cv::split(mat_tensor_2, channels_tensor_2); + + REQUIRE(channels_tensor_2.size() == channels.size()); + + // Check the elements + for (size_t i = 0; i < channels_tensor_2.size(); ++i) { + REQUIRE(cv::countNonZero(channels_tensor_2[i] != channels[i]) == 0); + } } } diff --git a/unit_tests/Test_StimulusImpl_opencv_imread.cpp b/unit_tests/Test_StimulusImpl_opencv_imread.cpp index b3b2b19..4fd5a99 100644 --- a/unit_tests/Test_StimulusImpl_opencv_imread.cpp +++ b/unit_tests/Test_StimulusImpl_opencv_imread.cpp @@ -59,9 +59,20 @@ TEST_CASE("StimulusImpl_opencv_imread creation", "[StimulusImpl_opencv_imread][O // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get()); + auto mat_tensor = tImpl_opencv->data(); - REQUIRE((tImpl_opencv->data().total() * tImpl_opencv->data().channels()) == (true_mat.total() * true_mat.channels())); - REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0); + // Check the dimensions + REQUIRE((mat_tensor.total() * mat_tensor.channels()) == (true_mat.total() * true_mat.channels())); + // Split it in channels + std::vector<cv::Mat> channels_tensor; + cv::split(mat_tensor, channels_tensor); + + REQUIRE(channels_tensor.size() == channels.size()); + + // Check the elements + for (size_t i = 0; i < channels_tensor.size(); ++i) { + REQUIRE(cv::countNonZero(channels_tensor[i] != channels[i]) == 0); + } } } -- GitLab