pythonopencvcamera-calibrationfisheye

How to show the correct video-image from calibrated camera stream?


I recently studied OpenCV. My task: to display a calibrated image from an action camera. I calibrated the camera. I can easily get a good calibrated image.

#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()

but when I try to work with frames in the video, I get a cropped image on the output. I don't understand what I'm doing wrong.

#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()

I get a cropped and distorted image


Solution

  • Thx all for help! I solved my problem this way: I realized that the camera image was in the wrong resolution. I just added cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) and it worked!