我正在尝试在跟踪对象上绘制一个3D立方体(使用ORB和对象的单应性) - opencv Python

问题描述 投票:0回答:1

现在,我只能在被跟踪对象周围绘制一个正方形(或其他东西),如下所示:What I can do now但我试图在任何纹理对象上执行此操作:What I want to do, but on any textured object

这是我的代码的一部分,我打算使用单应性绘图。抱歉我的英语。

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)
python opencv tracking homography orb
1个回答
0
投票

您需要知道相机校准才能做到这一点。我刚刚回答了今天发布的类似问题,我认为这会有所帮助。祝好运!

rotation and translation matrix from homography opencv

© www.soinside.com 2019 - 2024. All rights reserved.