#用的github上的张正友标定法,下面是获取1080p图像的程序
#运行指令:python3 calibration.py --image_size 1920x1080 --mode calibrate --corner 8x6 --square 20
import cv2
# 打开摄像头或视频文件
cap = cv2.VideoCapture(0) # 0表示默认摄像头,你也可以指定视频文件路径
# 设置摄像头的分辨率为1080p
cap.set(3, 1920) # 设置帧宽度
cap.set(4, 1080) # 设置帧高度
# 设置保存帧的间隔(例如,每秒保存一帧)
frame_interval = 30 # 这将每隔30帧保存一次,约等于每秒保存一帧
frame_count = 0 # 用于计数帧
while True:
ret, frame = cap.read() # 读取一帧图像
if not ret:
break
frame_count += 1
# 保存每秒的其中一帧
if frame_count % frame_interval == 0:
cv2.imwrite(f'frame_{frame_count}.jpg', frame)
cv2.imshow('Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
#根据图像进行校正并获取相机内外参数 这个版本最后没用
import cv2
import numpy as np
# 读取标定图像
image_paths = ["image1.jpg", "image2.jpg", ...] # 替换为你的标定图像路径
images = [cv2.imread(path) for path in image_paths]
# 相机分辨率(像素)
image_width = 1920
image_height = 1080
# 棋盘格尺寸(7x10的格子,每个格子实际物理尺寸是20x20毫米)
chessboard_size = (7, 10)
square_size_mm = 22.0 # 每个方格的实际物理尺寸(毫米)
# 创建对象点列表,坐标在实际世界中定义的平面
objp = np.zeros((chessboard_size[0] * chessboard_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2) * square_size_mm
# 存储角点和对象点
objpoints = [] # 对象点
imgpoints = [] # 图像点
for image in images:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
if ret:
objpoints.append(objp)
imgpoints.append(corners)
# 标定相机
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (image_width, image_height), None, None)
# 输出内外参数
print("相机内参数(Camera Matrix):\n", mtx)
print("镜头畸变参数(Distortion Coefficients):\n", dist)
print("旋转向量(Rotation Vectors):\n", rvecs)
print("平移向量(Translation Vectors):\n", tvecs)