如何从校准的摄像机流中显示正确的视频图像?
How to show the correct video-image from calibrated camera stream?
最近在研究OpenCV。我的任务:显示来自运动相机的校准图像。我校准了相机。我可以轻松获得良好的校准图像。
#CODE TO WORK WITH IMAGE
import numpy as np
import cv2
import sys
#Matrix
DIM = (1280,720)
K = np.array([[670.6687634787847, 0.0, 625.8352066309077], [0.0, 665.8169620465114, 349.9286858249417], [0.0, 0.0, 1.0]])
D = np.array([[-0.01833489984490284], [0.12136347203846999], [-0.4637418712120781], [0.5817376362743433]])
img = cv2.imread("C:\Test\Fish_eye_remove\Test.jpg")
h, w = img.shape[:2]
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
cv2.imshow("undistorted", undistorted_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是当我尝试处理视频中的帧时,我在输出中得到了裁剪图像。我不明白我做错了什么。
#CODE TO WORK WITH VIDEO-IMAGE
import numpy as np
import cv2
DIM = (1280,720)
K = np.array([[670.6687634787847, 0.0, 625.8352066309077], [0.0, 665.8169620465114, 349.9286858249417], [0.0, 0.0, 1.0]])
D = np.array([[-0.01833489984490284], [0.12136347203846999], [-0.4637418712120781], [0.5817376362743433]])
cap = cv2.VideoCapture(0)
while True:
flag, img = cap.read()
try:
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
calibrated = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
cv2.imshow('result', calibrated)
except:
cap.release()
raise
k = cv2.waitKey(30)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
我得到了裁剪和扭曲的图像
谢谢大家的帮助!
我这样解决了我的问题:
我意识到相机图像的分辨率错误。我刚刚添加了 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
并且成功了!
最近在研究OpenCV。我的任务:显示来自运动相机的校准图像。我校准了相机。我可以轻松获得良好的校准图像。
#CODE TO WORK WITH IMAGE
import numpy as np
import cv2
import sys
#Matrix
DIM = (1280,720)
K = np.array([[670.6687634787847, 0.0, 625.8352066309077], [0.0, 665.8169620465114, 349.9286858249417], [0.0, 0.0, 1.0]])
D = np.array([[-0.01833489984490284], [0.12136347203846999], [-0.4637418712120781], [0.5817376362743433]])
img = cv2.imread("C:\Test\Fish_eye_remove\Test.jpg")
h, w = img.shape[:2]
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
cv2.imshow("undistorted", undistorted_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是当我尝试处理视频中的帧时,我在输出中得到了裁剪图像。我不明白我做错了什么。
#CODE TO WORK WITH VIDEO-IMAGE
import numpy as np
import cv2
DIM = (1280,720)
K = np.array([[670.6687634787847, 0.0, 625.8352066309077], [0.0, 665.8169620465114, 349.9286858249417], [0.0, 0.0, 1.0]])
D = np.array([[-0.01833489984490284], [0.12136347203846999], [-0.4637418712120781], [0.5817376362743433]])
cap = cv2.VideoCapture(0)
while True:
flag, img = cap.read()
try:
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
calibrated = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
cv2.imshow('result', calibrated)
except:
cap.release()
raise
k = cv2.waitKey(30)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
我得到了裁剪和扭曲的图像
谢谢大家的帮助!
我这样解决了我的问题:
我意识到相机图像的分辨率错误。我刚刚添加了 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
并且成功了!