I am trying to detect ArUco Markers to recognise some real moving objects, to write my Degree Thesis. Fisrt, I tried with some 4x4 ArUco Markers, i got good results, then i tried with an image that i got from my camera. This image contains 5x5 ArUco Markers, applying same code, except changing Dictionaries, 4x4 per 5x5, why is only detecting 4x4 markers ?
Results: 4x4 Markers detected 1
5x5 Markers not detected 2
import cv2
import numpy as np
from cv2 import aruco
import camera_calibration
path='/home/alejandrocruzado/Documents/Imaginni/aruco4.png'
path_5 = '/home/alejandrocruzado/Documents/Imaginni/aruco5.jpg'
path5x5 = '/home/alejandrocruzado/Documents/Imaginni/5x5.jpg'
distCoeffs=[0.0189223469433419, -0.0206788674793396,0.003225513523750, 0.001510000668961]
cameraMatrix=[[600.8293, 0, 330.2756],[0, 601.3519, 225.9791],[0, 0, 1.0000]]
def MarkerDetection4x4(path):
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
aruco_dict = cv2.aruco_Dictionary.get(aruco.DICT_4X4_1000)
parameters = cv2.aruco_DetectorParameters.create()
corners, ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
cv2.aruco.drawDetectedMarkers(img,corners,ids,)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
rvecs, tvecs, _objPoints = cv2.aruco.estimatePoseSingleMarkers(corners, 0.045, cameraMatrix, distCoeffs)
cv2.aruco.drawAxis(img, cameraMatrix, distCoeff, rvecs, tvecs, 0.1)
def MarkerDetection5x5(path):
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
aruco_dict = cv2.aruco_Dictionary.get(aruco.DICT_5X5_250)
parameters = cv2.aruco_DetectorParameters.create()
corners, ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
cv2.aruco.drawDetectedMarkers(img,corners,ids,)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
rvecs, tvecs, _objPoints = cv2.aruco.estimatePoseSingleMarkers(corners, 0.045, cameraMatrix, distCoeffs)
cv2.aruco.drawAxis(img, cameraMatrix, distCoeff, rvecs, tvecs, 0.1)
if __name__ == "__main__":
#MarkerDetection4x4(path)
MarkerDetection5x5(path5x5)
Thanks
答案 0 :(得分:0)
您可以使用以下代码在被拒绝的标记候选项周围画线:
for rejected in rejected_img_points:
rejected = rejected.reshape((4, 2))
cv2.line(img, tuple(rejected[0]), tuple(rejected[1]), (0, 0, 255), thickness=2)
cv2.line(img, tuple(rejected[1]), tuple(rejected[2]), (0, 0, 255), thickness=2)
cv2.line(img, tuple(rejected[2]), tuple(rejected[3]), (0, 0, 255), thickness=2)
cv2.line(img, tuple(rejected[3]), tuple(rejected[0]), (0, 0, 255), thickness=2)
如果看到标记图像被正确检测但被拒绝,则可能是使用与绘制这些标记的字典不同的字典进行检测。标记字典已更改。
使用以下代码创建一组新的5x5标记,然后重试:
marker_id = 5
# The image size for marker should be a
# multiple of (number of bits + 2 * border) or
# very large compared to number of bits
marker_size = 140
marker_dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_5X5_50)
marker_imge = np.zeros((marker_size, marker_size), np.uint8)
marker_img = cv2.aruco.drawMarker(marker_dictionary, marker_id, marker_size, marker_img, 1)