import cv2 import numpy as np import os # 相機內參矩陣,假設為K K = np.array([[1451.7946523730436, 0, 960], [ 0, 1438.2609968095967, 540], [ 0, 0, 1]]) # 畸變係數,假設為D D = np.array([-0.0042837100252329525, -0.0016247045538291293, -0.0023595319957707256, 0.00036597253136795013]) # 去畸變函式 # 注意:cv2.undistort()函式的引數順序是:src, K, D, newCameraMatrix=None, roi=None # 其中newCameraMatrix是可選的,用於指定新的相機內參矩陣,如果設定為None,則使用原始的K # roi是可選的,用於指定輸出影像的感興趣區域 image_dir = '/home/dongdong/2project/0data/house3/120/distort_images' undistort_image_dir = '/home/dongdong/2project/0data/house3/120/undistort_images' os.makedirs(undistort_image_dir,exist_ok=True) for i in range(1,194): image = cv2.imread(os.path.join(image_dir,'{:>04}.jpg'.format(i))) undistorted_image = cv2.undistort(image, K, D, None, None) cv2.imwrite(os.path.join(undistort_image_dir,'{:>04}.jpg'.format(i)),undistorted_image)