I am working with OpenCV. I have a video file, and I want to read it in grayscale, instead of converting every frame in grayscale. i.e
cap = cv2.VideoCapture(VIDEO_PATH)
results = {}
curr_frame = 0
start = time.time()
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
curr_frame+=1
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
I do not want to do this, instead I want to read it directly in grayscale as we do in images i.e
im_gray = cv2.imread('grayscale_image.png', cv2.IMREAD_GRAYSCALE)
Now upon inspecting the documentation, I found that the read function "The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call." And inside retrieve, we can pass a flag value. Is it possible to combine it with read and read it in gray scale instead of converting it explicitly?
I tried doing
ret, frame = cap.read(flag=0)
But it is not a valid keyword argument.
error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'read'
> Overload resolution failed:
> - 'flag' is an invalid keyword argument for VideoCapture.read()
> - 'flag' is an invalid keyword argument for VideoCapture.read()
But I can pass in any random number in cap.read() and it works.
cap.read(-4)
I was looking for source code in Python, but can not found any source code in Python for this function on github.