From a2b6b595c20c15f7babe7e01b68df0e83be8c67f Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 17 Jan 2017 15:22:56 +0300 Subject: [PATCH] shape: force column-based vector --- modules/python/test/test_shape.py | 23 +++++++++++++++++++++++ modules/shape/src/haus_dis.cpp | 7 +++++++ modules/shape/src/sc_dis.cpp | 7 +++++++ modules/shape/test/test_shape.cpp | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 modules/python/test/test_shape.py diff --git a/modules/python/test/test_shape.py b/modules/python/test/test_shape.py new file mode 100644 index 0000000000..ad0f0be5d5 --- /dev/null +++ b/modules/python/test/test_shape.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import cv2 + +from tests_common import NewOpenCVTests + +class shape_test(NewOpenCVTests): + + def test_computeDistance(self): + + a = self.get_sample('samples/data/shape_sample/1.png', cv2.IMREAD_GRAYSCALE); + b = self.get_sample('samples/data/shape_sample/2.png', cv2.IMREAD_GRAYSCALE); + + _, ca, _ = cv2.findContours(a, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) + _, cb, _ = cv2.findContours(b, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) + + hd = cv2.createHausdorffDistanceExtractor() + sd = cv2.createShapeContextDistanceExtractor() + + d1 = hd.computeDistance(ca[0], cb[0]) + d2 = sd.computeDistance(ca[0], cb[0]) + + self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor") + self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor") diff --git a/modules/shape/src/haus_dis.cpp b/modules/shape/src/haus_dis.cpp index 6f372c416d..732f288946 100644 --- a/modules/shape/src/haus_dis.cpp +++ b/modules/shape/src/haus_dis.cpp @@ -138,6 +138,13 @@ float HausdorffDistanceExtractorImpl::computeDistance(InputArray contour1, Input set2.convertTo(set2, CV_32F); CV_Assert((set1.channels()==2) && (set1.cols>0)); CV_Assert((set2.channels()==2) && (set2.cols>0)); + + // Force vectors column-based + if (set1.dims > 1) + set1 = set1.reshape(2, 1); + if (set2.dims > 1) + set2 = set2.reshape(2, 1); + return std::max( _apply(set1, set2, distanceFlag, rankProportion), _apply(set2, set1, distanceFlag, rankProportion) ); } diff --git a/modules/shape/src/sc_dis.cpp b/modules/shape/src/sc_dis.cpp index 89c6d91255..bbda11cff0 100644 --- a/modules/shape/src/sc_dis.cpp +++ b/modules/shape/src/sc_dis.cpp @@ -202,6 +202,13 @@ float ShapeContextDistanceExtractorImpl::computeDistance(InputArray contour1, In CV_Assert((set1.channels()==2) && (set1.cols>0)); CV_Assert((set2.channels()==2) && (set2.cols>0)); + + // Force vectors column-based + if (set1.dims > 1) + set1 = set1.reshape(2, 1); + if (set2.dims > 1) + set2 = set2.reshape(2, 1); + if (imageAppearanceWeight!=0) { CV_Assert((!image1.empty()) && (!image2.empty())); diff --git a/modules/shape/test/test_shape.cpp b/modules/shape/test/test_shape.cpp index 0601594f08..97a621e4b5 100644 --- a/modules/shape/test/test_shape.cpp +++ b/modules/shape/test/test_shape.cpp @@ -299,3 +299,22 @@ TEST(Hauss, regression) ShapeBaseTest test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val); test.safe_run(); } + +TEST(computeDistance, regression_4976) +{ + Mat a = imread(cvtest::findDataFile("shape/samples/1.png"), 0); + Mat b = imread(cvtest::findDataFile("shape/samples/2.png"), 0); + + vector > ca,cb; + findContours(a, ca, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS); + findContours(b, cb, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS); + + Ptr hd = createHausdorffDistanceExtractor(); + Ptr sd = createShapeContextDistanceExtractor(); + + double d1 = hd->computeDistance(ca[0],cb[0]); + double d2 = sd->computeDistance(ca[0],cb[0]); + + EXPECT_NEAR(d1, 26.4196891785, 1e-3) << "HausdorffDistanceExtractor"; + EXPECT_NEAR(d2, 0.25804194808, 1e-3) << "ShapeContextDistanceExtractor"; +}