Matching shapes with Hausdorff and Shape Context distance

asked 2015-04-30 15:34:01 -0500

cza-57 gravatar image

updated 2015-05-02 04:37:22 -0500

I want to compute a distance between two shapes using the Hausdorff distance or shape context distance measure available in OpenCV 3.0. The shapes are simple white shapes on a black background.

In order to find the distance between two shapes, I find contours of each shape and then pass the contours two the following functions: ShapeDistanceExtractor::computeDistance(contours1, countours2) and HausdorffDistanceExtractor::computeDistance(contours1, countours2).

Could anyone please explain to me, why during the comparison the ShapeDistanceExtractor always returns 0.0, whereas the second method gives me different results depending on the position of a character on the image?

edit retag flag offensive close merge delete

Comments

1

I would like to highlight that I am using the Python version of OpenCV 3.0. I have just checked that in the C++ version everything seems to be working fine. Has anyone stumbled upon such a problem?

cza-57 gravatar imagecza-57 ( 2015-05-01 05:17:41 -0500 )edit

hmm, at least i can side your findings:

datapath = "E:/code/opencv/samples/data/shape_sample/";
a = cv2.imread(datapath+"1.png",0);
b = cv2.imread(datapath+"2.png",0);

_, ca, _ = cv2.findContours(a, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) 
_, cb, _ = cv2.findContours(b, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) 
print np.shape(ca[0]) , np.shape(cb[0])

hd = cv2.createHausdorffDistanceExtractor()
sd = cv2.createShapeContextDistanceExtractor()

d1 = hd.computeDistance(ca[0],cb[0])
d2 = sd.computeDistance(ca[0],cb[0])

print d1, " ", d2

(110, 1, 2) (126, 1, 2)
32.3109893799   0.0
berak gravatar imageberak ( 2015-05-02 01:06:00 -0500 )edit

pretty weird, that the c++ version even has a different value for hausdorff distance:

vector<vector<Point>> ca,cb;
findContours(a, ca, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
findContours(b, cb, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
cerr << ca.size() << "x" << ca[0].size() << " " << cb.size() << "x" << cb[0].size() << endl;

Ptr<HausdorffDistanceExtractor> hd = createHausdorffDistanceExtractor();
Ptr<ShapeContextDistanceExtractor> sd = createShapeContextDistanceExtractor();

double d1 = hd->computeDistance(ca[0],cb[0]);
double d2 = sd->computeDistance(ca[0],cb[0]);
cerr << d1 << " " << d2 << endl;

1x110 1x126
26.4197 0.258042
berak gravatar imageberak ( 2015-05-02 01:51:21 -0500 )edit

Hmm, now it looks like a definite bug. I thought I passed some wrong arguments to the computeDistance function but I see I did it the same way as you.

cza-57 gravatar imagecza-57 ( 2015-05-02 04:36:54 -0500 )edit

btw, i checked the generated wrapper function, it takes exactly the same default args as it's c++ counterpart. call me puzzled.

berak gravatar imageberak ( 2015-05-02 04:44:51 -0500 )edit