这段代码可以很好地扭曲我的图像
kp1, des1 = self._detector.detectAndCompute(static, None)
kp2, des2 = self._detector.detectAndCompute(moving, None)
matches = self._matcher.match(des1, des2)
assert len(matches) >= 4 # for perspective tform
moving_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
static_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
tform, mask = cv2.findHomography(moving_pts, static_pts, cv2.RANSAC, 5.0)
tform = self._force_translation_only(tform)
matches_mask = mask.ravel().tolist()
matches_image = cv2.drawMatches(static, kp1, moving, kp2, matches, None,
flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS,
matchesMask=matches_mask,
matchColor=(0, 255, 0),)
warped = cv2.warpPerspective(moving, tform, (static.shape[1], static.shape[0]), flags=cv2.WARP_INVERSE_MAP)
并得出以下结论
我想要一个非黑色部分的面具,当然也要做一个。warped>0
是不够好的,因为它不会捕获真正的黑色数据像素。
我相信有一个正确的方法可以做到这一点。
正如Miki在评论中所建议的那样,你可以翘曲一个相同大小的白色图像,但这样一来,正如你所指出的那样,翘曲要做两次。
相反,你可以翘曲4个角,然后画一个白色的多边形,用 cv2.polylines
但我不确定它是否会比扭曲白色图像更有效率。