(1)标定摄像机获得摄像机矩阵K(内参数矩阵)
内参数矩阵表示从归一化图像平面到物理成像平面的转换关系。 用OpenCV中的库函数标定摄像机会出现很多问题,主要包括:
1)
摄像头分辨率:尝试过使用手机摄像头,现在的手机拍摄的图片分辨率过高以至于找不到标定板的位置,因为有可能是检测角点的窗口范围设置有限,所以,分辨率过高的图像中的角点在窗口中水平方向和垂直方向的灰度梯度变化不明显,角点部分由过多的像素过渡,在过小的窗口看起来角点更像是变化的弧线。可以通过下采样的方式先降低图像分辨率,检测到模式后再上采样恢复角点实际在图像中的位置。然而,用笔记本的摄像头则没有这样的问题出现。
2)
设置模式大小:寻找标定板时,需要手动调整标定板对象点objpoint的容量,笔者这里的容量为9*6;如果不手动调整有可能找不到标定板。
摄像头标定代码如下:
import cv2
import numpy as np
import glob
################################################################################
print 'criteria and object points set'
# termination criteria
criteria = (3L, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(8,5,0)
objpoint = np.zeros((9 * 6, 3), np.float32)
objpoint[:,:2] = np.mgrid[0:9, 0:6].T.reshape(-1,2)
# arrays to store object points and image points from all the images
# 3d point in real world space
objpoints = []
# 2d points in image plane
imgpoints = []
################################################################################
print 'Load Images'
images = glob.glob('images/Phone Camera/*.bmp')
for frame in images:
img = cv2.imread(frame)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# find chess board corners
ret, corners = cv2.findChessboardCorners(imgGray, (9,6), None)
# print ret to check if pattern size is set correctly
print ret
# if found, add object points, image points (after refining them)
if ret == True:
# add object points
objpoints.append(objpoint)
cv2.cornerSubPix(imgGray, corners, (11,11), (-1,-1), criteria)
# add corners as image points
imgpoints.append(corners)
# draw corners
cv2.drawChessboardCorners(img, (9,6), corners, ret)
cv2.imshow('Image',img)
cv2.waitKey(0