Python 如何在OpenCv aruco跟踪程序中添加绘图
我制作了一个简单的程序,可以检测和跟踪阿鲁科标记。它可以显示标记的坐标(tvec)。我的目标是使用.subplot函数生成一个具有x、y、z坐标的实时图形。我想在新窗口中显示此绘图。我可以在我的程序中制作它,还是我必须制作另一个 我很乐意听取任何意见或建议Python 如何在OpenCv aruco跟踪程序中添加绘图,python,opencv,matplotlib,aruco,Python,Opencv,Matplotlib,Aruco,我制作了一个简单的程序,可以检测和跟踪阿鲁科标记。它可以显示标记的坐标(tvec)。我的目标是使用.subplot函数生成一个具有x、y、z坐标的实时图形。我想在新窗口中显示此绘图。我可以在我的程序中制作它,还是我必须制作另一个 我很乐意听取任何意见或建议 import numpy as np import cv2 import cv2.aruco as aruco camera_matrix = np.loadtxt('/home/maciej/PycharmProjects/Aruco_
import numpy as np
import cv2
import cv2.aruco as aruco
camera_matrix = np.loadtxt('/home/maciej/PycharmProjects/Aruco_Project/cameraMatrix.txt', delimiter=',')
camera_distortion = np.loadtxt('/home/maciej/PycharmProjects/Aruco_Project/cameraDistortion.txt', delimiter=',')
font = cv2.FONT_HERSHEY_PLAIN
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_ARUCO_ORIGINAL)
parameters = aruco.DetectorParameters_create()
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while True:
ret, frame = cap.read()
# operations on the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# set dictionary size depending on the aruco marker selected
aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)
# detector parameters can be set here (List of detection parameters[3])
parameters = aruco.DetectorParameters_create()
# lists of ids and the corners belonging to each id
corners, ids, rejectedImgPoints = aruco.detectMarkers(image=gray, dictionary=aruco_dict, parameters=parameters, cameraMatrix=camera_matrix, distCoeff=camera_distortion)
# font for displaying text (below)
font = cv2.FONT_HERSHEY_SIMPLEX
# check if the ids list is not empty
# if no check is added the code will crash
if np.all(ids is not None):
# estimate pose of each marker and return the values
# rvet and tvec-different from camera coefficients
rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners, 0.1, camera_matrix, camera_distortion)
# (rvec-tvec).any() # get rid of that nasty numpy value array error
for i in range(0, ids.size):
# draw axis for the aruco markers
aruco.drawAxis(frame, camera_matrix, camera_distortion, rvec[i], tvec[i], 0.1)
# draw a square around the markers
aruco.drawDetectedMarkers(frame, corners)
id_marker = str(ids[0][0])
cv2.putText(frame, "Id: " + id_marker, (0, 70), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "x=%.1f y=%.1f z=%.1f" % ((tvec[0][0][0] * 100), (tvec[0][0][1] * 100), (tvec[0][0][2] * 100)), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
else:
# code to show 'No Ids' when no markers are found
cv2.putText(frame, "No Ids", (0, 64), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()