Search code examples
pythonopencvtrackinghomographyorb

I'm trying to draw a 3D cube on a tracked object (using ORB and the object's homography) - opencv Python


At now, I'm just capable to draw a square (or something else) around the tracked object, something like this: What I can do now But I'm trying to do this on any textured object: What I want to do, but on any textured object

That's a part of my code, I'm planing to use the homography for drawing. Sorry about my english.

while(True):
ret, frame = cap.read(0)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create(nfeatures=500)

kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(frame, None)
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(des1, des2, None)

matches.sort(key=lambda x: x.distance, reverse=False)

numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)

for i, match in enumerate(matches):
    points1[i, :] = kp1[match.queryIdx].pt
    points2[i, :] = kp2[match.trainIdx].pt
M, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
h, w = img1.shape[:2]
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
pts = [np.int32(dst)]

frame = cv2.polylines(frame, pts, True, (255,0,255), 1, cv2.LINE_AA)

Solution

  • You will need to know the camera calibration to do that. I just answered a similar question posted today, I think it will help. Good luck!

    rotation and translation matrix from homography opencv