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代码如下:

<code>import cv2 as cv
import numpy as np

# Load the predefined dictionary
dictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)

# Generate the marker
markerImage = np.zeros((200, 200), dtype=np.uint8)/<code>
<code>markerImage = cv.aruco.drawMarker(dictionary, 33, 200, markerImage, 1);
cv.imwrite("marker33.png", markerImage);/<code>

检测ArUco标记

<code>#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 values
parameters = cv.aruco.DetectorParameters_create()

# Detect the markers in the image
markerCorners, markerIds, rejectedCandidates = cv.aruco.detectMarkers(frame, dictionary, parameters=parameters)/<code>

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

实际应用

<code># Calculate Homography
h, status = cv.findHomography(pts_src, pts_dst)

# Warp source image to destination based on homography
warped_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 warping
element = 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)/<code>

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


opencv中应用ArUco标记的AR


分享到:


相關文章: