图像去畸变(python)
功能:图像去除畸变并将视频保存成图片保存。
·
功能:鱼眼图像去除畸变并将视频保存成图片保存。
video2image.py如下
import os
import cv2
import csv
import numpy as np
#scale数值可以防止去畸变后,fov过小
def undistortRectifyMap(K,D,DIM,scale=1.0):
Knew = K.copy()
if scale:#change fov
Knew[(0,1), (0,1)] = scale * Knew[(0,1), (0,1)]
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), Knew, DIM, cv2.CV_16SC2)
return map1, map2
def undistort(img, map1, map2, DIM, imshow=False):
dim1 = img.shape[:2][::-1]
assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"
if dim1[0]!=DIM[0]:
img = cv2.resize(img,DIM,interpolation=cv2.INTER_AREA)
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
if imshow:
cv2.imshow("undistorted", undistorted_img)
return undistorted_img
def AVI_To_Img_And_save(video_file, map1, map2 ,DIM):
video_name = video_file.split('/')[-1].split('.')[0]
print('当前处理的视频为:',video_file)
cap = cv2.VideoCapture(video_file)
rval = cap.isOpened()
framenum=0
print(rval)
while rval:
rval, frame = cap.read()
if framenum%25!=0:
framenum+=1
continue
print('1')
save_dir = './'
Img_savename = save_dir + video_name + '_' + '%08d'%framenum +'.jpg'
if rval:
if os.path.exists(Img_savename)==False:
img = undistort(frame, map1, map2, DIM)
cv2.imwrite(Img_savename, img,[int(cv2.IMWRITE_JPEG_QUALITY), 100])
else:
break
framenum+=1
print('正在处理视频{}的第{}帧,rval = {}...'.format(video_name,framenum,rval))
if __name__ == '__main__':
DIM=(1920, 1080)
K=np.array([[9.5749393532104671e+02, 0., 1.0143950223349893e+03], [0., 9.5697913744394737e+02, 5.4154074349276050e+02], [0.0, 0.0, 1.0]])
D=np.array([[-3.4795012656299869e-02], [-9.6299582609726160e-03], [3.9250075601799878e-03], [-8.2559708738055783e-04]])
map1, map2 = undistortRectifyMap(K,D,DIM,0.9)
AVI_To_Img_And_save("/media/data/zs/project/天准车机/caiji/result2.avi", map1, map2, DIM)
标定文件如下:
calib.yaml
%YAML:1.0
---
CameraNum: 1
CameraModel: OPENCV_FISHEYE
CalibMode: CAMERA_SINGLE
K0: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.5749393532104671e+02, 0., 1.0143950223349893e+03, 0.,
9.5697913744394737e+02, 5.4154074349276050e+02, 0., 0., 1. ]
D0: !!opencv-matrix
rows: 4
cols: 1
dt: d
data: [ -3.4795012656299869e-02, -9.6299582609726160e-03,
3.9250075601799878e-03, -8.2559708738055783e-04 ]
ImageSize0: [ 1920, 1080 ]
R: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -1.6918078081605778e-01, 1.3851567051419672e-03,
-9.8558406274826371e-01, 9.8557019727130768e-01,
5.7251557764026349e-03, -1.6917035450082124e-01,
5.4082948391265054e-03, -9.9998265181564017e-01,
-2.3337554916848433e-03 ]
T: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 2.9429394656648822e+00, 8.2697708602684283e-01,
-2.9463223695744905e+01 ]
标定图像如下:
更多推荐
已为社区贡献6条内容
所有评论(0)