抗畸变预览

This commit is contained in:
2025-12-19 10:41:59 +08:00
parent 8c0727990e
commit 2838e1097b
5 changed files with 98 additions and 114 deletions

View File

@@ -1,16 +1,34 @@
# 相机参考工具
# @路怀帅 2025.12.19 10:32am
# PS : 新增 加载相机畸变KD 视频对比显示功能
import cv2
import threading
import sys
from datetime import datetime
import os
import argparse
import numpy as np
# 全局变量
frame = None
running = True
which_camera = 0
# 加载鱼眼相机参数
K = np.array([[5.3402108990030604e+02, 0., 9.2598444295282172e+02],
[0., 5.3455325152709827e+02, 5.7771767919091610e+02],
[0., 0., 1.]])
D = np.array([-1.8724887233075402e-02, 6.4408558584901701e-03,
-5.2069636709412993e-03, 8.4815411645490968e-04])
W, H = 1920, 1080
def video_thread():
global frame, running
cap = cv2.VideoCapture(1, cv2.CAP_ANY)
cap = cv2.VideoCapture(which_camera, cv2.CAP_ANY)
cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*"YUYV"))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
@@ -20,19 +38,33 @@ def video_thread():
print("[ERROR] Cannot open camera", file=sys.stderr)
running = False
return
# 鱼眼相机去畸变
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, (W, H), cv2.CV_16SC2)
while running:
ret, f = cap.read()
if not ret:
break
frame = f.copy()
# print(frame.shape)
# frame = cv2.resize(frame, (1280, 720))
# print(frame.shape)
cv2.namedWindow('AHD Video', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('AHD Video', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow('AHD Video', f)
# cv2.showFullscreen('Live Feed (Local Display)', f)
# 图像去畸变
undistorted = cv2.remap(f, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
# 拼接原视频和抗畸变后的视频 (左右显示)
comparison = np.hstack((f, undistorted))
scale = min(1920 / comparison.shape[1], 1080 / comparison.shape[0])
if scale < 1:
comparison = cv2.resize(comparison, (int(comparison.shape[1] * scale), int(comparison.shape[0] * scale)))
comparison = comparison.astype(np.uint8)
# 设置视频流全屏显示
text_info = f"Camera: {args.i.upper()} | Press 'q' to quit, 's' to screenshot"
cv2.putText(comparison, text_info, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA)
cv2.namedWindow('Video old vs new', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('Video old vs new', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow('Video old vs new', comparison)
if cv2.waitKey(1) & 0xFF == ord('q'):
running = False
break
@@ -62,6 +94,30 @@ def input_thread():
break
if __name__ == "__main__":
# 获取用户参数启动
parser = argparse.ArgumentParser(description="Example script with argparse")
# 获取用户输入的摄像头方位 front back left right
parser.add_argument("--i", type=str, required=True, help="摄像头方位")
args = parser.parse_args()
print("相机方位:", args.i)
if args.i == "front":
which_camera = 0
elif args.i == "back":
which_camera = 2
elif args.i == "left":
which_camera = 1
elif args.i == "right":
which_camera = 3
else:
print("[ERROR] Invalid camera direction. Use 'front', 'back', 'left', or 'right'.", file=sys.stderr)
running = False
exit(1)
# 启动视频线程
vt = threading.Thread(target=video_thread, daemon=True)
vt.start()

View File

@@ -1,37 +1,19 @@
%YAML:1.0
---
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 3.0434907840374234e+02, 0., 4.8133979392511606e+02, 0.,
3.2477726176795460e+02, 3.1646476882040702e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: d
data: [ -4.1568299226312187e-02, 3.1480645089822291e-03,
-2.3982702848139551e-03, 2.3821781880039081e-05 ]
resolution: !!opencv-matrix
rows: 2
cols: 1
dt: i
data: [ 960, 640 ]
project_matrix: !!opencv-matrix
data: [ 1920, 1080 ]
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -9.3070874510219026e-01, -4.1405550648825917e+00,
1.1216126052183415e+03, 1.3669740891976151e-01,
-4.6651507085268138e+00, 1.0607568570787648e+03,
2.8474752086329793e-04, -6.8625514942363052e-03, 1. ]
scale_xy: !!opencv-matrix
rows: 2
data: [ 5.3042094228516248e+02, 0., 9.5131550663475389e+02, 0.,
5.3026569308499541e+02, 5.3695007291032755e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: f
data: [ 8.00000012e-01, 1. ]
shift_xy: !!opencv-matrix
rows: 2
cols: 1
dt: f
data: [ 0., 100. ]
dt: d
data: [ -9.2062464083377798e-03, -6.3620029090856196e-03,
3.3735324773401221e-03, -1.0289639180500810e-03 ]

View File

@@ -1,37 +1,19 @@
%YAML:1.0
---
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 3.0245305983229298e+02, 0., 4.9664001463163459e+02, 0.,
3.2074618594392325e+02, 3.3119980984361649e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: d
data: [ -4.3735601598704078e-02, 2.1692522970939803e-02,
-2.6388839028513571e-02, 8.4123126605702321e-03 ]
resolution: !!opencv-matrix
rows: 2
cols: 1
dt: i
data: [ 1920, 1080 ]
project_matrix: !!opencv-matrix
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -7.0390891066994388e-01, -2.5544083216952904e+00,
7.0809808916259806e+02, -2.9600383808093766e-01,
-2.4971504395791286e+00, 6.3578234365104447e+02,
-5.6872782515522376e-04, -4.4482832729892769e-03, 1. ]
scale_xy: !!opencv-matrix
rows: 2
data: [ 5.3382445956203196e+02, 0., 9.7253025945442369e+02, 0.,
5.3393792084343488e+02, 5.6249605531924215e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: f
data: [ 6.99999988e-01, 8.00000012e-01 ]
shift_xy: !!opencv-matrix
rows: 2
cols: 1
dt: f
data: [ -150., -100. ]
dt: d
data: [ -1.5749135021037808e-02, 2.9390620422222835e-03,
-4.3176357910129585e-03, 1.3296605027646462e-03 ]

View File

@@ -1,37 +1,19 @@
%YAML:1.0
---
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 3.0334009006384287e+02, 0., 4.8649280066241465e+02, 0.,
3.2229678244636966e+02, 3.2388095214561167e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: d
data: [ -3.5510560636666778e-02, -1.9848228876245811e-02,
2.6080053057044101e-02, -9.7183762742328750e-03 ]
resolution: !!opencv-matrix
rows: 2
cols: 1
dt: i
data: [ 1920, 1080 ]
project_matrix: !!opencv-matrix
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -1.5134401274988541e+01, -4.2169445692489937e+01,
1.0724381138361938e+04, -6.7381376780590163e-01,
-2.9044610322524363e+01, 4.1490641432000384e+03,
4.9428170463677217e-04, -4.7489470989931934e-02, 1. ]
scale_xy: !!opencv-matrix
rows: 2
data: [ 5.3576790643136087e+02, 0., 9.5266112763930198e+02, 0.,
5.3494293886479875e+02, 5.4089729625135715e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: f
data: [ 4.00000006e-01, 8.00000012e-01 ]
shift_xy: !!opencv-matrix
rows: 2
cols: 1
dt: f
data: [ 150., 0. ]
dt: d
data: [ -1.1422407725638895e-02, -1.2103818796148216e-02,
9.0774770002077006e-03, -2.8278270352926444e-03 ]

View File

@@ -1,37 +1,19 @@
%YAML:1.0
---
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 3.0245305983229298e+02, 0., 4.9664001463163459e+02, 0.,
3.2074618594392325e+02, 3.3119980984361649e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: d
data: [ -4.3735601598704078e-02, 2.1692522970939803e-02,
-2.6388839028513571e-02, 8.4123126605702321e-03 ]
resolution: !!opencv-matrix
rows: 2
cols: 1
dt: i
data: [ 1920, 1080 ]
project_matrix: !!opencv-matrix
camera_matrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -7.0390891066994388e-01, -2.5544083216952904e+00,
7.0809808916259806e+02, -2.9600383808093766e-01,
-2.4971504395791286e+00, 6.3578234365104447e+02,
-5.6872782515522376e-04, -4.4482832729892769e-03, 1. ]
scale_xy: !!opencv-matrix
rows: 2
data: [ 5.3402108990030604e+02, 0., 9.2598444295282172e+02, 0.,
5.3455325152709827e+02, 5.7771767919091610e+02, 0., 0., 1. ]
dist_coeffs: !!opencv-matrix
rows: 4
cols: 1
dt: f
data: [ 6.99999988e-01, 8.00000012e-01 ]
shift_xy: !!opencv-matrix
rows: 2
cols: 1
dt: f
data: [ -150., -100. ]
dt: d
data: [ -1.8724887233075402e-02, 6.4408558584901701e-03,
-5.2069636709412993e-03, 8.4815411645490968e-04 ]