opencv 摄像机标定的实现
程序员文章站
2022-06-15 16:58:31
原图矫正后我新建了个jz的文件夹放相机矫正所需要拍摄的图片,如下:共12张# coding:utf-8import cv2import numpy as npimport glob# 找棋盘格角点#...
原图
矫正后
我新建了个jz的文件夹放相机矫正所需要拍摄的图片,如下:共12张
# coding:utf-8 import cv2 import numpy as np import glob # 找棋盘格角点 # 阈值 criteria = (cv2.term_criteria_eps + cv2.term_criteria_max_iter, 30, 0.001) # 棋盘格模板规格 从0开始计算 w = 9 h = 6 # 世界坐标系中的棋盘格点,例如(0,0,0), (1,0,0), (2,0,0) ....,(8,5,0),去掉z坐标,记为二维矩阵 objp = np.zeros((w * h, 3), np.float32) objp[:, :2] = np.mgrid[0:w, 0:h].t.reshape(-1, 2) # 储存棋盘格角点的世界坐标和图像坐标对 objpoints = [] # 在世界坐标系中的三维点 imgpoints = [] # 在图像平面的二维点 # 匹配读取文件夹内的特定文件 images = glob.glob('jz/*.jpg') for fname in images: img = cv2.imread(fname) gray = cv2.cvtcolor(img, cv2.color_bgr2gray) # 找到棋盘格角点 ret, corners = cv2.findchessboardcorners(gray, (w, h), none) # 将角点在图像上显示 cv2.drawchessboardcorners(img, (w, h), corners, ret) cv2.imshow('findcorners', img) cv2.waitkey(500) cv2.destroyallwindows() # 如果找到足够点对,将其存储起来 if ret == true: cv2.cornersubpix(gray, corners, (11, 11), (-1, -1), criteria) objpoints.append(objp) imgpoints.append(corners) # 标定 ret, mtx, dist, rvecs, tvecs = cv2.calibratecamera(objpoints, imgpoints, gray.shape[::-1], none, none) # 在应用时,将下面两个写死 print(mtx) print(dist) # 去畸变 img2 = cv2.imread('77.jpg') h, w = img2.shape[:2] newcameramtx, roi = cv2.getoptimalnewcameramatrix(mtx, dist, (w, h), 0, (w, h)) # *比例参数 dst = cv2.undistort(img2, mtx, dist, none, newcameramtx) # 根据前面roi区域裁剪图片 # x,y,w,h = roi # dst = dst[y:y+h, x:x+w] cv2.imwrite('1.jpg', dst) cv2.imshow('findcorners', dst) cv2.waitkey(0) cv2.destroyallwindows()
到此这篇关于opencv 摄像机标定的实现的文章就介绍到这了,更多相关opencv 摄像机标定内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: Qt中网络编程的实现