Skip to content

Instantly share code, notes, and snippets.

@cipher982
Created November 8, 2017 20:30
Show Gist options
  • Select an option

  • Save cipher982/0ca570ca74de176d585aba8301742c20 to your computer and use it in GitHub Desktop.

Select an option

Save cipher982/0ca570ca74de176d585aba8301742c20 to your computer and use it in GitHub Desktop.
camera calibration in opencv
def toCalibrate(img,nx,ny):
objpoints = []
imgpoints = []
objp = np.zeros((nx*ny, 3), np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2) # x,y coordinates
# Find chessboard corners
ret, corners= cv2.findChessboardCorners(img, (nx,ny), None)
# If found, draw corners
if ret == True:
# Draw and display the corners
cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
imgpoints.append(corners)
objpoints.append(objp)
plt.suptitle('Before Distortion', fontsize=14, fontweight='bold')
plt.imshow(img)
plt.show()
global mtx
global dist
global rvecs
global tvecs
global undist
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, toGray(img).shape[::-1], None, None)
undist = cv2.undistort(img, mtx, dist, None, mtx)
plt.suptitle('After Distortion', fontsize=14, fontweight='bold')
plt.imshow(undist)
plt.show()
if ret == False:
print('Did not find any corners')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment