Calibrate camera with checker board

In [1]:
import cv2
cv2.startWindowThread() # needed to use highgui from opencv
Out[1]:
0

capture calibration images (total number=num_capture_images)

In [2]:
import numpy as np
import cv2
#cv2.startWindowThread() # needed to use highgui from opencv
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)

num_capture_images = 35
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].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.


cap = cv2.VideoCapture(0)
count = 0

while(True):
    # Capture frame-by-frame
    ret, img = cap.read()
#    img = cv2.resize(img,(640,480))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (7,6),None)

    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img,str(count),(10,500), font, 10,(255,255,255),2,cv2.LINE_AA)
    cv2.imshow('img',img)
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)
        cv2.imshow('img',img)
        cv2.waitKey(500)
        count=count+1

    if count > num_capture_images:
        break
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

computer camera matrix

In [ ]:
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)

h,  w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))

Capture video stream and display it with radial distortion removed

In [5]:
import numpy as np
import cv2
#cv2.startWindowThread() # needed to use highgui from opencv
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, img = cap.read()

    # undistort
    dst = cv2.undistort(img, mtx, dist, None, newcameramtx)

    # crop the image
    x,y,w,h = roi # getOptimalNewCameraMatrix seems to have trouble getting the correct roi
    dst = dst[y:y+h, x:x+w]

    # Display the resulting frame
    cv2.imshow('frame',dst)
    cv2.setWindowProperty("frame", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Capture video and display it unprocessed

In [1]:
import cv2
import platform

cap=cv2.VideoCapture(0)

while (True):
    
    ret,frame=cap.read()
    
    cv2.namedWindow("camera",cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("camera",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    cv2.imshow('camera',frame)
    
    if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first
        break

        
cap.release()
cv2.destroyAllWindows()
In [ ]: