张正友相机标定(含python代码)笔记

阅读: 评论:0

张正友相机标定(含python代码)笔记张正友相机标定(含python代码)笔记
import cv2
import sys
import numpy as np
image = np.ones([1080,1920,3], np.uint8)*255
x_nums =14
y_nums =7
square_pixel =120# 1080/9 = 120 pixels
x0 = square_pixel
y0 = square_pixel
def DrawSquare():
flag =-1
for i in range(y_nums):
flag =0- flag
for j in range(x_nums):
if flag >0:
color =[0,0,0]
else:
color =[255,255,255]
(x0 + j*square_pixel+square_pixel,y0 + i*square_pixel+square_pixel),color,-1)
flag =0- flag
cv2.imwrite('D:/chess_map_14x7.bmp',image)
if __name__ =='__main__':
DrawSquare()
2.相机标定
import cv2
import numpy as np
import glob
#criteria:⾓点精准化迭代过程的终⽌条件
criteria =(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,30,0.001)
#棋盘格模板规格
len=18#⿊⽩格长度
w =6
h =13
# 世界坐标系中的棋盘格点,例如(0,0,0), (1,0,0), (2,0,0) ....,(8,5,0),去掉Z坐标,记为⼆维矩阵
world_point = np.zeros((w*h,3), np.float32)#初始化⼀个6*13⾏3列的矩阵,类型为float
#把每⼀个世界坐标的xy赋值[:,:2]中,:表⽰列表的所有字列表全部,:2表⽰从1截⽌到第⼆个数字
world_point[:,:2]= np.mgrid[0:w*len:len,0:h*len:len].T.reshape(-1,2)
# 储存棋盘格⾓点的世界坐标和图像坐标对
world_points =[]# 在世界坐标系中的三维点
imgpoints =[]# 在图像平⾯的⼆维点
j=1
images = glob.glob('*.jpg')#读取所有jpg⽂件
for fname in images:
img = cv2.imread(fname)
#将图⽚缩⼩
img = size(img,None,fx=0.4, fy=0.4, interpolation = cv2.INTER_CUBIC)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 到棋盘格⾓点
菲律宾的首都
#寻⾓点,存⼊corners,ret是到⾓点的flag(如果到⾓点则为true)
ret, corners = cv2.findChessboardCorners(gray,(w, h),None)
# 如果到⾜够点对,将其存储起来
if ret is True:
#加⼊到世界坐标系
world_points.append(world_point)
#⾓点加⼊到图像坐标系
imgpoints.append(corners)
# 将⾓点在图像上显⽰
cv2.drawChessboardCorners(img,(w, h), corners, ret)
cv2.imshow('findCorners', img)
cv2.waitKey(1)
print(str(j)+"完成\n")
j+=1
else:
print('错误')
j+=1
cv2.destroyAllWindows()
#求解摄像机的内在参数和外在参数。mtx 内参数矩阵,dist 畸变系数,rvecs 旋转向量,tvecs 平移向量
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(world_points, imgpoints, gray.shape[::-1],None,None)
# 棋盘格⾓点
# 去畸变
img2 = cv2.imread('1.jpg')
img2= size(img2,None,fx=0.4, fy=0.4, interpolation = cv2.INTER_CUBIC)
cv2.imshow("畸变图像", img2)
cv2.waitKey(100)
cv2.destroyAllWindows()
h,  w = img2.shape[:2]
#内参数矩阵
newcameramtx, roi = OptimalNewCameraMatrix(mtx, dist,(w, h),0,(w, h))# ⾃由⽐例参数
#校正后图像
dst = cv2.undistort(img2, mtx, dist,None, newcameramtx)
cv2.imwrite('calibresult.png', dst)
f =open('内参数矩阵.txt','w+')
乐和乐都攻略
f.write('内参数矩阵:\n'+str(newcameramtx)+'\n')
f.close()
print('内参数输出完毕')
f =open('外参数矩阵.txt','w+')
#得到旋转矩阵与平移尼泊尔国旗
for t in range(0,j-1):#向量变为矩阵
newrvecs=cv2.Rodrigues(rvecs[t].ravel())
newtvecs=cv2.Rodrigues(tvecs[t].ravel())
f.write('第'+str(t+1)+'张图像的外参数矩阵:\n'+'(1)旋转矩阵:\n'+str(newrvecs[0])+'\n'+'(2)平移矩阵:\n'+str(newtvecs[0])+'\n')    t+=1
f.close()
print('外参数输出完毕')
f =open('畸变系数.txt','w+')
f.write('畸变系数:\n'+str(dist)+'\n')
f.close()
print('结果输出完毕')
np.savez('data.npz', mtx= mtx, dist= dist,rvecs=rvecs,tvecs=tvecs)
print('*.npz⽂件输出完毕')
3.画三维坐标轴与⽴⽅体
import cv2
北京故宫详细介绍import numpy as np
import glob
#加载数据
with np.load('data.npz')as X:
#加载上⼀部⽣成的参数
mtx, dist, _, _=[X[i]for i in('mtx','dist','rvecs','tvecs')]
#criteria:⾓点精准化迭代过程的终⽌条件
criteria =(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,30,0.001)
#棋盘格模板规格
len=18#⿊⽩格长度
w =6
h =13
h =13
#画坐标轴和⽴⽅体
def draw(img, corners, imgpts,imgpts2):
corner=tuple(corners[0].ravel())#ravel()⽅法将数组维度拉成⼀维数组
# img要画的图像,corner起点,tuple终点,颜⾊,粗细
img= cv2.line(img, corner,tuple(imgpts2[0].ravel()),(255,0,0),8)
img= cv2.line(img, corner,tuple(imgpts2[1].ravel()),(0,255,0),8)
img= cv2.line(img, corner,tuple(imgpts2[2].ravel()),(0,0,255),8)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'X',tuple(imgpts2[0].ravel()+2), font,1,(255,0,0),2,cv2.LINE_AA)
cv2.putText(img,'Y',tuple(imgpts2[1].ravel()+2), font,1,(0,255,0),2,cv2.LINE_AA)
cv2.putText(img,'Z',tuple(imgpts2[2].ravel()+2), font,1,(0,0,255),2,cv2.LINE_AA)
imgpts= np.int32(imgpts).reshape(-1,2)#draw ground floor in green
for i,j in zip(range(4),range(4,8)):#正⽅体顶点逐个连接
img= cv2.line(img,tuple(imgpts[i]),tuple(imgpts[j]),(255,215,0),3)#draw top layer in red color
#imgpts[4:]是⼋个顶点中上⾯四个顶点
#imgpts[:4]是⼋个顶点中下⾯四个顶点
#⽤函数drawContours画出上下两个盖⼦,它的第⼀个参数是原始图像,第⼆个参数是轮廓,⼀个python列表,第三个参数是轮廓的索引(当设置为-1时绘制所有轮廓)
img = cv2.drawContours(img,[imgpts[4:]],-1,(255,215,0),3)
img = cv2.drawContours(img,[imgpts[:4]],-1,(255,215,0),3)
return img
objp= np.zeros((w*h,3), np.float32)
objp[:,:2]= np.mgrid[0:w*len:len,0:h*len:len].T.reshape(-1,2)
axis = np.float32([[0,0,0],[0,2*len,0],[2*len,2*len,0],[2*len,0,0],
[0,0,-2*len],[0,2*len,-2*len],[2*len,2*len,-2*len],[2*len,0,-2*len]])
axis2= np.float32([[3*len,0,0],[0,3*len,0],[0,0,-3*len]]).reshape(-1,3)
images = glob.glob('*.jpg')
i=1;
for fname in images:
img = cv2.imread(fname)
img = size(img,None,fx=0.4, fy=0.4, interpolation = cv2.INTER_CUBIC)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 到棋盘格⾓点
#寻⾓点,存⼊corners,ret是到⾓点的flag
ret, corners = cv2.findChessboardCorners(gray,(w, h),None)
if ret is True:
corners2= SubPix(gray,corners,(11,11),(-1,-1),criteria)
乐山大佛峨眉山旅游攻略
#求解物体位姿的需要
_,rvecs, tvecs, inliers =cv2.solvePnPRansac(objp, corners2, mtx, dist)
#projectPoints()根据所给的3D坐标和已知的⼏何变换来求解投影后的2D坐标。
#imgpts是整体的8个顶点
imgpts,_=cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
#imgpts2是三个坐标轴的x,y,z划线终点
imgpts2, _ =cv2.projectPoints(axis2, rvecs, tvecs, mtx, dist)
神农架景点地图img=draw(img,corners2,imgpts,imgpts2)
cv2.imshow('世界坐标系与⼩盒⼦',img)
cv2.imwrite(str(i)+'.png', img)
cv2.waitKey(1000)
i+=1;
cv2.destroyAllWindows()
print("完毕")
六、实验结果

本文发布于:2023-05-26 12:41:00,感谢您对本站的认可!

本文链接:http://www.035400.com/whly/2/437089.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   矩阵   图像   棋盘   求解   坐标   坐标系   顶点
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2024-2030 Comsenz Inc.Powered by © 文化旅游网 滇ICP备2022007236号-403 联系QQ:1103060800网站地图