验证中...
Languages: Python
Categories: 图形图像处理
Latest update 2019-10-10 10:20
gistfile1.txt
Raw Copy
#encoding=utf-8
import numpy as np
import cv2
import os
def undistortion(img, mtx, dist):
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
print('roi ', roi)
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# crop the image
x, y, w, h = roi
if roi != (0, 0, 0, 0):
dst = dst[y:y + h, x:x + w]
return dst
def calibrate():
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# 检测棋盘格的内点
Nx_cor = 9
Ny_cor = 6
objp = np.zeros((Nx_cor * Ny_cor, 3), np.float32)
objp[:, :2] = np.mgrid[0:Nx_cor, 0:Ny_cor].T.reshape(-1, 2)
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# source = ''# 海康摄像头的ip
# cap = cv2.VideoCapture(source)
imagePath = ''
count = 0 # count 用来标志成功检测到的棋盘格画面数量
while (True):
frame = cv2.imread(os.path.join(imagePath, str(count) + '.jpg'))
if frame:
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (Nx_cor, Ny_cor), None) # Find the corners
# If found, add object points, image points
if ret == True:
corners = cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), criteria)
objpoints.append(objp)
imgpoints.append(corners)
cv2.drawChessboardCorners(frame, (Nx_cor, Ny_cor), corners, ret)
count += 1
else:
print('end read image!')
break
# Display the resulting frame
# cv2.imshow('frame', frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
global mtx, dist
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
print(mtx, dist)
mean_error = 0
for i in range(len(objpoints)):
imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2)
mean_error += error
print ("total error: ", mean_error / len(objpoints))
# # When everything done, release the capture
np.savez('calibrate.npz', mtx=mtx, dist=dist[0:4])
def undistortion(img, mtx, dist):
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# crop the image
x, y, w, h = roi
if roi != (0, 0, 0, 0):
dst = dst[y:y + h, x:x + w]
return dst
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
mtx = []
dist = []
# 如果有文件,那么读取文件获取摄像机内参,如果没有那么计算摄像机内参
try:
npzfile = np.load('calibrate.npz')
mtx = npzfile['mtx']
dist = npzfile['dist']
except IOError:
calibrate()
print('dist', dist[0:4])
while (True):
ret, frame = cap.read()
frame = undistortion(frame, mtx, dist[0:4])
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Comment list( 0 )

You need to Sign in for post a comment

Help Search