0

The following code "works" to roate the video however, I get an error. Also, it seems like the scale is off.

Any recommendations for how to fix?

Original: enter image description here

Final output:

enter image description here


import numpy as np
import cv2

# capture video
cap = cv2.VideoCapture("c:/sandbox/temp.mp4")

# calculate the center of the image
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))


center = (w / 2, h/ 2)

angle270 = 270

scale = 1.0

#descripe a loop
#read video frame by frame
while True:
    ret,img = cap.read()
    #rotate frames of video
    M = cv2.getRotationMatrix2D(center, angle270, scale)
    img2 = cv2.warpAffine(img, M, (w, h))
    cv2.imshow('rotated video',img2)

    k=cv2.waitKey(30) & 0xff
    #once you inter Esc capturing will stop
    if k==27:
        break



cap.release()
cv2.destroyAllWindows()

Message=OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:2611: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'cv::warpAffine'

SDM1212
  • 153
  • 12
  • A few things.... Python 3 has been out for 10+ years and Python 2.7 is discontinued in 4-5 months. Check the return value of your `cap.read()` and avoid using the frame if incorrect. Calculate your rotation matrix outside your loop as it will not change. – Mark Setchell Sep 06 '19 at 20:04
  • You can use the imutils library. `pip install imutils`. To rotate, you can do `rotated_image = imutils.rotate_bound(img, angle=90)`. Here's the [source code](https://github.com/jrosebr1/imutils/blob/master/imutils/convenience.py#L41) – nathancy Sep 06 '19 at 20:09
  • Other parts of this solution use Python 2.7. I will try your recommendations. Thank you! – SDM1212 Sep 06 '19 at 22:03
  • as commented, `cap.read()` will fail at the very end of the video. When that happens, `ret` is `False` and `img` is `None`, so all the other operations would fail on `img`. Right after `cap.read()`, do `if not ret: break;`. – Quang Hoang Sep 06 '19 at 22:14
  • This is similar to rotating an image! – Santhosh Dhaipule Chandrakanth Sep 07 '19 at 16:38
  • Possible duplicate of [OpenCV Python rotate image by X degrees around specific point](https://stackoverflow.com/questions/9041681/opencv-python-rotate-image-by-x-degrees-around-specific-point) – Santhosh Dhaipule Chandrakanth Sep 07 '19 at 16:39

0 Answers0