Änderungen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche

OpenCV mit Python

1.501 Byte hinzugefügt, 17:39, 1. Apr. 2019
camera.close()
</pre>
 
[[Datei:OpencvWorkshopOpticalFlow.jpg|400px]]
 
== ArUco - Pose Estimation ==
<pre>
import numpy as np
import cv2
import cv2.aruco as aruco
 
cap = cv2.VideoCapture(0)
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
parameters = aruco.DetectorParameters_create()
 
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
gray = aruco.drawDetectedMarkers(gray, corners, ids)
 
mtx = np.array([[5.3434144579284975e+02, 0., 3.3915527836173959e+02],
[0., 5.3468425881789324e+02, 2.3384359492532246e+02],
[0., 0., 1.]], np.float)
dist = np.array([-2.8832098285875657e-01, 5.4107968489116441e-02,
1.7350162244695508e-03, -2.6133389531953340e-04,
2.0411046472667685e-01], np.float)
 
if ids != None: # if aruco marker detected
rvec, tvec, objp = aruco.estimatePoseSingleMarkers(corners, 3.5, mtx, dist) # For a single marker
aruco.drawDetectedMarkers(gray, corners, ids, (0, 255, 0))
aruco.drawAxis(gray, mtx, dist, rvec, tvec, 10)
print(tvec[0][0])
distance = round( np.linalg.norm(tvec[0][0]), 2)
cv2.putText(gray, str( distance ) + " cm", (10, 400), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 255))
 
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
 
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
</pre>
688
Bearbeitungen