Error while playing stored Video using OpenCV + Python
Hello,
I'm using Opencv 3.2 + Python on ubuntu 16.04 LTS OS. I'm trying to undistort the fisheye video which is collected from web cam. Below is my code where I'm undistorting the fisheye video by undistorting frame by frame. The program is executing fine and its undistorting fisheye video correctly but when I try to play the stored undistorted video (undistortedop.avi) it gives a error message saying "Could not demultiplex stream". Could anyone please help me to solve this problem..?
import numpy as np
import os
import glob
import sys
assert float(cv2.__version__.rsplit('.', 1)[0]) >= 3, 'OpenCV version 3 or newer required.'
DIM=(1280, 720)
K=np.array([[517.7167401534203, 0.0, 641.312338873659], [0.0, 518.0410707880329, 361.1273127787553], [0.0, 0.0, 1.0]])
D=np.array([[-0.00428080929837007], [-0.14786471866085527], [0.07941291495275071], [-0.025649243686649097]])
balance=0.95
dim2=None
dim3=None
cap = cv2.VideoCapture(0)
Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out1 = cv2.VideoWriter('undistortedop.avi',fourcc, 20.0, (640,480))
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# img = cv2.imread(img_path)
# dim1 = img.shape[:2][::-1] #dim1 is the dimension of input image to un-distort
dim1=(1280, 720)
assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"
if not dim2:
dim2 = dim1
if not dim3:
dim3 = dim1
dim3=(630, 480)
scaled_K = K * dim1[0] / DIM[0] # The values of K is to scale with image dimension.
scaled_K[2][2] = 1.0 # Except that K[2][2] is always 1.0
# This is how scaled_K, dim2 and balance are used to determine the final K used to un-distort image. OpenCV document failed to make this clear!
new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)
map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim3, cv2.CV_16SC2)
undistorted_img = cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
# cv2.imwrite('Testpic3_undistorted.jpg', undistorted_img)
out1.write(undistorted_img)
cv2.imshow("undistorted", undistorted_img)
# Write the frame
out.write(frame)
cv2.imshow('frame',frame)
# cv2.imshow('gray', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
out1.release()
cv2.destroyAllWindows()
Thanks,