Skip to content

Commit da63a88

Browse files
committed
calibration ipynb py: code tyding up
1 parent 343dfaa commit da63a88

File tree

2 files changed

+119
-16
lines changed

2 files changed

+119
-16
lines changed

calibration.ipynb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,109 @@
778778
" cv.destroyAllWindows()\n",
779779
" cv.waitKey(1000)"
780780
]
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+
]
781884
}
782885
],
783886
"metadata": {

calibration.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
import numpy as np
33
import matplotlib.pyplot as plt
44

5+
def showimg(windowname, img, atthelast = False):
6+
cv.imshow(windowname, img)
7+
cv.waitKey(0)
8+
cv.destroyAllWindows()
9+
10+
if atthelast:
11+
cv.waitKey(1000)
12+
13+
514
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
615

716
W = 23
@@ -15,32 +24,27 @@
1524

1625
imgname = 'camera_calib5.jpg'
1726
imgpath = f'images/{imgname}'
27+
28+
1829
img = cv.imread(imgpath)
19-
cv.imshow(imgname, img)
20-
cv.waitKey(0)
21-
cv.destroyAllWindows()
30+
showimg(imgname, img)
2231

2332
grayscaledimg = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
2433
grayscaledimgtitle = imgname + " grayscaled"
25-
cv.imshow(grayscaledimgtitle, grayscaledimg)
26-
cv.waitKey(0)
27-
cv.destroyAllWindows()
34+
showimg(grayscaledimgtitle, grayscaledimg)
2835

2936
ret, corners = cv.findChessboardCorners(grayscaledimg, (L,W), None)
3037
print("Are Chessboard corners detected:\n", ret, "\n")
3138

32-
if ret == True:
39+
if ret:
3340
objpoints.append(objp)
3441

3542
corners2 = cv.cornerSubPix(grayscaledimg, corners, (11, 11), (-1, -1), criteria)
3643
imgpoints.append(corners2)
3744

38-
# Draw and display the corners
3945
cv.drawChessboardCorners(img, (L, W), corners2, ret)
4046
window_name = imgpath + ' with corner'
41-
cv.imshow(window_name,img)
42-
cv.waitKey(0)
43-
cv.destroyAllWindows()
47+
showimg(window_name, img)
4448

4549
ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, grayscaledimg.shape[::-1], None, None)
4650
print(ret)
@@ -56,8 +60,4 @@
5660
x, y, w, h = roi
5761
dst = dst[y:y+h, x:x+w]
5862
outputpath = f"output/{imgname}"
59-
cv.imshow(window_name, dst)
60-
cv.waitKey(0)
61-
62-
cv.destroyAllWindows()
63-
cv.waitKey(1000)
63+
showimg(window_name, dst, True)

0 commit comments

Comments
 (0)