The import cv2
The import numpy as np
The import glob
# set to find the parameters of the subpixel corner, is the largest circulation stopping criteria used 30 times and the maximum error tolerance 0.001
The criteria=(cv2 TERM_CRITERIA_MAX_ITER | cv2. TERM_CRITERIA_EPS, 30, 0.001)
# for the position of the calibration of voids point
Objp=np. Zeros ((6 * 8, 3), np float32)
Objp [: : 2]=np. Mgrid [0:8, 0-6]. T.r eshape # (1, 2) the world coordinate system is built on the calibration board, all the points of Z coordinate is 0, so only need to assign a value of x and y
Obj_points=[] # storage point 3 d
Img_points=[] # store 2 d point
# glob. Glob function used to read images in a list, for the following function call
Images=glob. Glob (r "C: \ Users \ Finder \ AppData \ Local \ designed \ Python \ Python36 \ xiangjibiaodingtupian/*. JPG")
i=0;
For fname images in:
Img=cv2. Imread (fname)
Gray=cv2. CvtColor (img, cv2 COLOR_BGR2GRAY) # converts images to grayscale
Size=gray. The shape/: : 1 # is used to change the image size
Ret, corners=cv2. FindChessboardCorners (gray, (8, 6), None)
# print (corners)
If ret:
Obj_points. Append (objp)
Corners2=cv2. CornerSubPix (gray, corners, (5, 5), (1, 1), the criteria) # on the basis of the original angular point for subpixel corner
# print (corners2)
If [corners2] :
Img_points. Append (corners2)
The else:
Img_points. Append (corners)
Cv2. DrawChessboardCorners (img, (8, 6), corners, ret) # remember, OpenCV general drawing function has no return value
I +=1;
Cv2. Imwrite (' conimg + STR (I) + 'JPG', img)
Cv2. WaitKey (1500)
Print (len (img_points))
Cv2. DestroyAllWindows ()
# calibration
Ret, MTX, dist, rvecs tvecs=cv2. CalibrateCamera (obj_points img_points, size, None, None)
Print (" ret ", ret)
Print (" MTX: \ n ", MTX) # parameter matrix in
Print (" dist: \ n ", dist) # distortion coefficient of distortion cofficients=(k_1, k_2, p_1, p_2, k_3)
Print (" rvecs: \ n ", rvecs) # # outside the parameters of rotating vector
Print (" tvecs: \ n ", tvecs) outside # # translation vector parameter
Print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
Img=cv2. Imread (images [2])
H, w=img. Shape [: 2]
Newcameramtx, ROI=cv2. GetOptimalNewCameraMatrix (MTX, dist, (w, h), 1, (w, h) # according to the wider picture after mapping (normal weight will delete the part of the image)
Print (newcameramtx)
Print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- use undistort function -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
DST=cv2. Undistort (img, MTX, dist, None, newcameramtx)
X, y, w, h=ROI
Dst1=DST [y, y + h, x: x + w]
Cv2. Imwrite (' calibresult3. JPG, dst1)
Print (" method one: the size of the DST is: ", dst1, shape)
To have for a long time to come out as a result, is the problem that the code itself or is not computer, run code is slow, please give a big,,,,,,,,,