我正在尝试在跟踪对象上绘制 3D 立方体(使用 ORB 和对象的单应性)- opencv Python
I'm trying to draw a 3D cube on a tracked object (using ORB and the object's homography) - opencv Python
现在,我只能在跟踪对象周围绘制一个正方形(或其他东西),如下所示:
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)
您需要知道相机校准才能做到这一点。我刚刚回答了今天发布的类似问题,我认为它会有所帮助。祝你好运!
现在,我只能在跟踪对象周围绘制一个正方形(或其他东西),如下所示: 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)
您需要知道相机校准才能做到这一点。我刚刚回答了今天发布的类似问题,我认为它会有所帮助。祝你好运!