0

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

Dmitriy D
  • 15
  • 4
  • "I can easily get a good calibrated image, but when I try to work with frames in the video...". Add code that worked for you please. – Oliort UA Jun 11 '19 at 09:12
  • @Oliort I added it in my question. You can see it now – Dmitriy D Jun 11 '19 at 09:24
  • was the test undistorted image taken with same camera? what the dimensions of it – chris Jun 11 '19 at 09:29
  • @chris yeah, test image was taken from same camera. Resolution is 1280x720 – Dmitriy D Jun 11 '19 at 09:37
  • if you use the VideoCapture once in the working code instead of loading an offline image, does it still work? Maybe the cam is capturing in wrong image size? Btw. in the live mode you should initialize the maps only once! – Micka Jun 11 '19 at 09:45
  • can you test and print the dimensions of img? – Micka Jun 11 '19 at 09:47
  • @Micka I tested my code with offline video-image, and it works correctly. I'm completely confused – Dmitriy D Jun 11 '19 at 10:09
  • 1
    @Micka I just added CAP_PROP_FRAME for camera dimensions and it worked! thx 4 help! – Dmitriy D Jun 11 '19 at 10:19

1 Answers1

1

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!

Dmitriy D
  • 15
  • 4