frame中src怎么设置成一个变量_opencv中应用ArUco标记的AR

先解释什么是ArUco标记,然后怎么用ArUco来做简单的AR任务。

ArUco标记已经被应用在相机姿态估计,相机校准等方面。

什么是ArUco标记

ArUco标记是2014年S.Garrido-Jurado开发出来的。

ArUco标记是一个基准点,其被放置在对象上或场景中。

一个ArUco标记外围都有一组黑色边框,同时内部有着确定该标记ID的二维矩阵组合而成。黑色的边框能加速标记在图像中的检测速度,内部的二维编码能唯一识别该标记,同时进行错误检测和错误修复。标记的大小确定了内部矩阵的大小,例如4x4大小的标记有16个bit(5x5就有25bits?)。

一般是打印这些ArUco标记,放在真实世界中,然后拍照,检测这些标记。

用OpenCV产生ArUco标记

可以使用opencv容易产生标记。opencv的aruco模块有25个预定义字典。

所有标记都包含相同的大小(4x4,5x5,6x6,7x7)。可以用getPredefinedDictionary函数来加载250个标记的字典。python代码如下:

import cv2 as cvimport numpy as np​# Load the predefined dictionarydictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)​# Generate the markermarkerImage = np.zeros((200, 200), dtype=np.uint8)
markerImage = cv.aruco.drawMarker(dictionary, 33, 200, markerImage, 1);cv.imwrite("marker33.png", markerImage);

检测ArUco标记

#Load the dictionary that was used to generate the markers.dictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)​# Initialize the detector parameters using default valuesparameters =  cv.aruco.DetectorParameters_create()​# Detect the markers in the imagemarkerCorners, markerIds, rejectedCandidates = cv.aruco.detectMarkers(frame, dictionary, parameters=parameters)

detectMarkers函数的第一个参数是带有标记的图像,第二个参数是产生标记的字典,

实际应用

# Calculate Homographyh, status = cv.findHomography(pts_src, pts_dst)​# Warp source image to destination based on homographywarped_image = cv.warpPerspective(im_src, h, (frame.shape[1],frame.shape[0]))​# Prepare a mask representing region to copy from the warped image into the original frame.mask = np.zeros([frame.shape[0], frame.shape[1]], dtype=np.uint8);cv.fillConvexPoly(mask, np.int32([pts_dst_m]), (255, 255, 255), cv.LINE_AA);​# Erode the mask to not copy the boundary effects from the warpingelement = cv.getStructuringElement(cv.MORPH_RECT, (3,3));mask = cv.erode(mask, element, iterations=3);​​# Copy the mask into 3 channels.warped_image = warped_image.astype(float)mask3 = np.zeros_like(warped_image)for i in range(0, 3):    mask3[:,:,i] = mask/255​# Copy the masked warped image into the original frame in the mask region.warped_image_masked = cv.multiply(warped_image, mask3)frame_masked = cv.multiply(frame.astype(float), 1-mask3)im_out = cv.add(warped_image_masked, frame_masked)

这个实例是把相框中的图像替换为了另外图片。就是利用检测aruco标记确定坐标来实现的。

d5af5f4749f461da5dcde1533d7d307c.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值