我是靠谱客的博主 贪玩茉莉,最近开发中收集的这篇文章主要介绍Homography estimation(旋转估计),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

以下内容节选自https://bitesofcode.wordpress.com/2017/09/12/augmented-reality-with-python-and-opencv-part-1/comment-page-1/#comment-61

807375-20171117170044109-1327935107.png

对于上图中,怎么估计(begin{bmatrix}h_{11} & h_{12} & h_{13} \ h_{21} & h_{22} & h_{23} \ h_{31} & h_{32} & h_{33} end{bmatrix})?
我们可以使用Random Aample Consebsus(RANSAC)

RANSAC算法过程如下:

  1. 均匀随机选择点集合中的一部分点;
  2. Fit a model to subset
  3. find all remaining points that are "close" to the model and reject the rest as outliers
  4. do this many times and choose the best model

举个例子,假设我们有一下点集,我们想用使用RANSAC来用一条直线拟合:
807375-20171117170835640-548727942.png
算法过程如下:

  1. sample (randomly) the number of points required to fit the model
  2. solve for model params using samples
  3. Score by the fraction of inliers within a present threshold 't' of the model
  4. repeat 1-3 until the best model is found with high confidence

运行这个算法的结果图如下,显示了第一次迭代的前三个步骤, 之后的是Score步骤:
807375-20171117171212515-1657756181.png

对于我们homograpy estimation,
807375-20171117171331109-1357415054.png
在Opencv中

# assuming matches stores the matches found and
# returned by bf.match(des_model, des_frame)
# differenciate between source points and destination points
src_pts = np.float32([kp_model[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp_frame[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
# compute Homography
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

以上代码中5.0是阈值距离来决定一个匹配是否跟预估的Homography 一致,在预估了homography之后,我们可以投影四个角点:

# Draw a rectangle that marks the found model in the frame
h, w = model.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
# project corners into frame
dst = cv2.perspectiveTransform(pts, M)
# connect them with lines
img2 = cv2.polylines(img_rgb, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)
cv2.imshow('frame', cap)
cv2.waitKey(0)

结果如下:
807375-20171117171814421-279198290.png

转载于:https://www.cnblogs.com/rogerjin/p/7852472.html

最后

以上就是贪玩茉莉为你收集整理的Homography estimation(旋转估计)的全部内容,希望文章能够帮你解决Homography estimation(旋转估计)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部