|
778 | 778 | " cv.destroyAllWindows()\n", |
779 | 779 | " cv.waitKey(1000)" |
780 | 780 | ] |
| 781 | + }, |
| 782 | + { |
| 783 | + "cell_type": "markdown", |
| 784 | + "metadata": {}, |
| 785 | + "source": [ |
| 786 | + "# Code tyding up" |
| 787 | + ] |
| 788 | + }, |
| 789 | + { |
| 790 | + "cell_type": "code", |
| 791 | + "execution_count": 1, |
| 792 | + "metadata": {}, |
| 793 | + "outputs": [ |
| 794 | + { |
| 795 | + "name": "stdout", |
| 796 | + "output_type": "stream", |
| 797 | + "text": [ |
| 798 | + "Are Chessboard corners detected:\n", |
| 799 | + " True \n", |
| 800 | + "\n", |
| 801 | + "0.2789344851009926\n", |
| 802 | + "Camera Matrix:\n", |
| 803 | + " [[459.49635782 0. 462.98078123]\n", |
| 804 | + " [ 0. 425.93925902 332.55096499]\n", |
| 805 | + " [ 0. 0. 1. ]] \n", |
| 806 | + "\n", |
| 807 | + "Distortion Coefficient:\n", |
| 808 | + " [[-0.13575781 0.02472651 0.00342986 0.00018729 -0.00074961]] \n", |
| 809 | + "\n", |
| 810 | + "(array([[-0.07403562],\n", |
| 811 | + " [ 0.00389051],\n", |
| 812 | + " [ 0.07810778]]),) \n", |
| 813 | + " (array([[-12.0383291 ],\n", |
| 814 | + " [-13.96143052],\n", |
| 815 | + " [ 20.05135661]]),)\n" |
| 816 | + ] |
| 817 | + } |
| 818 | + ], |
| 819 | + "source": [ |
| 820 | + "import cv2 as cv\n", |
| 821 | + "import numpy as np\n", |
| 822 | + "import matplotlib.pyplot as plt\n", |
| 823 | + "\n", |
| 824 | + "def showimg(windowname, img, atthelast = False):\n", |
| 825 | + " cv.imshow(windowname, img)\n", |
| 826 | + " cv.waitKey(0)\n", |
| 827 | + " cv.destroyAllWindows()\n", |
| 828 | + "\n", |
| 829 | + " if atthelast:\n", |
| 830 | + " cv.waitKey(1000)\n", |
| 831 | + "\n", |
| 832 | + "\n", |
| 833 | + "criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)\n", |
| 834 | + "\n", |
| 835 | + "W = 23\n", |
| 836 | + "L = 31\n", |
| 837 | + "\n", |
| 838 | + "objp = np.zeros((W*L, 3), np.float32)\n", |
| 839 | + "objp[:,:2] = np.mgrid[0:L,0:W].T.reshape(-1,2)\n", |
| 840 | + "\n", |
| 841 | + "objpoints = [] \n", |
| 842 | + "imgpoints = [] \n", |
| 843 | + "\n", |
| 844 | + "imgname = 'camera_calib5.jpg'\n", |
| 845 | + "imgpath = f'images/{imgname}'\n", |
| 846 | + "\n", |
| 847 | + "\n", |
| 848 | + "img = cv.imread(imgpath)\n", |
| 849 | + "showimg(imgname, img)\n", |
| 850 | + "\n", |
| 851 | + "grayscaledimg = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n", |
| 852 | + "grayscaledimgtitle = imgname + \" grayscaled\"\n", |
| 853 | + "showimg(grayscaledimgtitle, grayscaledimg)\n", |
| 854 | + "\n", |
| 855 | + "ret, corners = cv.findChessboardCorners(grayscaledimg, (L,W), None)\n", |
| 856 | + "print(\"Are Chessboard corners detected:\\n\", ret, \"\\n\")\n", |
| 857 | + "\n", |
| 858 | + "if ret:\n", |
| 859 | + " objpoints.append(objp)\n", |
| 860 | + "\n", |
| 861 | + " corners2 = cv.cornerSubPix(grayscaledimg, corners, (11, 11), (-1, -1), criteria)\n", |
| 862 | + " imgpoints.append(corners2)\n", |
| 863 | + "\n", |
| 864 | + " cv.drawChessboardCorners(img, (L, W), corners2, ret)\n", |
| 865 | + " window_name = imgpath + ' with corner'\n", |
| 866 | + " showimg(window_name, img)\n", |
| 867 | + "\n", |
| 868 | + " ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, grayscaledimg.shape[::-1], None, None)\n", |
| 869 | + " print(ret)\n", |
| 870 | + " print(\"Camera Matrix:\\n\", mtx, \"\\n\")\n", |
| 871 | + " print(\"Distortion Coefficient:\\n\", dist, \"\\n\")\n", |
| 872 | + " print(rvecs, \"\\n\", tvecs)\n", |
| 873 | + "\n", |
| 874 | + " h, w = grayscaledimg.shape[:2]\n", |
| 875 | + " newcameramtx, roi = cv.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))\n", |
| 876 | + "\n", |
| 877 | + " dst = cv.undistort(grayscaledimg, mtx, dist, None, newcameramtx)\n", |
| 878 | + " \n", |
| 879 | + " x, y, w, h = roi\n", |
| 880 | + " dst = dst[y:y+h, x:x+w]\n", |
| 881 | + " outputpath = f\"output/{imgname}\"\n", |
| 882 | + " showimg(window_name, dst, True)" |
| 883 | + ] |
781 | 884 | } |
782 | 885 | ], |
783 | 886 | "metadata": { |
|
0 commit comments