-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycharm_aruco_pose
445 lines (349 loc) · 14.4 KB
/
pycharm_aruco_pose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
installing pycharm
install pycharm tar.gz on Standalone installation for linux
pycharm-community-2021.1.3.tar.gz
follow instructions from cd Downloads
https://itsfoss.com/install-pycharm-ubuntu/
after pycharm installed
file > New Project > choose location file > create
file > settings > Project:(project name) > python interpreter > choose "+" > opencv-contrib-python
STEP 1 (save_snapshots.py)
"""
Saves a series of snapshots with the current camera as snapshot_<width>_<height>_<nnn>.jpg
Arguments:
--f <output folder> default: current folder
--n <file name> default: snapshot
--w <width px> default: none
--h <height px> default: none
Buttons:
q - quit
space bar - save the snapshot
"""
import cv2
# import time
# import sys
import argparse
import os
__author__ = "Tiziano Fiorenzani"
__date__ = "01/06/2018"
def save_snaps(width=0, height=0, name="snapshot", folder=".", raspi=False):
if raspi:
os.system('sudo modprobe bcm2835-v4l2')
cap = cv2.VideoCapture(0)
if width > 0 and height > 0:
print("Setting the custom Width and Height")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
try:
if not os.path.exists(folder):
os.makedirs(folder)
# ----------- CREATE THE FOLDER -----------------
folder = os.path.dirname(folder)
try:
os.stat(folder)
except:
os.mkdir(folder)
except:
pass
nSnap = 0
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fileName = "%s/%s_%d_%d_" % (folder, name, w, h)
while True:
ret, frame = cap.read()
cv2.imshow('camera', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
if key == ord(' '):
print("Saving image ", nSnap)
cv2.imwrite("%s%d.jpg" % (fileName, nSnap), frame)
nSnap += 1
cap.release()
cv2.destroyAllWindows()
def main():
# ---- DEFAULT VALUES ---
SAVE_FOLDER = "."
FILE_NAME = "snapshot"
FRAME_WIDTH = 0
FRAME_HEIGHT = 0
# ----------- PARSE THE INPUTS -----------------
parser = argparse.ArgumentParser(
description="Saves snapshot from the camera. \n q to quit \n spacebar to save the snapshot")
parser.add_argument("--folder", default=SAVE_FOLDER, help="Path to the save folder (default: current)")
parser.add_argument("--name", default=FILE_NAME, help="Picture file name (default: snapshot)")
parser.add_argument("--dwidth", default=FRAME_WIDTH, type=int, help="<width> px (default the camera output)")
parser.add_argument("--dheight", default=FRAME_HEIGHT, type=int, help="<height> px (default the camera output)")
parser.add_argument("--raspi", default=False, type=bool, help="<bool> True if using a raspberry Pi")
args = parser.parse_args()
SAVE_FOLDER = args.folder
FILE_NAME = args.name
FRAME_WIDTH = args.dwidth
FRAME_HEIGHT = args.dheight
save_snaps(width=args.dwidth, height=args.dheight, name=args.name, folder=args.folder, raspi=args.raspi)
print("Files saved")
if __name__ == "__main__":
main()
STEP 2
open terminal 1
cd PycharmProject/(project name)
python save_snapshots.py --dwidth 640 --dheight 480 --raspi True
capture photos more the 20 photos with different angle
STEP 3
terminal 1
mkdir camera_01
move everyphotos from snap shots result to camera_01 folder
STEP 4(cameracalib.py)
#!/usr/bin/env python
"""
From https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html#calibration
Calling:
cameracalib.py <folder> <image type> <num rows> <num cols> <cell dimension>
like cameracalib.py folder_name png
--h for help
"""
__author__ = "Tiziano Fiorenzani"
__date__ = "01/06/2018"
import numpy as np
import cv2
import glob
import sys
#import argparse
#---------------------- SET THE PARAMETERS
nRows = 9
nCols = 6
dimension = 25 #- mm
workingFolder = "./camera_01"
imageType = 'jpg'
#------------------------------------------
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, dimension, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((nRows*nCols,3), np.float32)
objp[:,:2] = np.mgrid[0:nCols,0:nRows].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
if len(sys.argv) < 6:
print("\n Not enough inputs are provided. Using the default values.\n\n"
" type -h for help")
else:
workingFolder = sys.argv[1]
imageType = sys.argv[2]
nRows = int(sys.argv[3])
nCols = int(sys.argv[4])
dimension = float(sys.argv[5])
if '-h' in sys.argv or '--h' in sys.argv:
print("\n IMAGE CALIBRATION GIVEN A SET OF IMAGES")
print(" call: python cameracalib.py <folder> <image type> <num rows (9)> <num cols (6)> <cell dimension (25)>")
print("\n The script will look for every image in the provided folder and will show the pattern found."
" User can skip the image pressing ESC or accepting the image with RETURN. "
" At the end the end the following files are created:"
" - cameraDistortion.txt"
" - cameraMatrix.txt \n\n")
sys.exit()
# Find the images files
filename = workingFolder + "/*." + imageType
images = glob.glob(filename)
print(len(images))
if len(images) < 9:
print("Not enough images were found: at least 9 shall be provided!!!")
sys.exit()
else:
nPatternFound = 0
imgNotGood = images[1]
for fname in images:
if 'calibresult' in fname: continue
#-- Read the file and convert in greyscale
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print("Reading image ", fname)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (nCols,nRows),None)
# If found, add object points, image points (after refining them)
if ret == True:
print("Pattern found! Press ESC to skip or ENTER to accept")
#--- Sometimes, Harris cornes fails with crappy pictures, so
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# Draw and display the corners
cv2.drawChessboardCorners(img, (nCols,nRows), corners2,ret)
cv2.imshow('img',img)
# cv2.waitKey(0)
k = cv2.waitKey(0) & 0xFF
if k == 27: #-- ESC Button
print("Image Skipped")
imgNotGood = fname
continue
print("Image accepted")
nPatternFound += 1
objpoints.append(objp)
imgpoints.append(corners2)
# cv2.waitKey(0)
else:
imgNotGood = fname
cv2.destroyAllWindows()
if (nPatternFound > 1):
print("Found %d good images" % (nPatternFound))
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
# Undistort an image
img = cv2.imread(imgNotGood)
h, w = img.shape[:2]
print("Image to undistort: ", imgNotGood)
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
# undistort
mapx,mapy = cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),5)
dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
# crop the image
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
print("ROI: ", x, y, w, h)
cv2.imwrite(workingFolder + "/calibresult.png",dst)
print("Calibrated picture saved as calibresult.png")
print("Calibration Matrix: ")
print(mtx)
print("Disortion: ", dist)
#--------- Save result
filename = workingFolder + "/cameraMatrix.txt"
np.savetxt(filename, mtx, delimiter=',')
filename = workingFolder + "/cameraDistortion.txt"
np.savetxt(filename, dist, delimiter=',')
mean_error = 0
for i in xrange(len(objpoints)):
imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
mean_error += error
print("total error: ", mean_error/len(objpoints))
else:
print("In order to calibrate you need at least 9 good pictures... try again")
STEP 5
terminal 1
python cameracalib.py camera_01 jpg 9 6 23
STEP 6 (aruco_pose_estimation.py)
"""
This demo calculates multiple things for different scenarios.
Here are the defined reference frames:
TAG:
A y
|
|
|tag center
O---------> x
CAMERA:
X--------> x
| frame center
|
|
V y
F1: Flipped (180 deg) tag frame around x axis
F2: Flipped (180 deg) camera frame around x axis
The attitude of a generic frame 2 respect to a frame 1 can obtained by calculating euler(R_21.T)
We are going to obtain the following quantities:
> from aruco library we obtain tvec and Rct, position of the tag in camera frame and attitude of the tag
> position of the Camera in Tag axis: -R_ct.T*tvec
> Transformation of the camera, respect to f1 (the tag flipped frame): R_cf1 = R_ct*R_tf1 = R_cf*R_f
> Transformation of the tag, respect to f2 (the camera flipped frame): R_tf2 = Rtc*R_cf2 = R_tc*R_f
> R_tf1 = R_cf2 an symmetric = R_f
"""
import numpy as np
import cv2
import cv2.aruco as aruco
import sys, time, math
# --- Define Tag
id_to_find = 0
marker_size = 10 # - [cm]
# ------------------------------------------------------------------------------
# ------- ROTATIONS https://www.learnopencv.com/rotation-matrix-to-euler-angles/
# ------------------------------------------------------------------------------
# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R):
Rt = np.transpose(R)
shouldBeIdentity = np.dot(Rt, R)
I = np.identity(3, dtype=R.dtype)
n = np.linalg.norm(I - shouldBeIdentity)
return n < 1e-6
# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
def rotationMatrixToEulerAngles(R):
assert (isRotationMatrix(R))
sy = math.sqrt(R[0, 0] * R[0, 0] + R[1, 0] * R[1, 0])
singular = sy < 1e-6
if not singular:
x = math.atan2(R[2, 1], R[2, 2])
y = math.atan2(-R[2, 0], sy)
z = math.atan2(R[1, 0], R[0, 0])
else:
x = math.atan2(-R[1, 2], R[1, 1])
y = math.atan2(-R[2, 0], sy)
z = 0
return np.array([x, y, z])
# --- Get the camera calibration path
calib_path = "camera_01/"
camera_matrix = np.loadtxt(calib_path + 'cameraMatrix.txt', delimiter=',')
camera_distortion = np.loadtxt(calib_path + 'cameraDistortion.txt', delimiter=',')
# --- 180 deg rotation matrix around the x axis
R_flip = np.zeros((3, 3), dtype=np.float32)
R_flip[0, 0] = 1.0
R_flip[1, 1] = -1.0
R_flip[2, 2] = -1.0
# --- Define the aruco dictionary
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_6X6_250)
parameters = aruco.DetectorParameters_create()
# --- Capture the videocamera (this may also be a video or a picture)
cap = cv2.VideoCapture(0)
# -- Set the camera size as the one it was calibrated with
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# -- Font for the text in the image
font = cv2.FONT_HERSHEY_PLAIN
while True:
# -- Read the camera frame
ret, frame = cap.read()
# -- Convert in gray scale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # -- remember, OpenCV stores color images in Blue, Green, Red
# -- Find all the aruco markers in the image
corners, ids, rejected = aruco.detectMarkers(image=gray, dictionary=aruco_dict, parameters=parameters,
cameraMatrix=camera_matrix, distCoeff=camera_distortion)
if ids is not None and ids[0] == id_to_find:
# -- ret = [rvec, tvec, ?]
# -- array of rotation and position of each marker in camera frame
# -- rvec = [[rvec_1], [rvec_2], ...] attitude of the marker respect to camera frame
# -- tvec = [[tvec_1], [tvec_2], ...] position of the marker in camera frame
ret = aruco.estimatePoseSingleMarkers(corners, marker_size, camera_matrix, camera_distortion)
# -- Unpack the output, get only the first
rvec, tvec = ret[0][0, 0, :], ret[1][0, 0, :]
# -- Draw the detected marker and put a reference frame over it
aruco.drawDetectedMarkers(frame, corners)
aruco.drawAxis(frame, camera_matrix, camera_distortion, rvec, tvec, 10)
# -- Print the tag position in camera frame
str_position = "MARKER Position x=%4.0f y=%4.0f z=%4.0f" % (tvec[0], tvec[1], tvec[2])
cv2.putText(frame, str_position, (0, 100), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# -- Obtain the rotation matrix tag->camera
R_ct = np.matrix(cv2.Rodrigues(rvec)[0])
R_tc = R_ct.T
# -- Get the attitude in terms of euler 321 (Needs to be flipped first)
roll_marker, pitch_marker, yaw_marker = rotationMatrixToEulerAngles(R_flip * R_tc)
# -- Print the marker's attitude respect to camera frame
str_attitude = "MARKER Attitude r=%4.0f p=%4.0f y=%4.0f" % (
math.degrees(roll_marker), math.degrees(pitch_marker),
math.degrees(yaw_marker))
cv2.putText(frame, str_attitude, (0, 150), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# -- Now get Position and attitude f the camera respect to the marker
pos_camera = -R_tc * np.matrix(tvec).T
str_position = "CAMERA Position x=%4.0f y=%4.0f z=%4.0f" % (pos_camera[0], pos_camera[1], pos_camera[2])
cv2.putText(frame, str_position, (0, 200), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# -- Get the attitude of the camera respect to the frame
roll_camera, pitch_camera, yaw_camera = rotationMatrixToEulerAngles(R_flip * R_tc)
str_attitude = "CAMERA Attitude r=%4.0f p=%4.0f y=%4.0f" % (
math.degrees(roll_camera), math.degrees(pitch_camera),
math.degrees(yaw_camera))
cv2.putText(frame, str_attitude, (0, 250), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# --- Display the frame
cv2.imshow('frame', frame)
# --- use 'q' to quit
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
STEP 7
RUN aruco_pose_estimation.py