From 616f187a4d39a1a665b510e72723f65649b43c87 Mon Sep 17 00:00:00 2001 From: efahnestock <13023468+efahnestock@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:55:02 -0500 Subject: [PATCH] adding video common (correct this time) (#351) --- common/BUILD | 2 +- common/video/BUILD | 21 +++++++++++++++++++ common/video/image_compare.cc | 15 ++++++++++++++ common/video/image_compare.hh | 9 ++++++++ common/video/image_compare_test.cc | 33 ++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 common/video/BUILD create mode 100644 common/video/image_compare.cc create mode 100644 common/video/image_compare.hh create mode 100644 common/video/image_compare_test.cc diff --git a/common/BUILD b/common/BUILD index ed0bddf6..096ee465 100644 --- a/common/BUILD +++ b/common/BUILD @@ -105,4 +105,4 @@ cc_test( "@bs_thread_pool", "@com_google_googletest//:gtest_main", ], -) +) \ No newline at end of file diff --git a/common/video/BUILD b/common/video/BUILD new file mode 100644 index 00000000..8696c6ba --- /dev/null +++ b/common/video/BUILD @@ -0,0 +1,21 @@ +package(features=["warning_compile_flags"]) + +cc_library( + name = "image_compare", + hdrs = ["image_compare.hh"], + srcs = ["image_compare.cc"], + deps = [ + "@eigen//:eigen", + "@opencv", + ], + visibility = ["//visibility:public"], +) + +cc_test( + name = "image_compare_test", + srcs = ["image_compare_test.cc"], + deps = [ + ":image_compare", + "@com_google_googletest//:gtest_main", + ] +) diff --git a/common/video/image_compare.cc b/common/video/image_compare.cc new file mode 100644 index 00000000..145befb6 --- /dev/null +++ b/common/video/image_compare.cc @@ -0,0 +1,15 @@ +#include "opencv2/opencv.hpp" + +namespace robot::common::video { + +bool images_equal(const cv::Mat& img1, const cv::Mat& img2) { + if (img1.size() != img2.size() || img1.type() != img2.type()) { + return false; + } + cv::Mat diff; + cv::absdiff(img1, img2, diff); + diff = diff.reshape(1); + return cv::countNonZero(diff) == 0; +} + +} // namespace robot::common::video diff --git a/common/video/image_compare.hh b/common/video/image_compare.hh new file mode 100644 index 00000000..c9d56195 --- /dev/null +++ b/common/video/image_compare.hh @@ -0,0 +1,9 @@ +#pragma once + +#include "opencv2/opencv.hpp" + +namespace robot::common::video { + +bool images_equal(const cv::Mat& img1, const cv::Mat& img2); + +} \ No newline at end of file diff --git a/common/video/image_compare_test.cc b/common/video/image_compare_test.cc new file mode 100644 index 00000000..9d3da9e5 --- /dev/null +++ b/common/video/image_compare_test.cc @@ -0,0 +1,33 @@ +#include "common/video/image_compare.hh" + +#include + +#include "opencv2/opencv.hpp" + +namespace robot::common::video { + +TEST(ImageCompareTest, ImagesEqual_SameImages) { + cv::Mat img1 = cv::Mat::zeros(100, 100, CV_8UC3); + cv::Mat img2 = img1.clone(); + EXPECT_TRUE(images_equal(img1, img2)); +} + +TEST(ImageCompareTest, ImagesEqual_DifferentSizes) { + cv::Mat img1 = cv::Mat::zeros(100, 100, CV_8UC3); + cv::Mat img2 = cv::Mat::zeros(200, 200, CV_8UC3); + EXPECT_FALSE(images_equal(img1, img2)); +} + +TEST(ImageCompareTest, ImagesEqual_DifferentTypes) { + cv::Mat img1 = cv::Mat::zeros(100, 100, CV_8UC3); + cv::Mat img2 = cv::Mat::zeros(100, 100, CV_8UC1); + EXPECT_FALSE(images_equal(img1, img2)); +} + +TEST(ImageCompareTest, ImagesEqual_DifferentContent) { + cv::Mat img1 = cv::Mat::zeros(100, 100, CV_8UC3); + cv::Mat img2 = cv::Mat::ones(100, 100, CV_8UC3); + EXPECT_FALSE(images_equal(img1, img2)); +} + +} // namespace robot::common::video \ No newline at end of file