convexityDefects computes wrong result
Hi,
when running the following code snippet:
vector<Point> c = { { 356, 339 }, { 355, 340 }, { 353, 340 }, { 350, 343 }, { 349, 343 }, { 347, 345 }, { 344, 345 }, { 343, 346 }, { 334, 346 }, { 330, 350 }, { 331, 350 }, { 332, 351 }, { 334, 351 }, { 335, 352 }, { 336, 352 }, { 338, 354 }, { 339, 354 }, { 340, 355 }, { 340, 354 }, { 341, 353 }, { 342, 353 }, { 343, 352 }, { 344, 352 }, { 345, 351 }, { 349, 351 }, { 350, 350 }, { 352, 350 }, { 353, 349 }, { 354, 349 }, { 355, 350 }, { 356, 350 }, { 357, 351 }, { 357, 353 }, { 354, 356 }, { 354, 357 }, { 352, 359 }, { 353, 360 }, { 350, 363 }, { 350, 364 }, { 351, 365 }, { 351, 366 }, { 353, 368 }, { 353, 369 }, { 355, 371 }, { 355, 372 }, { 356, 371 }, { 356, 369 }, { 358, 367 }, { 358, 366 }, { 361, 363 }, { 361, 357 }, { 362, 356 }, { 362, 354 }, { 364, 352 }, { 364, 351 }, { 367, 348 }, { 368, 348 }, { 369, 347 }, { 367, 345 }, { 367, 343 }, { 366, 343 }, { 363, 340 }, { 359, 340 }, { 358, 339 } };
Mat img(480, 640, CV_8UC3, Scalar(0, 0, 0));
polylines( img, c, true, Scalar( 255, 0, 0 ), 1 );
vector<int> h;
convexHull( c, h );
vector<Point> hp;
for( const auto &e : h )
{
hp.push_back( c[ e ] );
}
cv::polylines( img, hp, true, Scalar( 0, 255, 0 ), 1 );
vector<Vec4i> d;
convexityDefects( c, h, d );
for( const auto &e : d )
{
circle( img, c[ e[ 2 ] ], 1, Scalar( 0, 0, 255 ), 1 );
}
I am getting the follwing output:
h: [57, 49, 45, 44, 9, 8, 0, 63, 61, 59]
d: [[57, 59, 9, 9273], [59, 61, 60, 154], [61, 63, 62, 201], [0, 8, 5, 765], [9, 44, 30, 4397], [45, 49, 46, 271], [49, 57, 52, 801]]
I am looking for the defect that is the farthest away from the hull which is: [9, 44, 30, 4397] which is located at the center of the image
The problem is, there is a wrong result: [57, 59, 9, 9273] which is even further away. In the image this wrong result is the the most left cross which is located on the hull and should have a depth of 0 and not 9273
Am I doing something wrong or is this a bug?
Thanks Chris
Edit: I created this bug report over there: click
but how are you trying to find the distance between the hull and the defects?
convexityDefects() already computes the distance of the defect from the hull, its index 3 of the result Vec4i
@Christoph Pacher you are right. Check my answer below.