Home > other >  In python running camera calibration procedure, to cv2. FindChessboardCorners (gray, 8 (11), None) r
In python running camera calibration procedure, to cv2. FindChessboardCorners (gray, 8 (11), None) r

Time:10-11

With python2 and opencv2 program is also in the BBS for
But application in ret, corners=cv2. FindChessboardCorners (gray, 8 (11), None) seemed to move here, please help to see
Enclosed I have a problem with the figure and code there seems to be no access to in this code the checkerboard method of unit length, how he calibration of

 
# - * - coding: utf-8 - * -

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
Criteria=(cv2 TERM_CRITERIA_MAX_ITER | cv2. TERM_CRITERIA_EPS, 50 and 0.001)

# for the position of the calibration of voids point
Objp=np. Zeros ((8 * 11, 3), np float32)
Objp [: : 2]=np. Mgrid [0:11, 0:8]. 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


Img=cv2. Imread ('/home/PI/Desktop/aboy/qipan. JPG ')
Gray=cv2. CvtColor (img, cv2 COLOR_BGR2GRAY)
Size=gray. The shape] [: : - 1
Ret, corners=cv2. FindChessboardCorners (gray, 8 (11), None)
Print '1'
If ret:
Obj_points. Append (objp)
Corners2=cv2. CornerSubPix (gray, corners, (11, 11), (1, 1), the criteria) # on the basis of the original angular point for subpixel corner
If corners2:
Img_points. Append (corners2)
The else:
Img_points. Append (corners)

Cv2. DrawChessboardCorners (img, 8 (11), corners, ret) # remember, OpenCV general drawing function has no return value
Cv2. Imshow (' img 'img)
Cv2. WaitKey (50)

Print len (img_points)
Cv2. DestroyAllWindows ()

# calibration
Ret, MTX, dist, rvecs tvecs=cv2. CalibrateCamera (obj_points img_points, size, None, None)

Ret: "print", 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", outside tvecs # # translation vector parameter

Print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
# distortion correction
Img=cv2. Imread (images [12])
H, w=img. Shape [: 2]
Newcameramtx, ROI=cv2. GetOptimalNewCameraMatrix (MTX, dist, (w, h), 1, (w, h))
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 (' calibresult11. JPG, dst1)
Print "method one: the size of the DST is:", dst1. Shape

# undistort method 2
Print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- using the mapping way -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
Mapx, mapy=cv2. InitUndistortRectifyMap (MTX, dist, None, newcameramtx, (w, h), 5) # for mapping equation
# DST=cv2. Remap (img and mapx, mapy, cv2 INTER_LINEAR) # heavy map
DST=cv2. Remap (img and mapx, mapy, cv2 INTER_CUBIC) # after heavy map, the image becomes
X, y, w, h=ROI
Dst2=DST [y, y + h, x: x + w]
Cv2. Imwrite (' calibresult11_2. JPG, dst2)
Print "method 2: the size of the DST is:", dst2. Shape # image than method a little

Print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- calculating reverse projection error -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
Tot_error=0
For I in xrange (len (obj_points) :
Img_points2, _=cv2. ProjectPoints (obj_points [I], rvecs [I], tvecs [I], MTX, dist)
Error=cv2. Norm (img_points [I], img_points2, cv2. NORM_L2)/len (img_points2)
Tot_error +=error

Mean_error=tot_error/len (obj_points)
Print "total error", tot_error
Print "mean error:", mean_error

CodePudding user response:

Try to (8, 11)
  • Related