diff --git a/modules/imgproc/test/test_filter.cpp b/modules/imgproc/test/test_filter.cpp index 011cbaab0b..a0ccf4372f 100644 --- a/modules/imgproc/test/test_filter.cpp +++ b/modules/imgproc/test/test_filter.cpp @@ -2380,4 +2380,176 @@ TEST(Imgproc, morphologyEx_small_input_22893) ASSERT_EQ(0, cvtest::norm(result, gold, NORM_INF)); } +TEST(Imgproc_sepFilter2D, identity) +{ + std::vector kernelX{0, 0, 0, 1, 0, 0, 0}; + std::vector kernelY{0, 0, 1, 0, 0}; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, input.depth(), kernelX, kernelY); + + EXPECT_EQ(0, cv::norm(result, input, NORM_INF)); +} + +TEST(Imgproc_sepFilter2D, shift) +{ + std::vector kernelX{1, 0, 0}; + std::vector kernelY{0, 0, 1}; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, input.depth(), kernelX, kernelY); + + int W = input.cols; + int H = input.rows; + Mat inputCrop = input(Range(1, H), Range(0, W - 1)); + Mat resultCrop = result(Range(0, H - 1), Range(1, W)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + // Checking borders. Should be BORDER_REFLECT_101 + + inputCrop = input(Range(H - 2, H - 1), Range(0, W - 1)); + resultCrop = result(Range(H - 1, H), Range(1, W)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + inputCrop = input(Range(1, H), Range(1, 2)); + resultCrop = result(Range(0, H - 1), Range(0, 1)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + inputCrop = input(Range(H - 2, H - 1), Range(1, 2)); + resultCrop = result(Range(H - 1, H), Range(0, 1)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); +} + +TEST(Imgproc_sepFilter2D, zeroPadding) +{ + std::vector kernelX{1, 0, 0}; + std::vector kernelY{0, 0, 1}; + Point anchor(-1, -1); + double delta = 0; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, input.depth(), kernelX, kernelY, anchor, delta, BORDER_CONSTANT); + + int W = input.cols; + int H = input.rows; + Mat inputCrop = input(Range(1, H), Range(0, W - 1)); + Mat resultCrop = result(Range(0, H - 1), Range(1, W)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + // Checking borders + + resultCrop = result(Range(H - 1, H), Range(0, W)); + EXPECT_EQ(0, cv::norm(resultCrop, NORM_INF)); + + resultCrop = result(Range(0, H), Range(0, 1)); + EXPECT_EQ(0, cv::norm(resultCrop, NORM_INF)); +} + +TEST(Imgproc_sepFilter2D, anchor) +{ + std::vector kernelX{0, 1, 0}; + std::vector kernelY{0, 1, 0}; + Point anchor(2, 0); + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, input.depth(), kernelX, kernelY, anchor); + + int W = input.cols; + int H = input.rows; + Mat inputCrop = input(Range(1, H), Range(0, W - 1)); + Mat resultCrop = result(Range(0, H - 1), Range(1, W)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + // Checking borders. Should be BORDER_REFLECT_101 + + inputCrop = input(Range(H - 2, H - 1), Range(0, W - 1)); + resultCrop = result(Range(H - 1, H), Range(1, W)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + inputCrop = input(Range(1, H), Range(1, 2)); + resultCrop = result(Range(0, H - 1), Range(0, 1)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); + + inputCrop = input(Range(H - 2, H - 1), Range(1, 2)); + resultCrop = result(Range(H - 1, H), Range(0, 1)); + EXPECT_EQ(0, cv::norm(resultCrop, inputCrop, NORM_INF)); +} + +TEST(Imgproc_sepFilter2D, delta) +{ + std::vector kernelX{0, 0.5, 0}; + std::vector kernelY{0, 1, 0}; + Point anchor(1, 1); + double delta = 5; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, input.depth(), kernelX, kernelY, anchor, delta); + + Mat gt = input / 2 + delta; + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +typedef testing::TestWithParam Imgproc_sepFilter2D_outTypes; +TEST_P(Imgproc_sepFilter2D_outTypes, simple) +{ + int outputType = GetParam(); + std::vector kernelX{0, 0.5, 0}; + std::vector kernelY{0, 0.5, 0}; + Point anchor(1, 1); + double delta = 5; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::sepFilter2D(input, result, outputType, kernelX, kernelY, anchor, delta); + + input.convertTo(input, outputType); + Mat gt = input / 4 + delta; + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +INSTANTIATE_TEST_CASE_P(/**/, Imgproc_sepFilter2D_outTypes, + testing::Values(CV_16S, CV_32F, CV_64F), +); + +typedef testing::TestWithParam Imgproc_sepFilter2D_types; +TEST_P(Imgproc_sepFilter2D_types, simple) +{ + int outputType = GetParam(); + std::vector kernelX{0, 0.5, 0}; + std::vector kernelY{0, 0.5, 0}; + Point anchor(1, 1); + double delta = 5; + + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + input.convertTo(input, outputType); + Mat result; + + cv::sepFilter2D(input, result, outputType, kernelX, kernelY, anchor, delta); + + Mat gt = input / 4 + delta; + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +INSTANTIATE_TEST_CASE_P(/**/, Imgproc_sepFilter2D_types, + testing::Values(CV_16S, CV_32F, CV_64F), +); + }} // namespace diff --git a/modules/imgproc/test/test_thresh.cpp b/modules/imgproc/test/test_thresh.cpp index 3eb096a655..f14f2e5716 100644 --- a/modules/imgproc/test/test_thresh.cpp +++ b/modules/imgproc/test/test_thresh.cpp @@ -541,4 +541,58 @@ TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_21258_Max) EXPECT_EQ(0, cv::norm(result, NORM_INF)); } +TEST(Imgproc_AdaptiveThreshold, mean) +{ + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::adaptiveThreshold(input, result, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 8); + + const string gt_path = cvtest::findDataFile("../cv/imgproc/adaptive_threshold1.png"); + Mat gt = imread(gt_path, IMREAD_GRAYSCALE); + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +TEST(Imgproc_AdaptiveThreshold, mean_inv) +{ + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::adaptiveThreshold(input, result, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 15, 8); + + const string gt_path = cvtest::findDataFile("../cv/imgproc/adaptive_threshold1.png"); + Mat gt = imread(gt_path, IMREAD_GRAYSCALE); + gt = Mat(gt.rows, gt.cols, CV_8UC1, cv::Scalar(255)) - gt; + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +TEST(Imgproc_AdaptiveThreshold, gauss) +{ + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::adaptiveThreshold(input, result, 200, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 21, -5); + + const string gt_path = cvtest::findDataFile("../cv/imgproc/adaptive_threshold2.png"); + Mat gt = imread(gt_path, IMREAD_GRAYSCALE); + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + +TEST(Imgproc_AdaptiveThreshold, gauss_inv) +{ + const string input_path = cvtest::findDataFile("../cv/shared/baboon.png"); + Mat input = imread(input_path, IMREAD_GRAYSCALE); + Mat result; + + cv::adaptiveThreshold(input, result, 200, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 21, -5); + + const string gt_path = cvtest::findDataFile("../cv/imgproc/adaptive_threshold2.png"); + Mat gt = imread(gt_path, IMREAD_GRAYSCALE); + gt = Mat(gt.rows, gt.cols, CV_8UC1, cv::Scalar(200)) - gt; + EXPECT_EQ(0, cv::norm(result, gt, NORM_INF)); +} + }} // namespace