Skip to content

Instantly share code, notes, and snippets.

@giraffesyo
Created November 26, 2017 23:50
Show Gist options
  • Save giraffesyo/8d1854b42408f0d433fa35e943c0c6d6 to your computer and use it in GitHub Desktop.
Save giraffesyo/8d1854b42408f0d433fa35e943c0c6d6 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 37 valid images for calibration\n",
"DIM=(1920, 1080)\n",
"K=np.array([[543.0104867188367, 0.0, 939.0225470258382], [0.0, 542.5485795027022, 537.924099639527], [0.0, 0.0, 1.0]])\n",
"D=np.array([[-0.03526355863702719], [-0.016844894500392806], [0.008612142278291523], [-0.0022750917496576397]])\n"
]
}
],
"source": [
"import cv2\n",
"assert cv2.__version__[0] == '3', 'The fisheye module requires opencv version >= 3.0.0'\n",
"import numpy as np\n",
"import os\n",
"import glob\n",
"CHECKERBOARD = (6,9)\n",
"subpix_criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)\n",
"calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEW\n",
"objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)\n",
"objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)\n",
"_img_shape = None\n",
"objpoints = [] # 3d point in real world space\n",
"imgpoints = [] # 2d points in image plane.\n",
"images = glob.glob('/Users/michaelmcquade/Desktop/top/*.jpg')\n",
"for fname in images:\n",
" img = cv2.imread(fname)\n",
" if _img_shape == None:\n",
" _img_shape = img.shape[:2]\n",
" else:\n",
" assert _img_shape == img.shape[:2], \"All images must share the same size.\"\n",
" gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
" # Find the chess board corners\n",
" ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)\n",
" # If found, add object points, image points (after refining them)\n",
" if ret == True:\n",
" objpoints.append(objp)\n",
" cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria)\n",
" imgpoints.append(corners)\n",
"N_OK = len(objpoints)\n",
"K = np.zeros((3, 3))\n",
"D = np.zeros((4, 1))\n",
"rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]\n",
"tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]\n",
"rms, _, _, _, _ = \\\n",
" cv2.fisheye.calibrate(\n",
" objpoints,\n",
" imgpoints,\n",
" gray.shape[::-1],\n",
" K,\n",
" D,\n",
" rvecs,\n",
" tvecs,\n",
" calibration_flags,\n",
" (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)\n",
" )\n",
"print(\"Found \" + str(N_OK) + \" valid images for calibration\")\n",
"print(\"DIM=\" + str(_img_shape[::-1]))\n",
"print(\"K=np.array(\" + str(K.tolist()) + \")\")\n",
"print(\"D=np.array(\" + str(D.tolist()) + \")\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment