OpenCV-python应用示例

0.概述

相机是机器人、太空探索等多个领域不可或缺的一部分,相机起着重要作用。它有助于捕捉每一个时刻,并有助于许多分析。为了将相机用作视觉传感器,需要知道相机的参数。相机校准只是估计相机的参数,需要有关相机的参数来确定现实世界中的3D点与其在校准相机捕获的图像中的相应2D投影(像素)之间的准确关系。
需要考虑内部参数,如焦距、光学中心和透镜的径向畸变系数等,以及外部参数,如相机相对于现实世界坐标系的旋转和平移。

1.相机标定

相机标定可以分步进行:

step 1:首先使用已知大小的棋盘图案定义3D点的真实世界坐标。
step 2:拍摄检查板图像的不同视角。
step 3:findChessboardCorners()是OpenCV中的一种方法,用于为不同图像中的每个3D点查找像素坐标(u,v)
step 4:然后使用CalibratCamera()方法查找相机参数

它将把我们计算出的(三点、两点、grayColor.shape[::-1]、None、None)作为参数,并返回包含相机矩阵、失真系数、旋转向量和平移向量等元素的列表。
Camera Matrix有助于将3D对象点转换为2D图像点,畸变系数返回相机在世界上的位置,以及旋转和平移矢量的值。

2.代码

# Import required modules 
import cv2 
import numpy as np 
import os 
import glob 


# Define the dimensions of checkerboard 
CHECKERBOARD = (6, 9) 


# stop the iteration when specified 
# accuracy, epsilon, is reached or 
# specified number of iterations are completed. 
criteria = (cv2.TERM_CRITERIA_EPS +
			cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 


# Vector for 3D points 
threedpoints = [] 

# Vector for 2D points 
twodpoints = [] 


# 3D points real world coordinates 
objectp3d = np.zeros((1, CHECKERBOARD[0] 
					* CHECKERBOARD[1], 
					3), np.float32) 
objectp3d[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 
							0:CHECKERBOARD[1]].T.reshape(-1, 2) 
prev_img_shape = None


# Extracting path of individual image stored 
# in a given directory. Since no path is 
# specified, it will take current directory 
# jpg files alone 
images = glob.glob('*.jpg') 

for filename in images: 
	image = cv2.imread(filename) 
	grayColor = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

	# Find the chess board corners 
	# If desired number of corners are 
	# found in the image then ret = true 
	ret, corners = cv2.findChessboardCorners( 
					grayColor, CHECKERBOARD, 
					cv2.CALIB_CB_ADAPTIVE_THRESH 
					+ cv2.CALIB_CB_FAST_CHECK +
					cv2.CALIB_CB_NORMALIZE_IMAGE) 

	# If desired number of corners can be detected then, 
	# refine the pixel coordinates and display 
	# them on the images of checker board 
	if ret == True: 
		threedpoints.append(objectp3d) 

		# Refining pixel coordinates 
		# for given 2d points. 
		corners2 = cv2.cornerSubPix( 
			grayColor, corners, (11, 11), (-1, -1), criteria) 

		twodpoints.append(corners2) 

		# Draw and display the corners 
		image = cv2.drawChessboardCorners(image, 
										CHECKERBOARD, 
										corners2, ret) 

	cv2.imshow('img', image) 
	cv2.waitKey(0) 

cv2.destroyAllWindows() 

h, w = image.shape[:2] 


# Perform camera calibration by 
# passing the value of above found out 3D points (threedpoints) 
# and its corresponding pixel coordinates of the 
# detected corners (twodpoints) 
ret, matrix, distortion, r_vecs, t_vecs = cv2.calibrateCamera( 
	threedpoints, twodpoints, grayColor.shape[::-1], None, None) 


# Displaying required output 
print(" Camera matrix:") 
print(matrix) 

print("\n Distortion coefficient:") 
print(distortion) 

print("\n Rotation Vectors:") 
print(r_vecs) 

print("\n Translation Vectors:") 
print(t_vecs) 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scott198512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值