diff --git a/.ipynb_checkpoints/CameraCalibration-checkpoint.ipynb b/.ipynb_checkpoints/CameraCalibration-checkpoint.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..9fa3bbe9119284a0bb75236a837087385bbee808 --- /dev/null +++ b/.ipynb_checkpoints/CameraCalibration-checkpoint.ipynb @@ -0,0 +1,728 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 35, + "id": "2a7d76f9-118d-495f-bce1-8d502caa3f4d", + "metadata": {}, + "outputs": [ + { + "ename": "error", + "evalue": "OpenCV(4.5.5) /Users/runner/miniforge3/conda-bld/libopencv_1648505779078/work/modules/calib3d/src/calibration.cpp:3694: error: (-215:Assertion failed) nimages > 0 in function 'calibrateCameraRO'\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31merror\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-35-f93f09ea17d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0mdetected\u001b[0m \u001b[0mcorners\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mimgpoints\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 67\u001b[0m \"\"\"\n\u001b[0;32m---> 68\u001b[0;31m \u001b[0mret\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmtx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrvecs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtvecs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcalibrateCamera\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobjpoints\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mimgpoints\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 69\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Camera matrix : \\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31merror\u001b[0m: OpenCV(4.5.5) /Users/runner/miniforge3/conda-bld/libopencv_1648505779078/work/modules/calib3d/src/calibration.cpp:3694: error: (-215:Assertion failed) nimages > 0 in function 'calibrateCameraRO'\n" + ] + } + ], + "source": [ + "import cv2\n", + "import numpy as np\n", + "import os\n", + "import glob\n", + "\n", + "# Defining the dimensions of checkerboard\n", + "CHECKERBOARD = (6,9)\n", + "criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", + "\n", + "# Creating vector to store vectors of 3D points for each checkerboard image\n", + "objpoints = []\n", + "# Creating vector to store vectors of 2D points for each checkerboard image\n", + "imgpoints = [] \n", + "\n", + "\n", + "# Defining the world coordinates for 3D points\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", + "prev_img_shape = None\n", + "\n", + "# Extracting path of individual image stored in a given directory\n", + "images = glob.glob('./images/*.jpg')\n", + "a = 1\n", + "for fname in images:\n", + " print(\"Load images\")\n", + " img = cv2.imread(fname)\n", + " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", + " # Find the chess board corners\n", + " # If desired number of corners are found in the image then ret = true\n", + " print(\"Finding chessboard corners\")\n", + " ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)\n", + " \n", + " print(\"corners \\n\")\n", + " print(corners)\n", + " print(\"ret \\n\")\n", + " print(ret)\n", + " \n", + " \"\"\"\n", + " If desired number of corner are detected,\n", + " we refine the pixel coordinates and display \n", + " them on the images of checker board\n", + " \"\"\"\n", + " if ret == True:\n", + " print(\"Corners detected\")\n", + " objpoints.append(objp)\n", + " # refining pixel coordinates for given 2d points.\n", + " corners2 = cv2.cornerSubPix(gray, corners, (11,11),(-1,-1), criteria)\n", + " \n", + " imgpoints.append(corners2)\n", + "\n", + " # Draw and display the corners\n", + " img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)\n", + " \n", + " print(\"Show images\")\n", + " # cv2.imshow('img',img)\n", + " nome='foto_processada' + str(a) + '.jpg'\n", + " cv2.imwrite(nome, img)\n", + " a+=1\n", + "\n", + "h,w = img.shape[:2]\n", + "\n", + "\"\"\"\n", + "Performing camera calibration by \n", + "passing the value of known 3D points (objpoints)\n", + "and corresponding pixel coordinates of the \n", + "detected corners (imgpoints)\n", + "\"\"\"\n", + "ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)\n", + "\n", + "print(\"Camera matrix : \\n\")\n", + "print(mtx)\n", + "print(\"dist : \\n\")\n", + "print(dist)\n", + "print(\"rvecs : \\n\")\n", + "print(rvecs)\n", + "print(\"tvecs : \\n\")\n", + "print(tvecs)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "ff6380f6-b96f-469c-bd7f-17fbb3721389", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[142.64168 254.9432 ]]\n", + "\n", + " [[146.75854 225.61407 ]]\n", + "\n", + " [[150.54066 196.51573 ]]\n", + "\n", + " [[154.04866 167.27895 ]]\n", + "\n", + " [[157.64587 138.57889 ]]\n", + "\n", + " [[161.52353 110.188255]]\n", + "\n", + " [[173.47812 258.40778 ]]\n", + "\n", + " [[176.93672 229.4982 ]]\n", + "\n", + " [[180.49413 200.70557 ]]\n", + "\n", + " [[184.05624 171.98401 ]]\n", + "\n", + " [[187.66382 144.17943 ]]\n", + "\n", + " [[190.79807 116.24408 ]]\n", + "\n", + " [[203.03679 261.4418 ]]\n", + "\n", + " [[206.62283 232.9877 ]]\n", + "\n", + " [[209.51048 204.94817 ]]\n", + "\n", + " [[212.91487 176.95428 ]]\n", + "\n", + " [[216.23514 149.11143 ]]\n", + "\n", + " [[219.46265 121.66086 ]]\n", + "\n", + " [[231.7584 264.68643 ]]\n", + "\n", + " [[234.71873 236.5048 ]]\n", + "\n", + " [[237.93755 209.0442 ]]\n", + "\n", + " [[241.19179 181.42677 ]]\n", + "\n", + " [[243.81526 154.10678 ]]\n", + "\n", + " [[246.94101 126.50662 ]]\n", + "\n", + " [[259.238 267.7193 ]]\n", + "\n", + " [[262.34668 240.02777 ]]\n", + "\n", + " [[265.4873 213.02408 ]]\n", + "\n", + " [[268.29196 185.80069 ]]\n", + "\n", + " [[271.08856 158.79974 ]]\n", + "\n", + " [[274.03 131.71118 ]]\n", + "\n", + " [[285.894 270.6857 ]]\n", + "\n", + " [[288.91974 243.85074 ]]\n", + "\n", + " [[291.67294 216.57468 ]]\n", + "\n", + " [[294.623 189.9902 ]]\n", + "\n", + " [[297.21625 163.05467 ]]\n", + "\n", + " [[300.24863 136.88556 ]]\n", + "\n", + " [[312.1065 273.9727 ]]\n", + "\n", + " [[314.64572 246.71307 ]]\n", + "\n", + " [[317.157 220.33134 ]]\n", + "\n", + " [[320.10812 193.81598 ]]\n", + "\n", + " [[322.75974 167.55089 ]]\n", + "\n", + " [[325.6024 141.55737 ]]\n", + "\n", + " [[336.9503 276.28848 ]]\n", + "\n", + " [[339.49103 249.95747 ]]\n", + "\n", + " [[341.7496 224.4254 ]]\n", + "\n", + " [[344.58643 197.6241 ]]\n", + "\n", + " [[347.26367 172.06337 ]]\n", + "\n", + " [[350.02148 146.011 ]]\n", + "\n", + " [[361.31314 279.1676 ]]\n", + "\n", + " [[363.75967 252.91393 ]]\n", + "\n", + " [[365.80142 227.11314 ]]\n", + "\n", + " [[368.71347 201.5229 ]]\n", + "\n", + " [[371.02176 176.04767 ]]\n", + "\n", + " [[373.48065 150.7466 ]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[290.16937 306.88214]]\n", + "\n", + " [[290.53278 280.11667]]\n", + "\n", + " [[290.77576 252.93272]]\n", + "\n", + " [[291.0845 225.27896]]\n", + "\n", + " [[291.53607 197.0897 ]]\n", + "\n", + " [[292.41327 168.24815]]\n", + "\n", + " [[315.06952 311.0208 ]]\n", + "\n", + " [[315.6739 283.6647 ]]\n", + "\n", + " [[316.2393 256.1716 ]]\n", + "\n", + " [[316.59756 227.64337]]\n", + "\n", + " [[317.8294 198.79442]]\n", + "\n", + " [[318.80643 169.55096]]\n", + "\n", + " [[340.638 315.39575]]\n", + "\n", + " [[341.63934 287.54718]]\n", + "\n", + " [[342.54242 259.35623]]\n", + "\n", + " [[343.91376 230.37785]]\n", + "\n", + " [[345.06934 201.05199]]\n", + "\n", + " [[346.2913 171.26308]]\n", + "\n", + " [[367.9363 319.6347 ]]\n", + "\n", + " [[368.97598 291.21472]]\n", + "\n", + " [[370.40988 262.52164]]\n", + "\n", + " [[371.5862 233.02112]]\n", + "\n", + " [[373.30624 203.30626]]\n", + "\n", + " [[374.47787 172.69196]]\n", + "\n", + " [[396.0801 324.31546]]\n", + "\n", + " [[397.2893 295.3254 ]]\n", + "\n", + " [[398.77914 265.93915]]\n", + "\n", + " [[400.4216 236.04366]]\n", + "\n", + " [[402.11496 205.60799]]\n", + "\n", + " [[403.97397 174.59514]]\n", + "\n", + " [[425.58887 328.73993]]\n", + "\n", + " [[427.1569 299.32626]]\n", + "\n", + " [[428.80847 269.61078]]\n", + "\n", + " [[430.6196 238.81488]]\n", + "\n", + " [[432.5938 207.88199]]\n", + "\n", + " [[434.81232 176.30792]]\n", + "\n", + " [[456.30505 333.57922]]\n", + "\n", + " [[458.36752 303.69302]]\n", + "\n", + " [[460.31778 273.17523]]\n", + "\n", + " [[462.44522 242.11354]]\n", + "\n", + " [[464.63388 210.30637]]\n", + "\n", + " [[467.06512 178.04515]]\n", + "\n", + " [[488.32422 338.7988 ]]\n", + "\n", + " [[490.48627 308.26813]]\n", + "\n", + " [[492.9652 276.772 ]]\n", + "\n", + " [[495.6021 245.111 ]]\n", + "\n", + " [[498.3957 212.66432]]\n", + "\n", + " [[501.15192 179.73419]]\n", + "\n", + " [[521.8725 344.20172]]\n", + "\n", + " [[524.48047 312.59525]]\n", + "\n", + " [[527.2914 281.0416 ]]\n", + "\n", + " [[530.3484 248.3829 ]]\n", + "\n", + " [[533.45984 215.4207 ]]\n", + "\n", + " [[536.5843 181.58168]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[181.65076 240.43515 ]]\n", + "\n", + " [[182.98524 216.97383 ]]\n", + "\n", + " [[184.64604 192.23103 ]]\n", + "\n", + " [[186.13664 165.44173 ]]\n", + "\n", + " [[187.51831 136.71654 ]]\n", + "\n", + " [[188.62773 106.87892 ]]\n", + "\n", + " [[209.48434 240.83936 ]]\n", + "\n", + " [[211.59288 217.80495 ]]\n", + "\n", + " [[213.6713 193.07858 ]]\n", + "\n", + " [[215.5483 166.35489 ]]\n", + "\n", + " [[217.48494 138.31812 ]]\n", + "\n", + " [[219.43669 108.85271 ]]\n", + "\n", + " [[236.70854 241.51544 ]]\n", + "\n", + " [[239.46967 218.49294 ]]\n", + "\n", + " [[241.6721 194.00006 ]]\n", + "\n", + " [[244.1657 167.18895 ]]\n", + "\n", + " [[246.48727 139.78937 ]]\n", + "\n", + " [[249.17108 111.17387 ]]\n", + "\n", + " [[263.09882 241.98895 ]]\n", + "\n", + " [[266.31976 218.94453 ]]\n", + "\n", + " [[269.2836 194.60536 ]]\n", + "\n", + " [[272.11176 168.56544 ]]\n", + "\n", + " [[275.07178 141.52344 ]]\n", + "\n", + " [[278.2868 113.34714 ]]\n", + "\n", + " [[288.6269 242.41682 ]]\n", + "\n", + " [[292.33762 219.4731 ]]\n", + "\n", + " [[295.634 195.30553 ]]\n", + "\n", + " [[299.0187 169.72737 ]]\n", + "\n", + " [[302.50778 143.14719 ]]\n", + "\n", + " [[306.42252 115.552376]]\n", + "\n", + " [[313.5622 242.52242 ]]\n", + "\n", + " [[317.43845 219.64905 ]]\n", + "\n", + " [[321.32867 195.67903 ]]\n", + "\n", + " [[325.13644 170.68053 ]]\n", + "\n", + " [[329.4506 144.55687 ]]\n", + "\n", + " [[333.65683 117.407425]]\n", + "\n", + " [[337.9189 242.53233 ]]\n", + "\n", + " [[342.0708 219.9899 ]]\n", + "\n", + " [[346.30737 196.3446 ]]\n", + "\n", + " [[350.5993 171.67969 ]]\n", + "\n", + " [[355.18494 146.09834 ]]\n", + "\n", + " [[360.2282 119.398865]]\n", + "\n", + " [[361.50598 242.38771 ]]\n", + "\n", + " [[365.82083 220.18697 ]]\n", + "\n", + " [[370.56412 196.69379 ]]\n", + "\n", + " [[375.1744 172.38173 ]]\n", + "\n", + " [[380.44196 147.4049 ]]\n", + "\n", + " [[386.07758 121.229385]]\n", + "\n", + " [[384.24615 242.4664 ]]\n", + "\n", + " [[389.10178 220.35866 ]]\n", + "\n", + " [[394.30902 197.43806 ]]\n", + "\n", + " [[399.71222 173.47057 ]]\n", + "\n", + " [[405.3322 148.5755 ]]\n", + "\n", + " [[411.4092 122.746666]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[287.53036 361.7949 ]]\n", + "\n", + " [[282.58374 342.85358]]\n", + "\n", + " [[278.01462 325.26227]]\n", + "\n", + " [[272.65985 307.25195]]\n", + "\n", + " [[268.46243 289.60532]]\n", + "\n", + " [[263.20535 271.30942]]\n", + "\n", + " [[305.57693 356.4085 ]]\n", + "\n", + " [[300.56805 337.95624]]\n", + "\n", + " [[295.50082 319.71555]]\n", + "\n", + " [[290.40643 301.90472]]\n", + "\n", + " [[285.50308 284.46655]]\n", + "\n", + " [[281.19418 267.06412]]\n", + "\n", + " [[323.32886 351.0642 ]]\n", + "\n", + " [[318.34674 332.87128]]\n", + "\n", + " [[313.38998 314.55502]]\n", + "\n", + " [[307.87402 296.82272]]\n", + "\n", + " [[303.11688 279.44043]]\n", + "\n", + " [[298.32162 261.79645]]\n", + "\n", + " [[340.5354 345.56195]]\n", + "\n", + " [[335.7001 327.50388]]\n", + "\n", + " [[330.57962 310.177 ]]\n", + "\n", + " [[325.31915 292.1104 ]]\n", + "\n", + " [[320.627 274.28772]]\n", + "\n", + " [[315.36737 257.47568]]\n", + "\n", + " [[358.15817 340.35425]]\n", + "\n", + " [[352.91727 322.5884 ]]\n", + "\n", + " [[347.64328 304.45084]]\n", + "\n", + " [[342.7978 287.11328]]\n", + "\n", + " [[337.47632 269.54434]]\n", + "\n", + " [[332.27792 253.0178 ]]\n", + "\n", + " [[375.521 334.54907]]\n", + "\n", + " [[370.54572 317.49283]]\n", + "\n", + " [[365.13348 300.1963 ]]\n", + "\n", + " [[359.96573 282.50775]]\n", + "\n", + " [[354.63464 264.84613]]\n", + "\n", + " [[349.94232 247.95018]]\n", + "\n", + " [[392.5023 330.13177]]\n", + "\n", + " [[387.49005 312.34338]]\n", + "\n", + " [[381.98724 294.8061 ]]\n", + "\n", + " [[376.97012 277.55432]]\n", + "\n", + " [[371.30545 260.40204]]\n", + "\n", + " [[366.55707 243.28087]]\n", + "\n", + " [[409.58173 325.1956 ]]\n", + "\n", + " [[404.145 307.3832 ]]\n", + "\n", + " [[398.70715 289.91235]]\n", + "\n", + " [[393.5826 272.64722]]\n", + "\n", + " [[388.28845 255.7331 ]]\n", + "\n", + " [[384.17352 239.4941 ]]\n", + "\n", + " [[426.2251 320.06195]]\n", + "\n", + " [[420.66238 302.5737 ]]\n", + "\n", + " [[414.9943 284.84647]]\n", + "\n", + " [[410.52255 267.81345]]\n", + "\n", + " [[405.14496 251.26006]]\n", + "\n", + " [[400.21164 234.16982]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Camera matrix : \n", + "\n", + "[[603.52572321 0. 326.41229818]\n", + " [ 0. 603.65899926 235.9537242 ]\n", + " [ 0. 0. 1. ]]\n", + "dist : \n", + "\n", + "[[ 1.78080527e-01 -1.34103119e+00 -4.51376510e-03 2.52476784e-03\n", + " 4.71440744e+00]]\n", + "rvecs : \n", + "\n", + "(array([[ 0.19951224],\n", + " [-0.31463841],\n", + " [-1.43904688]]), array([[-0.19044869],\n", + " [ 0.44388641],\n", + " [-1.47868386]]), array([[ 0.61618287],\n", + " [ 0.13666227],\n", + " [-1.44098723]]), array([[ 0.01147331],\n", + " [-0.24455644],\n", + " [-1.83474358]]))\n", + "tvecs : \n", + "\n", + "(array([[-6.18074691],\n", + " [ 0.64577552],\n", + " [20.4775399 ]]), array([[-1.33022191],\n", + " [ 2.5755565 ],\n", + " [21.87116664]]), array([[-5.25095778],\n", + " [ 0.22170776],\n", + " [22.06269285]]), array([[-2.0724692 ],\n", + " [ 6.70679783],\n", + " [32.30219442]]))\n" + ] + } + ], + "source": [ + "import cv2\n", + "import numpy as np\n", + "import os\n", + "import glob\n", + "\n", + "# Defining the dimensions of checkerboard\n", + "CHECKERBOARD = (6,9)\n", + "criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", + "\n", + "# Creating vector to store vectors of 3D points for each checkerboard image\n", + "objpoints = []\n", + "# Creating vector to store vectors of 2D points for each checkerboard image\n", + "imgpoints = [] \n", + "\n", + "\n", + "# Defining the world coordinates for 3D points\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", + "prev_img_shape = None\n", + "\n", + "# Extracting path of individual image stored in a given directory\n", + "images = glob.glob('./images/*.jpeg')\n", + "a = 1\n", + "for fname in images:\n", + " print(\"Load images\")\n", + " img = cv2.imread(fname)\n", + " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", + " # Find the chess board corners\n", + " # If desired number of corners are found in the image then ret = true\n", + " print(\"Finding chessboard corners\")\n", + " ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, None)\n", + " \n", + " print(\"corners \\n\")\n", + " print(corners)\n", + " print(\"ret \\n\")\n", + " print(ret)\n", + " \n", + " \"\"\"\n", + " If desired number of corner are detected,\n", + " we refine the pixel coordinates and display \n", + " them on the images of checker board\n", + " \"\"\"\n", + " if ret == True:\n", + " print(\"Corners detected\")\n", + " objpoints.append(objp)\n", + " # refining pixel coordinates for given 2d points.\n", + " corners2 = cv2.cornerSubPix(gray, corners, (11,11),(-1,-1), criteria)\n", + " \n", + " imgpoints.append(corners2)\n", + "\n", + " # Draw and display the corners\n", + " img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)\n", + " \n", + " print(\"Show images\")\n", + " # cv2.imshow('img',img)\n", + " nome='foto_processada' + str(a) + '.jpg'\n", + " cv2.imwrite(nome, img)\n", + " a+=1\n", + "\n", + "h,w = img.shape[:2]\n", + "\n", + "\"\"\"\n", + "Performing camera calibration by \n", + "passing the value of known 3D points (objpoints)\n", + "and corresponding pixel coordinates of the \n", + "detected corners (imgpoints)\n", + "\"\"\"\n", + "ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)\n", + "\n", + "print(\"Camera matrix : \\n\")\n", + "print(mtx)\n", + "print(\"dist : \\n\")\n", + "print(dist)\n", + "print(\"rvecs : \\n\")\n", + "print(rvecs)\n", + "print(\"tvecs : \\n\")\n", + "print(tvecs)\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1dd04b0-6ae7-49a6-bb70-9dbb9adbc5c4", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.8 64-bit ('base': conda)", + "language": "python", + "name": "python388jvsc74a57bd031c25f6a857731a2f8662034dc52be9f81dff2f8cc48f1a86bd9478e5f7d5ada" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/.ipynb_checkpoints/foto_processada1-checkpoint.jpg b/.ipynb_checkpoints/foto_processada1-checkpoint.jpg new file mode 100644 index 0000000000000000000000000000000000000000..7ed0f1eaca18a074feff6ee524b607212c9cfe76 Binary files /dev/null and b/.ipynb_checkpoints/foto_processada1-checkpoint.jpg differ diff --git a/.ipynb_checkpoints/foto_processada2-checkpoint.jpg b/.ipynb_checkpoints/foto_processada2-checkpoint.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0c3e9a59dfe242b66c361ed18e8088ea72261a6c Binary files /dev/null and b/.ipynb_checkpoints/foto_processada2-checkpoint.jpg differ diff --git a/.ipynb_checkpoints/foto_processada3-checkpoint.jpg b/.ipynb_checkpoints/foto_processada3-checkpoint.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c9b30ea4ae5247821a4a69e626586d288d3e08c7 Binary files /dev/null and b/.ipynb_checkpoints/foto_processada3-checkpoint.jpg differ diff --git a/.ipynb_checkpoints/foto_processada4-checkpoint.jpg b/.ipynb_checkpoints/foto_processada4-checkpoint.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6be4de0669aac0b7dc5f6bfc403bacb4680d85d9 Binary files /dev/null and b/.ipynb_checkpoints/foto_processada4-checkpoint.jpg differ diff --git a/CameraCalibration.ipynb b/CameraCalibration.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..9fa3bbe9119284a0bb75236a837087385bbee808 --- /dev/null +++ b/CameraCalibration.ipynb @@ -0,0 +1,728 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 35, + "id": "2a7d76f9-118d-495f-bce1-8d502caa3f4d", + "metadata": {}, + "outputs": [ + { + "ename": "error", + "evalue": "OpenCV(4.5.5) /Users/runner/miniforge3/conda-bld/libopencv_1648505779078/work/modules/calib3d/src/calibration.cpp:3694: error: (-215:Assertion failed) nimages > 0 in function 'calibrateCameraRO'\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31merror\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-35-f93f09ea17d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0mdetected\u001b[0m \u001b[0mcorners\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mimgpoints\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 67\u001b[0m \"\"\"\n\u001b[0;32m---> 68\u001b[0;31m \u001b[0mret\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmtx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrvecs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtvecs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcalibrateCamera\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobjpoints\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mimgpoints\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 69\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Camera matrix : \\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31merror\u001b[0m: OpenCV(4.5.5) /Users/runner/miniforge3/conda-bld/libopencv_1648505779078/work/modules/calib3d/src/calibration.cpp:3694: error: (-215:Assertion failed) nimages > 0 in function 'calibrateCameraRO'\n" + ] + } + ], + "source": [ + "import cv2\n", + "import numpy as np\n", + "import os\n", + "import glob\n", + "\n", + "# Defining the dimensions of checkerboard\n", + "CHECKERBOARD = (6,9)\n", + "criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", + "\n", + "# Creating vector to store vectors of 3D points for each checkerboard image\n", + "objpoints = []\n", + "# Creating vector to store vectors of 2D points for each checkerboard image\n", + "imgpoints = [] \n", + "\n", + "\n", + "# Defining the world coordinates for 3D points\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", + "prev_img_shape = None\n", + "\n", + "# Extracting path of individual image stored in a given directory\n", + "images = glob.glob('./images/*.jpg')\n", + "a = 1\n", + "for fname in images:\n", + " print(\"Load images\")\n", + " img = cv2.imread(fname)\n", + " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", + " # Find the chess board corners\n", + " # If desired number of corners are found in the image then ret = true\n", + " print(\"Finding chessboard corners\")\n", + " ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)\n", + " \n", + " print(\"corners \\n\")\n", + " print(corners)\n", + " print(\"ret \\n\")\n", + " print(ret)\n", + " \n", + " \"\"\"\n", + " If desired number of corner are detected,\n", + " we refine the pixel coordinates and display \n", + " them on the images of checker board\n", + " \"\"\"\n", + " if ret == True:\n", + " print(\"Corners detected\")\n", + " objpoints.append(objp)\n", + " # refining pixel coordinates for given 2d points.\n", + " corners2 = cv2.cornerSubPix(gray, corners, (11,11),(-1,-1), criteria)\n", + " \n", + " imgpoints.append(corners2)\n", + "\n", + " # Draw and display the corners\n", + " img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)\n", + " \n", + " print(\"Show images\")\n", + " # cv2.imshow('img',img)\n", + " nome='foto_processada' + str(a) + '.jpg'\n", + " cv2.imwrite(nome, img)\n", + " a+=1\n", + "\n", + "h,w = img.shape[:2]\n", + "\n", + "\"\"\"\n", + "Performing camera calibration by \n", + "passing the value of known 3D points (objpoints)\n", + "and corresponding pixel coordinates of the \n", + "detected corners (imgpoints)\n", + "\"\"\"\n", + "ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)\n", + "\n", + "print(\"Camera matrix : \\n\")\n", + "print(mtx)\n", + "print(\"dist : \\n\")\n", + "print(dist)\n", + "print(\"rvecs : \\n\")\n", + "print(rvecs)\n", + "print(\"tvecs : \\n\")\n", + "print(tvecs)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "ff6380f6-b96f-469c-bd7f-17fbb3721389", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[142.64168 254.9432 ]]\n", + "\n", + " [[146.75854 225.61407 ]]\n", + "\n", + " [[150.54066 196.51573 ]]\n", + "\n", + " [[154.04866 167.27895 ]]\n", + "\n", + " [[157.64587 138.57889 ]]\n", + "\n", + " [[161.52353 110.188255]]\n", + "\n", + " [[173.47812 258.40778 ]]\n", + "\n", + " [[176.93672 229.4982 ]]\n", + "\n", + " [[180.49413 200.70557 ]]\n", + "\n", + " [[184.05624 171.98401 ]]\n", + "\n", + " [[187.66382 144.17943 ]]\n", + "\n", + " [[190.79807 116.24408 ]]\n", + "\n", + " [[203.03679 261.4418 ]]\n", + "\n", + " [[206.62283 232.9877 ]]\n", + "\n", + " [[209.51048 204.94817 ]]\n", + "\n", + " [[212.91487 176.95428 ]]\n", + "\n", + " [[216.23514 149.11143 ]]\n", + "\n", + " [[219.46265 121.66086 ]]\n", + "\n", + " [[231.7584 264.68643 ]]\n", + "\n", + " [[234.71873 236.5048 ]]\n", + "\n", + " [[237.93755 209.0442 ]]\n", + "\n", + " [[241.19179 181.42677 ]]\n", + "\n", + " [[243.81526 154.10678 ]]\n", + "\n", + " [[246.94101 126.50662 ]]\n", + "\n", + " [[259.238 267.7193 ]]\n", + "\n", + " [[262.34668 240.02777 ]]\n", + "\n", + " [[265.4873 213.02408 ]]\n", + "\n", + " [[268.29196 185.80069 ]]\n", + "\n", + " [[271.08856 158.79974 ]]\n", + "\n", + " [[274.03 131.71118 ]]\n", + "\n", + " [[285.894 270.6857 ]]\n", + "\n", + " [[288.91974 243.85074 ]]\n", + "\n", + " [[291.67294 216.57468 ]]\n", + "\n", + " [[294.623 189.9902 ]]\n", + "\n", + " [[297.21625 163.05467 ]]\n", + "\n", + " [[300.24863 136.88556 ]]\n", + "\n", + " [[312.1065 273.9727 ]]\n", + "\n", + " [[314.64572 246.71307 ]]\n", + "\n", + " [[317.157 220.33134 ]]\n", + "\n", + " [[320.10812 193.81598 ]]\n", + "\n", + " [[322.75974 167.55089 ]]\n", + "\n", + " [[325.6024 141.55737 ]]\n", + "\n", + " [[336.9503 276.28848 ]]\n", + "\n", + " [[339.49103 249.95747 ]]\n", + "\n", + " [[341.7496 224.4254 ]]\n", + "\n", + " [[344.58643 197.6241 ]]\n", + "\n", + " [[347.26367 172.06337 ]]\n", + "\n", + " [[350.02148 146.011 ]]\n", + "\n", + " [[361.31314 279.1676 ]]\n", + "\n", + " [[363.75967 252.91393 ]]\n", + "\n", + " [[365.80142 227.11314 ]]\n", + "\n", + " [[368.71347 201.5229 ]]\n", + "\n", + " [[371.02176 176.04767 ]]\n", + "\n", + " [[373.48065 150.7466 ]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[290.16937 306.88214]]\n", + "\n", + " [[290.53278 280.11667]]\n", + "\n", + " [[290.77576 252.93272]]\n", + "\n", + " [[291.0845 225.27896]]\n", + "\n", + " [[291.53607 197.0897 ]]\n", + "\n", + " [[292.41327 168.24815]]\n", + "\n", + " [[315.06952 311.0208 ]]\n", + "\n", + " [[315.6739 283.6647 ]]\n", + "\n", + " [[316.2393 256.1716 ]]\n", + "\n", + " [[316.59756 227.64337]]\n", + "\n", + " [[317.8294 198.79442]]\n", + "\n", + " [[318.80643 169.55096]]\n", + "\n", + " [[340.638 315.39575]]\n", + "\n", + " [[341.63934 287.54718]]\n", + "\n", + " [[342.54242 259.35623]]\n", + "\n", + " [[343.91376 230.37785]]\n", + "\n", + " [[345.06934 201.05199]]\n", + "\n", + " [[346.2913 171.26308]]\n", + "\n", + " [[367.9363 319.6347 ]]\n", + "\n", + " [[368.97598 291.21472]]\n", + "\n", + " [[370.40988 262.52164]]\n", + "\n", + " [[371.5862 233.02112]]\n", + "\n", + " [[373.30624 203.30626]]\n", + "\n", + " [[374.47787 172.69196]]\n", + "\n", + " [[396.0801 324.31546]]\n", + "\n", + " [[397.2893 295.3254 ]]\n", + "\n", + " [[398.77914 265.93915]]\n", + "\n", + " [[400.4216 236.04366]]\n", + "\n", + " [[402.11496 205.60799]]\n", + "\n", + " [[403.97397 174.59514]]\n", + "\n", + " [[425.58887 328.73993]]\n", + "\n", + " [[427.1569 299.32626]]\n", + "\n", + " [[428.80847 269.61078]]\n", + "\n", + " [[430.6196 238.81488]]\n", + "\n", + " [[432.5938 207.88199]]\n", + "\n", + " [[434.81232 176.30792]]\n", + "\n", + " [[456.30505 333.57922]]\n", + "\n", + " [[458.36752 303.69302]]\n", + "\n", + " [[460.31778 273.17523]]\n", + "\n", + " [[462.44522 242.11354]]\n", + "\n", + " [[464.63388 210.30637]]\n", + "\n", + " [[467.06512 178.04515]]\n", + "\n", + " [[488.32422 338.7988 ]]\n", + "\n", + " [[490.48627 308.26813]]\n", + "\n", + " [[492.9652 276.772 ]]\n", + "\n", + " [[495.6021 245.111 ]]\n", + "\n", + " [[498.3957 212.66432]]\n", + "\n", + " [[501.15192 179.73419]]\n", + "\n", + " [[521.8725 344.20172]]\n", + "\n", + " [[524.48047 312.59525]]\n", + "\n", + " [[527.2914 281.0416 ]]\n", + "\n", + " [[530.3484 248.3829 ]]\n", + "\n", + " [[533.45984 215.4207 ]]\n", + "\n", + " [[536.5843 181.58168]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[181.65076 240.43515 ]]\n", + "\n", + " [[182.98524 216.97383 ]]\n", + "\n", + " [[184.64604 192.23103 ]]\n", + "\n", + " [[186.13664 165.44173 ]]\n", + "\n", + " [[187.51831 136.71654 ]]\n", + "\n", + " [[188.62773 106.87892 ]]\n", + "\n", + " [[209.48434 240.83936 ]]\n", + "\n", + " [[211.59288 217.80495 ]]\n", + "\n", + " [[213.6713 193.07858 ]]\n", + "\n", + " [[215.5483 166.35489 ]]\n", + "\n", + " [[217.48494 138.31812 ]]\n", + "\n", + " [[219.43669 108.85271 ]]\n", + "\n", + " [[236.70854 241.51544 ]]\n", + "\n", + " [[239.46967 218.49294 ]]\n", + "\n", + " [[241.6721 194.00006 ]]\n", + "\n", + " [[244.1657 167.18895 ]]\n", + "\n", + " [[246.48727 139.78937 ]]\n", + "\n", + " [[249.17108 111.17387 ]]\n", + "\n", + " [[263.09882 241.98895 ]]\n", + "\n", + " [[266.31976 218.94453 ]]\n", + "\n", + " [[269.2836 194.60536 ]]\n", + "\n", + " [[272.11176 168.56544 ]]\n", + "\n", + " [[275.07178 141.52344 ]]\n", + "\n", + " [[278.2868 113.34714 ]]\n", + "\n", + " [[288.6269 242.41682 ]]\n", + "\n", + " [[292.33762 219.4731 ]]\n", + "\n", + " [[295.634 195.30553 ]]\n", + "\n", + " [[299.0187 169.72737 ]]\n", + "\n", + " [[302.50778 143.14719 ]]\n", + "\n", + " [[306.42252 115.552376]]\n", + "\n", + " [[313.5622 242.52242 ]]\n", + "\n", + " [[317.43845 219.64905 ]]\n", + "\n", + " [[321.32867 195.67903 ]]\n", + "\n", + " [[325.13644 170.68053 ]]\n", + "\n", + " [[329.4506 144.55687 ]]\n", + "\n", + " [[333.65683 117.407425]]\n", + "\n", + " [[337.9189 242.53233 ]]\n", + "\n", + " [[342.0708 219.9899 ]]\n", + "\n", + " [[346.30737 196.3446 ]]\n", + "\n", + " [[350.5993 171.67969 ]]\n", + "\n", + " [[355.18494 146.09834 ]]\n", + "\n", + " [[360.2282 119.398865]]\n", + "\n", + " [[361.50598 242.38771 ]]\n", + "\n", + " [[365.82083 220.18697 ]]\n", + "\n", + " [[370.56412 196.69379 ]]\n", + "\n", + " [[375.1744 172.38173 ]]\n", + "\n", + " [[380.44196 147.4049 ]]\n", + "\n", + " [[386.07758 121.229385]]\n", + "\n", + " [[384.24615 242.4664 ]]\n", + "\n", + " [[389.10178 220.35866 ]]\n", + "\n", + " [[394.30902 197.43806 ]]\n", + "\n", + " [[399.71222 173.47057 ]]\n", + "\n", + " [[405.3322 148.5755 ]]\n", + "\n", + " [[411.4092 122.746666]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Load images\n", + "Finding chessboard corners\n", + "corners \n", + "\n", + "[[[287.53036 361.7949 ]]\n", + "\n", + " [[282.58374 342.85358]]\n", + "\n", + " [[278.01462 325.26227]]\n", + "\n", + " [[272.65985 307.25195]]\n", + "\n", + " [[268.46243 289.60532]]\n", + "\n", + " [[263.20535 271.30942]]\n", + "\n", + " [[305.57693 356.4085 ]]\n", + "\n", + " [[300.56805 337.95624]]\n", + "\n", + " [[295.50082 319.71555]]\n", + "\n", + " [[290.40643 301.90472]]\n", + "\n", + " [[285.50308 284.46655]]\n", + "\n", + " [[281.19418 267.06412]]\n", + "\n", + " [[323.32886 351.0642 ]]\n", + "\n", + " [[318.34674 332.87128]]\n", + "\n", + " [[313.38998 314.55502]]\n", + "\n", + " [[307.87402 296.82272]]\n", + "\n", + " [[303.11688 279.44043]]\n", + "\n", + " [[298.32162 261.79645]]\n", + "\n", + " [[340.5354 345.56195]]\n", + "\n", + " [[335.7001 327.50388]]\n", + "\n", + " [[330.57962 310.177 ]]\n", + "\n", + " [[325.31915 292.1104 ]]\n", + "\n", + " [[320.627 274.28772]]\n", + "\n", + " [[315.36737 257.47568]]\n", + "\n", + " [[358.15817 340.35425]]\n", + "\n", + " [[352.91727 322.5884 ]]\n", + "\n", + " [[347.64328 304.45084]]\n", + "\n", + " [[342.7978 287.11328]]\n", + "\n", + " [[337.47632 269.54434]]\n", + "\n", + " [[332.27792 253.0178 ]]\n", + "\n", + " [[375.521 334.54907]]\n", + "\n", + " [[370.54572 317.49283]]\n", + "\n", + " [[365.13348 300.1963 ]]\n", + "\n", + " [[359.96573 282.50775]]\n", + "\n", + " [[354.63464 264.84613]]\n", + "\n", + " [[349.94232 247.95018]]\n", + "\n", + " [[392.5023 330.13177]]\n", + "\n", + " [[387.49005 312.34338]]\n", + "\n", + " [[381.98724 294.8061 ]]\n", + "\n", + " [[376.97012 277.55432]]\n", + "\n", + " [[371.30545 260.40204]]\n", + "\n", + " [[366.55707 243.28087]]\n", + "\n", + " [[409.58173 325.1956 ]]\n", + "\n", + " [[404.145 307.3832 ]]\n", + "\n", + " [[398.70715 289.91235]]\n", + "\n", + " [[393.5826 272.64722]]\n", + "\n", + " [[388.28845 255.7331 ]]\n", + "\n", + " [[384.17352 239.4941 ]]\n", + "\n", + " [[426.2251 320.06195]]\n", + "\n", + " [[420.66238 302.5737 ]]\n", + "\n", + " [[414.9943 284.84647]]\n", + "\n", + " [[410.52255 267.81345]]\n", + "\n", + " [[405.14496 251.26006]]\n", + "\n", + " [[400.21164 234.16982]]]\n", + "ret \n", + "\n", + "True\n", + "Corners detected\n", + "Show images\n", + "Camera matrix : \n", + "\n", + "[[603.52572321 0. 326.41229818]\n", + " [ 0. 603.65899926 235.9537242 ]\n", + " [ 0. 0. 1. ]]\n", + "dist : \n", + "\n", + "[[ 1.78080527e-01 -1.34103119e+00 -4.51376510e-03 2.52476784e-03\n", + " 4.71440744e+00]]\n", + "rvecs : \n", + "\n", + "(array([[ 0.19951224],\n", + " [-0.31463841],\n", + " [-1.43904688]]), array([[-0.19044869],\n", + " [ 0.44388641],\n", + " [-1.47868386]]), array([[ 0.61618287],\n", + " [ 0.13666227],\n", + " [-1.44098723]]), array([[ 0.01147331],\n", + " [-0.24455644],\n", + " [-1.83474358]]))\n", + "tvecs : \n", + "\n", + "(array([[-6.18074691],\n", + " [ 0.64577552],\n", + " [20.4775399 ]]), array([[-1.33022191],\n", + " [ 2.5755565 ],\n", + " [21.87116664]]), array([[-5.25095778],\n", + " [ 0.22170776],\n", + " [22.06269285]]), array([[-2.0724692 ],\n", + " [ 6.70679783],\n", + " [32.30219442]]))\n" + ] + } + ], + "source": [ + "import cv2\n", + "import numpy as np\n", + "import os\n", + "import glob\n", + "\n", + "# Defining the dimensions of checkerboard\n", + "CHECKERBOARD = (6,9)\n", + "criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", + "\n", + "# Creating vector to store vectors of 3D points for each checkerboard image\n", + "objpoints = []\n", + "# Creating vector to store vectors of 2D points for each checkerboard image\n", + "imgpoints = [] \n", + "\n", + "\n", + "# Defining the world coordinates for 3D points\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", + "prev_img_shape = None\n", + "\n", + "# Extracting path of individual image stored in a given directory\n", + "images = glob.glob('./images/*.jpeg')\n", + "a = 1\n", + "for fname in images:\n", + " print(\"Load images\")\n", + " img = cv2.imread(fname)\n", + " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", + " # Find the chess board corners\n", + " # If desired number of corners are found in the image then ret = true\n", + " print(\"Finding chessboard corners\")\n", + " ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, None)\n", + " \n", + " print(\"corners \\n\")\n", + " print(corners)\n", + " print(\"ret \\n\")\n", + " print(ret)\n", + " \n", + " \"\"\"\n", + " If desired number of corner are detected,\n", + " we refine the pixel coordinates and display \n", + " them on the images of checker board\n", + " \"\"\"\n", + " if ret == True:\n", + " print(\"Corners detected\")\n", + " objpoints.append(objp)\n", + " # refining pixel coordinates for given 2d points.\n", + " corners2 = cv2.cornerSubPix(gray, corners, (11,11),(-1,-1), criteria)\n", + " \n", + " imgpoints.append(corners2)\n", + "\n", + " # Draw and display the corners\n", + " img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)\n", + " \n", + " print(\"Show images\")\n", + " # cv2.imshow('img',img)\n", + " nome='foto_processada' + str(a) + '.jpg'\n", + " cv2.imwrite(nome, img)\n", + " a+=1\n", + "\n", + "h,w = img.shape[:2]\n", + "\n", + "\"\"\"\n", + "Performing camera calibration by \n", + "passing the value of known 3D points (objpoints)\n", + "and corresponding pixel coordinates of the \n", + "detected corners (imgpoints)\n", + "\"\"\"\n", + "ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)\n", + "\n", + "print(\"Camera matrix : \\n\")\n", + "print(mtx)\n", + "print(\"dist : \\n\")\n", + "print(dist)\n", + "print(\"rvecs : \\n\")\n", + "print(rvecs)\n", + "print(\"tvecs : \\n\")\n", + "print(tvecs)\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1dd04b0-6ae7-49a6-bb70-9dbb9adbc5c4", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.8 64-bit ('base': conda)", + "language": "python", + "name": "python388jvsc74a57bd031c25f6a857731a2f8662034dc52be9f81dff2f8cc48f1a86bd9478e5f7d5ada" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/foto_processada1.jpg b/foto_processada1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3f4fa285fc39526d27b3774eb58939aa0f3181d0 Binary files /dev/null and b/foto_processada1.jpg differ diff --git a/foto_processada2.jpg b/foto_processada2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..21cb72f80cfc2551d64086e6efb1b93de93d138f Binary files /dev/null and b/foto_processada2.jpg differ diff --git a/foto_processada3.jpg b/foto_processada3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..55bc395cda2efd66a35a347fd3b2e64af24120eb Binary files /dev/null and b/foto_processada3.jpg differ diff --git a/foto_processada4.jpg b/foto_processada4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5bf6246fcd8e6da70d6cc53a9800100aef15d2e7 Binary files /dev/null and b/foto_processada4.jpg differ diff --git a/images/photo_2022-07-19 18.46.50.jpeg b/images/photo_2022-07-19 18.46.50.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e915d08019f47aaf050426e6934147ab712fbaf7 Binary files /dev/null and b/images/photo_2022-07-19 18.46.50.jpeg differ diff --git a/images/photo_2022-07-19 18.46.53.jpeg b/images/photo_2022-07-19 18.46.53.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..a8690684ca280d5fe5e217791f1eacba7a2c8916 Binary files /dev/null and b/images/photo_2022-07-19 18.46.53.jpeg differ diff --git a/images/photo_2022-07-19 18.46.55.jpeg b/images/photo_2022-07-19 18.46.55.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..3f836b1a4105d7ef7826efd97107661cc2b7207c Binary files /dev/null and b/images/photo_2022-07-19 18.46.55.jpeg differ diff --git a/images/photo_2022-07-19 18.48.00.jpeg b/images/photo_2022-07-19 18.48.00.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..085e2eeee360eacb69b5980595a47e7c06d36fc6 Binary files /dev/null and b/images/photo_2022-07-19 18.48.00.jpeg differ