-
-
Save phsamuel/5b4bbe479dc70d71bc6e17e5c379c1fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Calibrate camera with checker board" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Also see https://youtu.be/TXdOZwAE9lc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import cv2\n", | |
"cv2.startWindowThread() # needed to use highgui from opencv" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## capture calibration images (total number=num_capture_images)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import cv2\n", | |
"#cv2.startWindowThread() # needed to use highgui from opencv\n", | |
"cv2.namedWindow('frame',cv2.WINDOW_NORMAL)\n", | |
"\n", | |
"num_capture_images = 35\n", | |
"# termination criteria\n", | |
"criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", | |
"\n", | |
"# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)\n", | |
"objp = np.zeros((6*7,3), np.float32)\n", | |
"objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)\n", | |
"\n", | |
"# Arrays to store object points and image points from all the images.\n", | |
"objpoints = [] # 3d point in real world space\n", | |
"imgpoints = [] # 2d points in image plane.\n", | |
"\n", | |
"\n", | |
"cap = cv2.VideoCapture(0)\n", | |
"count = 0\n", | |
"\n", | |
"while(True):\n", | |
" # Capture frame-by-frame\n", | |
" ret, img = cap.read()\n", | |
"# img = cv2.resize(img,(640,480))\n", | |
" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n", | |
" \n", | |
" # Find the chess board corners\n", | |
" ret, corners = cv2.findChessboardCorners(gray, (7,6),None)\n", | |
"\n", | |
" font = cv2.FONT_HERSHEY_SIMPLEX\n", | |
" cv2.putText(img,str(count),(10,500), font, 10,(255,255,255),2,cv2.LINE_AA)\n", | |
" cv2.imshow('img',img)\n", | |
" # If found, add object points, image points (after refining them)\n", | |
" if ret == True:\n", | |
" objpoints.append(objp)\n", | |
"\n", | |
" corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)\n", | |
" imgpoints.append(corners2)\n", | |
"\n", | |
" # Draw and display the corners\n", | |
" img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)\n", | |
" cv2.imshow('img',img)\n", | |
" cv2.waitKey(500)\n", | |
" count=count+1\n", | |
"\n", | |
" if count > num_capture_images:\n", | |
" break\n", | |
" if cv2.waitKey(50) & 0xFF == ord('q'):\n", | |
" break\n", | |
"\n", | |
"# When everything done, release the capture\n", | |
"cap.release()\n", | |
"cv2.destroyAllWindows()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## computer camera matrix" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)\n", | |
"\n", | |
"h, w = img.shape[:2]\n", | |
"newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Capture video stream and display it with radial distortion removed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import cv2\n", | |
"#cv2.startWindowThread() # needed to use highgui from opencv\n", | |
"cv2.namedWindow('frame',cv2.WINDOW_NORMAL)\n", | |
"\n", | |
"cap = cv2.VideoCapture(0)\n", | |
"\n", | |
"while(True):\n", | |
" # Capture frame-by-frame\n", | |
" ret, img = cap.read()\n", | |
"\n", | |
" # undistort\n", | |
" dst = cv2.undistort(img, mtx, dist, None, newcameramtx)\n", | |
"\n", | |
" # crop the image\n", | |
" x,y,w,h = roi # getOptimalNewCameraMatrix seems to have trouble getting the correct roi\n", | |
" dst = dst[y:y+h, x:x+w]\n", | |
"\n", | |
" # Display the resulting frame\n", | |
" cv2.imshow('frame',dst)\n", | |
" cv2.setWindowProperty(\"frame\", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n", | |
" if cv2.waitKey(1) & 0xFF == ord('q'):\n", | |
" break\n", | |
"\n", | |
"# When everything done, release the capture\n", | |
"cap.release()\n", | |
"cv2.destroyAllWindows()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Capture video and display it unprocessed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import platform\n", | |
"\n", | |
"cap=cv2.VideoCapture(0)\n", | |
"\n", | |
"while (True):\n", | |
" \n", | |
" ret,frame=cap.read()\n", | |
" \n", | |
" cv2.namedWindow(\"camera\",cv2.WND_PROP_FULLSCREEN)\n", | |
" cv2.setWindowProperty(\"camera\",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n", | |
" cv2.imshow('camera',frame)\n", | |
" \n", | |
" if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first\n", | |
" break\n", | |
"\n", | |
" \n", | |
"cap.release()\n", | |
"cv2.destroyAllWindows()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.15rc1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment