如何从校准的摄像机流中显示正确的视频图像?

时间:2019-06-11 08:42:59

标签: python opencv camera-calibration fisheye

我最近学习了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()

我得到了裁剪和变形的图像

1 个答案:

答案 0 :(得分:1)

向所有人寻求帮助! 我这样解决了我的问题: 我意识到相机的图像分辨率不正确。我刚刚添加了cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720),就可以了!