features2d usage issue (in mexopencv)

asked 2012-10-11 11:51:45 -0500

8mike gravatar image

updated 2012-10-26 05:39:52 -0500

Rui Marques gravatar image

Hello, i am using opencv in matlab through the library mexopencv. The creator of the library said that my issues should be asked directly to the creators of opencv so here i am and thanks in advance for your help.

I am trying different algorithms for keypoints detection, description and matching. My problem is that the results are not satisfying even if i directly use the same procedure shown in your tutorial with the same image (bastoncini di pesce box). So here is the code i am using:

a = imread('scene.pgm');
b = imread('box.pgm');

detector = cv.FeatureDetector('SURF');
k1 = detector.detect(a);
k2 = detector.detect(b);

extractor = cv.DescriptorExtractor('SURF');
d1 = extractor.compute(a, k1);
d2 = extractor.compute(b, k2);

matcher = cv.DescriptorMatcher('FlannBased');
matches = matcher.match(d1, d2);

n = numel(matches);
maxDist = 0; minDist = Inf;
for i = 1:n
  if(matches(i).distance > maxDist) maxDist = matches(i).distance; end
  if(matches(i).distance < minDist) minDist = matches(i).distance; end

cont = 1;
i = 1;
while(i < n)
  if(matches(i).distance <= minDist*2)
    goodMatches(cont) = matches(i);
    cont = cont + 1;
  i = i + 1;

n = numel(goodMatches);
for i=1:n
  srcPoints(1,i,1) = k1(goodMatches(i).queryIdx + 1).pt(1);
  srcPoints(1,i,2) = k1(goodMatches(i).queryIdx + 1).pt(2);
  dstPoints(1,i,1) = k2(goodMatches(i).trainIdx + 1).pt(1);
  dstPoints(1,i,2) = k2(goodMatches(i).trainIdx + 1).pt(2);

[H, ok] = cv.findHomography(srcPoints, dstPoints, 'Method', 'Ransac');

//project corners of image a in b
[h,l] = size(b);
p1 = H * [0; 0; 1];
p2 = H * [l; 0; 1];
p3 = H * [l; h; 1];
p4 = H * [0; h; 1];
x3 = [p1 p2 p3 p4];

inliers = [];
cont = 1;
for i = 1:numel(ok)
  if(ok(i) == 1)
    inliers(cont) = i;
    cont = cont + 1;

for j = 1:numel(inliers)
  newMatches(j) = goodMatches(inliers(j));

imshow(cv.drawMatches(a, k1, b, k2, newMatches));

Then i plot the corners on b. Here are the results:

  • Using minDist*2 as threshold:

using minDist*2 as threshold

  • Corner points plotted on b:

corner points plotted on b

  • Using minDist*3 as threshold:

using minDist*3 as threshold

  • No threshold:

no threshold

edit retag flag offensive close merge delete


Hi, I'm using the mexopencv in matlab. But i encountered a very tough problem: When I use mexopencv in my matlab, the matlab will breakdown and close. I wonder why? I just install my mexopencv following the instruction given by the original author. I download the mexopencv.zip file and compile it in the matlab 2010Ra with the compiler of VS2008. I can coompile it successfully, and generate all the mexwin32 file in +cv filefolder. But when I use the mexopencv in matlab ,the matlab will bereakdown and close up. I think you must install it successfully. I need your help urgently, and I will be very grateful if you can help me out. Thank you!

yxwang11 gravatar imageyxwang11 ( 2012-10-26 02:56:58 -0500 )edit

@yxwang11, the original question is more related to opencv, but your question is related to mexopencv, so you should ask it to the author, not here.

Rui Marques gravatar imageRui Marques ( 2012-10-26 05:34:54 -0500 )edit

Ok, thank you also the same.

yxwang11 gravatar imageyxwang11 ( 2012-10-26 08:29:27 -0500 )edit