Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.pyc
.idea/
33 changes: 17 additions & 16 deletions calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import glob
import matplotlib.pyplot as plt
import pickle

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8, 0:6].T.reshape(-1,2)
objp = np.zeros((6 * 8, 3), np.float32)
objp[:, :2] = np.mgrid[0:8, 0:6].T.reshape(-1, 2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.

# Make a list of calibration images
images = glob.glob('calibration_wide/GO*.jpg')
Expand All @@ -20,16 +21,16 @@
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (8,6), None)
ret, corners = cv2.findChessboardCorners(gray, (8, 6), None)
# If found, add object points, image points
if ret == True:
if ret:
objpoints.append(objp)
imgpoints.append(corners)

# Draw and display the corners
cv2.drawChessboardCorners(img, (8,6), corners, ret)
#write_name = 'corners_found'+str(idx)+'.jpg'
#cv2.imwrite(write_name, img)
cv2.drawChessboardCorners(img, (8, 6), corners, ret)
# write_name = 'corners_found'+str(idx)+'.jpg'
# cv2.imwrite(write_name, img)
cv2.imshow('img', img)
cv2.waitKey(500)

Expand All @@ -40,21 +41,21 @@
img_size = (img.shape[1], img.shape[0])

# Do camera calibration given object points and image points
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size,None,None)

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)

dst = cv2.undistort(img, mtx, dist, None, mtx)
cv2.imwrite('calibration_wide/test_undist.jpg',dst)
cv2.imwrite('calibration_wide/output/test_undist.jpg', dst)

# Save the camera calibration result for later use (we won't worry about rvecs / tvecs)
dist_pickle = {}
dist_pickle = dict()
dist_pickle["mtx"] = mtx
dist_pickle["dist"] = dist
pickle.dump( dist_pickle, open( "calibration_wide/wide_dist_pickle.p", "wb" ) )
#dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
pickle.dump(dist_pickle, open("calibration_wide/output/wide_dist_pickle.p", "wb"))
# dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
# Visualize undistortion
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
ax1.imshow(img)
ax1.set_title('Original Image', fontsize=30)
ax2.imshow(dst)
ax2.set_title('Undistorted Image', fontsize=30)
plt.show()
3 changes: 3 additions & 0 deletions calibration_wide/output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# folder and subfolders are ignored
*
!.gitignore
4 changes: 2 additions & 2 deletions camera_calibration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@
"\n",
"\n",
"dst = cv2.undistort(img, mtx, dist, None, mtx)\n",
"cv2.imwrite('calibration_wide/test_undist.jpg',dst)\n",
"cv2.imwrite('calibration_wide/output/test_undist.jpg',dst)\n",
"\n",
"# Save the camera calibration result for later use (we won't worry about rvecs / tvecs)\n",
"dist_pickle = {}\n",
"dist_pickle[\"mtx\"] = mtx\n",
"dist_pickle[\"dist\"] = dist\n",
"pickle.dump( dist_pickle, open( \"calibration_wide/wide_dist_pickle.p\", \"wb\" ) )\n",
"pickle.dump( dist_pickle, open( \"calibration_wide/output/wide_dist_pickle.p\", \"wb\" ) )\n",
"#dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)\n",
"# Visualize undistortion\n",
"f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))\n",
Expand Down