0

My objective is to have the webcam transmitting live feed and I could rotate the image feed by some degries (this part would be controlled by the values of a gyroscope). The problem is although the image rotates when using ndimage.rotate by the correct angle the video becomes really slow (below 10 fps). This is an example code of how im using it (in here I use a fixed angle just to demonstrate):

import cv2
from scipy import ndimage

cap = cv2.VideoCapture(0)
mode=2 #mode 1 = Normal mode, mode 2 = Rotated mode
# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, None, fx=1, fy=1, interpolation=cv2.INTER_AREA)
    
    if mode==1:
        cv2.imshow('Input', frame) #Original version
    else:
        rotate=ndimage.rotate(frame, angle=45, reshape=False)
        cv2.imshow('Input', rotate) #Rotated version (very slow)
    

    c = cv2.waitKey(1)
    if c == 27:
        break

cap.release()
cv2.destroyAllWindows()

How can I make the video to run as fast as the original mode? Or at least somewhat faster.

Thanks, its my first question so I apologize if Im missing something.

  • 1
    Does transmitting grayscale image also make it slower? I might be wrong but just give it a try – Jeru Luke Apr 28 '22 at 10:50
  • With grayscale it seems to help but I would prefer to have the image with color since the main application will be used in an object detection algorythm – José Santos Apr 28 '22 at 11:01
  • this transformation seems to happen CPU-side. you could try opencv's warpAffine and getRotationMatrix2D, or you could involve some GPU programming (OpenGL/Vulkan/D3D at least) -- your `resize()` does nothing since fx=fy=1 – Christoph Rackwitz Apr 28 '22 at 13:12
  • Try [imutils.rotate_bound](https://github.com/PyImageSearch/imutils/blob/master/imutils/convenience.py#L41) – nathancy Apr 29 '22 at 01:42
  • Thank you ChristophRackwitz and @nathancy, it worked, I used the function that nathancy provided and it rotates while keeping a good framerate, my only problem now is that the function rotates but reshapes the image instead of keeping the same dimensions like with the ndimage.rotate where I could choose not to reshape – José Santos Apr 29 '22 at 10:49

0 Answers0