当前位置:网站首页>【雙目視覺】雙目矯正
【雙目視覺】雙目矯正
2022-07-02 07:48:00 【寂雲蕭】
一、雙目標定
雙目標定需要獲取到兩個相機的內參以及變換矩陣。可參照鏈接:
https://blog.csdn.net/qq_38236355/article/details/89280633
https://blog.csdn.net/qingfengxiaosong/article/details/109897053
或者自行百度
建議使用Matlab工具箱做標定,其中建議勾選3 Coefficients。
輸出Matlab的數據之後,可用一下脚本提取數據:
rowName = cell(1,10);
rowName{1,1} = '平移矩陣';
rowName{1,2} = '旋轉矩陣';
rowName{1,3} = '相機1內參矩陣';
rowName{1,4} = '相機1徑向畸變';
rowName{1,5} = '相機1切向畸變';
rowName{1,6} = '相機2內參矩陣';
rowName{1,7} = '相機2徑向畸變';
rowName{1,8} = '相機2切向畸變';
rowName{1,9} = '相機1畸變向量';
rowName{1,10} = '相機2畸變向量';
xlswrite('out.xlsx',rowName(1,1),1,'A1');
xlswrite('out.xlsx',rowName(1,2),1,'A2');
xlswrite('out.xlsx',rowName(1,3),1,'A5');
xlswrite('out.xlsx',rowName(1,4),1,'A8');
xlswrite('out.xlsx',rowName(1,5),1,'A9');
xlswrite('out.xlsx',rowName(1,6),1,'A10');
xlswrite('out.xlsx',rowName(1,7),1,'A13');
xlswrite('out.xlsx',rowName(1,8),1,'A14');
xlswrite('out.xlsx',rowName(1,9),1,'A15');
xlswrite('out.xlsx',rowName(1,10),1,'A16');
xlswrite('out.xlsx',stereoParams.TranslationOfCamera2,1,'B1'); % 平移矩陣
xlswrite('out.xlsx',stereoParams.RotationOfCamera2.',1,'B2'); % 旋轉矩陣
xlswrite('out.xlsx',stereoParams.CameraParameters1.IntrinsicMatrix.',1,'B5'); % 相機1內參矩陣
xlswrite('out.xlsx',stereoParams.CameraParameters1.RadialDistortion,1,'B8'); % 相機1徑向畸變(1,2,5)
xlswrite('out.xlsx',stereoParams.CameraParameters1.TangentialDistortion,1,'B9'); % 相機1切向畸變(3,4)
xlswrite('out.xlsx',stereoParams.CameraParameters2.IntrinsicMatrix.',1,'B10'); % 相機2內參矩陣
xlswrite('out.xlsx',stereoParams.CameraParameters2.RadialDistortion,1,'B13'); % 相機2徑向畸變(1,2,5)
xlswrite('out.xlsx',stereoParams.CameraParameters2.TangentialDistortion,1,'B14'); % 相機2切向畸變(3,4)
xlswrite('out.xlsx',[stereoParams.CameraParameters1.RadialDistortion(1:2), stereoParams.CameraParameters1.TangentialDistortion,...
stereoParams.CameraParameters1.RadialDistortion(3)],1,'B15'); % 相機1畸變向量
xlswrite('out.xlsx',[stereoParams.CameraParameters2.RadialDistortion(1:2), stereoParams.CameraParameters2.TangentialDistortion,...
stereoParams.CameraParameters2.RadialDistortion(3)],1,'B16'); % 相機2畸變向量
Python雙目矯正
新建一個python脚本,輸入以下代碼:
import cv2
import numpy as np
# 左目內參
left_camera_matrix = np.array([[443.305413261701, 0., 473.481578105186],
[0., 445.685585080218, 481.627083907456],
[0., 0., 1.]])
#左目畸變
#k1 k2 p1 p2 k3
left_distortion = np.array([[-0.261575534517449, 0.0622298171820726, 0., 0., -0.00638628534161724]])
# 右目內參
right_camera_matrix = np.array([[441.452616156177,0., 484.276702473006],
[0., 444.350924943458, 465.054536507021],
[0., 0., 1.]])
# 右目畸變
right_distortion = np.array([[-0.257761221642368, 0.0592089672793365, 0., 0., -0.00576090991058531]])
# 旋轉矩陣
R = np.matrix([
[0.999837210893742, -0.00477934325693493, 0.017398551383822],
[0.00490062605211919, 0.999963944810228, -0.0069349076319899],
[-0.0173647797717217, 0.00701904249875521, 0.999824583347439]
])
# 平移矩陣
T = np.array([-71.0439056359403, -0.474467959947789, -0.27989811881883]) # 平移關系向量
size = (960, 960) # 圖像尺寸
# 進行立體更正
R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(left_camera_matrix, left_distortion,
right_camera_matrix, right_distortion, size, R,
T)
# 計算更正map
left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion, R1, P1, size, cv2.CV_16SC2)
right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion, R2, P2, size, cv2.CV_16SC2)
參數需要換成自己實際的參數。
接下來隨便寫一個脚本測試一下更正結果:
import cv2
import numpy as np
import camera_config
w = 1920
h = 960
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
key = ""
ww = int(w/2)
jiange = int(h/10)
while key!=27:
ret, img = cap.read()
if ret:
imgLeft = img[:, :ww]
imgRight = img[:, ww:w]
left_remap = cv2.remap(imgLeft, camera_config.left_map1, camera_config.left_map2, cv2.INTER_LINEAR)
right_remap = cv2.remap(imgRight, camera_config.right_map1, camera_config.right_map2, cv2.INTER_LINEAR)
out = np.hstack([left_remap, right_remap])
for i in range(10):
cv2.line(out, (0, jiange*i), (w, jiange*i), (255, 0, 0), 2)
cv2.imshow("frame", out)
key = cv2.waitKey(10)
cap.release()
cv2.destroyAllWindows()
即可看到效果:
校正前(很差的相機,魚眼效果,不適合用於實際使用):
校正後:
边栏推荐
- 【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
- [introduction to information retrieval] Chapter 6 term weight and vector space model
- The difference and understanding between generative model and discriminant model
- Calculate the difference in days, months, and years between two dates in PHP
- What if the laptop can't search the wireless network signal
- Machine learning theory learning: perceptron
- 【多模态】CLIP模型
- 【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
- 解决latex图片浮动的问题
- 超时停靠视频生成
猜你喜欢
【Random Erasing】《Random Erasing Data Augmentation》
常见CNN网络创新点
Faster-ILOD、maskrcnn_ Benchmark installation process and problems encountered
[paper introduction] r-drop: regulated dropout for neural networks
iOD及Detectron2搭建过程问题记录
【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
[introduction to information retrieval] Chapter II vocabulary dictionary and inverted record table
How do vision transformer work?【论文解读】
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
随机推荐
自然辩证辨析题整理
Drawing mechanism of view (I)
How to turn on night mode on laptop
win10解决IE浏览器安装不上的问题
【Cascade FPD】《Deep Convolutional Network Cascade for Facial Point Detection》
Faster-ILOD、maskrcnn_ Benchmark training coco data set and problem summary
程序的内存模型
【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
yolov3训练自己的数据集(MMDetection)
Yolov3 trains its own data set (mmdetection)
PointNet原理证明与理解
[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image
Delete the contents under the specified folder in PHP
What if a new window always pops up when opening a folder on a laptop
【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
[model distillation] tinybert: distilling Bert for natural language understanding
传统目标检测笔记1__ Viola Jones
Tencent machine test questions
【Programming】
Calculate the total in the tree structure data in PHP