Skip to content

Commit 9356c1d

Browse files
author
Joseph Schornak
committed
Initial Commit
0 parents  commit 9356c1d

8 files changed

+1257
-0
lines changed

calibration.npz

2.09 KB
Binary file not shown.

needle_localization.py

Lines changed: 752 additions & 0 deletions
Large diffs are not rendered by default.

output_side.avi

765 KB
Binary file not shown.

output_top.avi

733 KB
Binary file not shown.

record_stereo.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import numpy as np
2+
import cv2
3+
import time
4+
import subprocess
5+
6+
def main():
7+
max_count = 18
8+
delay = 5
9+
count = 0
10+
start = time.time()
11+
12+
SIDE = True
13+
TOP = True
14+
15+
boardSize = (9, 7)
16+
17+
IMG_PATH = '/home/jgschornak/NeedleGuidance/images_converging_cams/'
18+
19+
bashCommand = 'v4l2-ctl -d /dev/video1 -c focus_auto=0'
20+
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
21+
cv2.waitKey(100)
22+
bashCommand = 'v4l2-ctl -d /dev/video1 -c focus_absolute=20'
23+
process1 = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
24+
cv2.waitKey(100)
25+
bashCommand = 'v4l2-ctl -d /dev/video2 -c focus_auto=0'
26+
process2 = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
27+
cv2.waitKey(100)
28+
bashCommand = 'v4l2-ctl -d /dev/video2 -c focus_absolute=30'
29+
process3 = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
30+
cv2.waitKey(100)
31+
32+
top = cv2.VideoCapture(1)
33+
side = cv2.VideoCapture(2)
34+
35+
ret, frame = top.read()
36+
height, width, channels = frame.shape
37+
38+
calibration = np.load('calibration.npz')
39+
40+
# P1 = calibration['P1']
41+
# P2 = calibration['P2']
42+
43+
top_camera_matrix = calibration['CameraMatrix1']
44+
top_dist_coefs = calibration['DistCoeffs1']
45+
46+
side_camera_matrix = calibration['CameraMatrix2']
47+
side_dist_coefs = calibration['DistCoeffs2']
48+
49+
tvec_top = None
50+
tvec_side = None
51+
rvec_top = None
52+
rvec_side = None
53+
rmat_top = None
54+
rmat_side = None
55+
56+
top_homogeneous = np.eye(4)
57+
side_homogeneous = np.eye(4)
58+
59+
squareSize = 5.5
60+
pattern_points = np.zeros((np.prod(boardSize), 3), np.float32)
61+
pattern_points[:, :2] = np.indices(boardSize).T.reshape(-1, 2)
62+
pattern_points *= squareSize
63+
64+
start_vec = np.array([0,0,0,1]).reshape(-1,1)
65+
66+
while True:
67+
if TOP:
68+
ret, top_frame = top.read()
69+
top_frame = cv2.cvtColor(top_frame, cv2.COLOR_BGR2GRAY)
70+
found_top, corners_top = cv2.findChessboardCorners(top_frame, boardSize)
71+
vis_top = cv2.cvtColor(top_frame, cv2.COLOR_GRAY2BGR)
72+
if found_top:
73+
cv2.drawChessboardCorners(vis_top, boardSize, corners_top, found_top)
74+
ret, rvec_top, tvec_top = cv2.solvePnP(pattern_points, corners_top, top_camera_matrix, top_dist_coefs)
75+
rmat_top, jac = cv2.Rodrigues(rvec_top)
76+
77+
top_homogeneous[:3,:3] = rmat_top
78+
top_homogeneous[:3,3:4] = tvec_top.reshape(3,-1)
79+
cv2.imshow("top", vis_top)
80+
print('t_top', tvec_top)
81+
print('homo_top', top_homogeneous)
82+
83+
if SIDE:
84+
ret, side_frame = side.read()
85+
side_frame = cv2.cvtColor(side_frame, cv2.COLOR_BGR2GRAY)
86+
found_side, corners_side = cv2.findChessboardCorners(side_frame, boardSize)
87+
vis_side = cv2.cvtColor(side_frame, cv2.COLOR_GRAY2BGR)
88+
if found_side:
89+
cv2.drawChessboardCorners(vis_side, boardSize, corners_side, found_side)
90+
ret, rvec_side, tvec_side = cv2.solvePnP(pattern_points, corners_side, side_camera_matrix, side_dist_coefs)
91+
rmat_side, jac = cv2.Rodrigues(rvec_side)
92+
93+
side_homogeneous[:3,:3] = rmat_side
94+
side_homogeneous[:3,3:4] = tvec_side.reshape(3,-1)
95+
cv2.imshow("side", vis_side)
96+
print('t_side', tvec_side)
97+
print('homo_side', side_homogeneous)
98+
99+
100+
101+
102+
103+
if TOP and SIDE:
104+
product = np.dot(top_homogeneous,start_vec)
105+
# product = np.transpose(start_vec)*top_homogeneous
106+
# print(start_vec)
107+
# print('translation', product)
108+
109+
cumulative = np.dot(side_homogeneous, np.linalg.inv(top_homogeneous))
110+
# cum_translation = np.dot(cumulative, start_vec)
111+
# print('cumulative', cumulative)
112+
# print('cum trans', cum_translation/cum_translation[3,:])
113+
114+
R = cumulative[:3,:3]
115+
T = cumulative[:3,3:4]
116+
117+
# F = cv2.findFundamentalMatrix(corners_top, corners_side)
118+
119+
120+
if cv2.waitKey(1) & 0xFF == ord('t'):
121+
if TOP:
122+
filename1 = IMG_PATH + 'top' + str(count) + '.jpg'
123+
cv2.imwrite(filename1, top_frame)
124+
print('Saved still as: ' + filename1)
125+
126+
if SIDE:
127+
filename2 = IMG_PATH + 'side' + str(count) + '.jpg'
128+
cv2.imwrite(filename2, side_frame)
129+
print('Saved still as: ' + filename2)
130+
count = count + 1
131+
# np.savez_compressed('camera_transform.npz',R=R, T=T)
132+
# print("Saved transform!")
133+
134+
if cv2.waitKey(1) & 0xFF == ord('q'):
135+
break
136+
top.release()
137+
side.release()
138+
139+
140+
if __name__ == '__main__':
141+
main()

side_calibration.npz

624 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)