kyamagu / mexopencv

Collection and a development kit of matlab mex functions for OpenCV library
http://kyamagu.github.io/mexopencv
Other
659 stars 318 forks source link

Remove Duplicate Keypoints not working #422

Closed youthiya closed 5 years ago

youthiya commented 5 years ago

Hello. I am working on removal of duplicate keypoints after SIFT feature detection but following function is not removing the duplicate keypoints (that have same coordinates but different orientation).

keypoints = cv.KeyPointsFilter.removeDuplicated(keypoints)

Is this function coded wrong? What could be the issue?

amroamroamro commented 5 years ago

A keypoint struct has the following fields:

Looking at the opencv source code of the removeDuplicated function (see here), two keypoints are considered duplicate if they have the same pt, size, and angle (all three fields):

img = imread('image.jpg');
obj = cv.SIFT();
kpts = obj.detect(img);

kpts0 = cv.KeyPointsFilter.removeDuplicated(kpts);

If you want to consider only pt (coordinates) when removing duplicates, you'll have to filter the points yourself. You can do this in plain MATLAB:

pt = cat(1, kpts.pt);
[~,ind] = unique(pt, 'rows');
kpts1 = kpts(ind);

You can make this fancier by using uniquetol instead of unique to perform comparison with a tolerance instead of an exact comparison (i.e. keep points that are at least a distance tolerance apart, not necessarily exactly equal):

pt = cat(1, kpts.pt);
tol = 0.1;
[~,ind] = uniquetol(pt, tol, 'ByRows',true, 'DataScale',[1 1]);
kpts2 = kpts(ind);
youthiya commented 5 years ago

Thanks a lot. Issue is clear now.