Ask Your Question
0

[BFMatcher] 'list' object has no attribute 'trainIdx'

asked 2018-02-08 12:21:16 -0600

MattMgn gravatar image

Hello

I am trying to get attributes from Matcher object. Even if I can successfully get distance attributes, I can't get trainIdx one.

print(matches.trainIdx[:10]) AttributeError: 'list' object has no attribute 'trainIdx'

As it would be possible, as define here:

The result of matches = bf.match(des1,des2) line is a list of DMatch objects. This DMatch object has following attributes:

  • DMatch.distance - Distance between descriptors. The lower, the better it is.
  • DMatch.trainIdx - Index of the descriptor in train descriptors
  • DMatch.queryIdx - Index of the descriptor in query descriptors
  • DMatch.imgIdx - Index of the train image.

Code I use is:

imgL = cv2.imread('im0.png')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)

imgR = cv2.imread('im1.png')
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)

# Initiate SIFT DETECTOR
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kpL, desL = sift.detectAndCompute(grayL, None)
kpR, desR = sift.detectAndCompute(grayR, None)

print("# kp: {}, descriptors: {}".format(len(kpL), desL.shape))

# create BFMatcher object
bf = cv2.BFMatcher()

# Match descriptors.
matches = bf.match(desL,desR)

print(matches.trainIdx[:10])

matt

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-02-08 13:58:42 -0600

updated 2018-02-08 14:02:34 -0600

One way of avoiding/understanding these errors is printing out the type.

When you run print(type(matches)) you will see that bf.match(desL,desR) returns a list. So accessing trainIdx on the entire list makes no sense at all.

Instead, you want to first extract a particular match inside this list. For example, if you want to access the first match, do matches[0].

The second problem in your code is trainIdx[:10]. This means that trainIdx returns a list and you are trying to access every element in it from index 0 to index 10. But if you again run print(type(matches[0].trainIdx)), it will return an int and not a list.

So the correct syntax for accessing the training id of the first match should be print(matches[0].trainIdx).

I will let you figure out how to access the remaining training idxs of all the matches.

Whenever in doubt, refer to the documentation.

Cheers :)

edit flag offensive delete link more

Comments

Allright, I understand ! Thank you eshirima

matt

MattMgn gravatar imageMattMgn ( 2018-02-13 15:32:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-08 12:21:16 -0600

Seen: 2,880 times

Last updated: Feb 08 '18