Skip to content

Commit

Permalink
adding video common (correct this time) (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
efahnestock authored Dec 10, 2024
1 parent 580fe60 commit 616f187
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ cc_test(
"@bs_thread_pool",
"@com_google_googletest//:gtest_main",
],
)
)
21 changes: 21 additions & 0 deletions common/video/BUILD
Original file line number Diff line number Diff line change
@@ -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",
]
)
15 changes: 15 additions & 0 deletions common/video/image_compare.cc
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions common/video/image_compare.hh
Original file line number Diff line number Diff line change
@@ -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);

}
33 changes: 33 additions & 0 deletions common/video/image_compare_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "common/video/image_compare.hh"

#include <gtest/gtest.h>

#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

0 comments on commit 616f187

Please sign in to comment.