问题描述
我编写了一个简单的程序,该程序可以检测并跟踪Aruco标记。它可以显示标记(tvec)的坐标。我的目标是使用.subplots函数制作带有x,y,z坐标的实时图形。我想在新窗口中显示该图。我可以在程序中制作它还是必须制作另一个?
对于任何想法或建议,我都会感到高兴。
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',')
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_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,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)),40),cv2.FONT_HERSHEY_SIMPLEX,255),cv2.LINE_AA)
else:
# code to show 'No Ids' when no markers are found
cv2.putText(frame,"No Ids",64),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()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)