0

I want to get alpha channel from mov file using opencv-python. And, I read these information: https://github.com/opencv/opencv/pull/13395. I tried to write simple code, but failed:

import cv2
import numpy as np

print("OpenCV:", cv2.__version__)

file_name = "../assets/piku01.mov"

# Capture
cap = cv2.VideoCapture(file_name)
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0)# No means...!?

while cap.isOpened():
    ret, frame = cap.read()# Read
    print("frame:", frame.shape)# Can't get alpha channel
    print(frame[0][0])# Removed alpha channel...?
    break

cap.release()

Output:

OpenCV: 4.5.4-dev
frame: (1080, 1920, 3)
[0 0 0]

Any idea?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • use PyAV instead. expect OpenCV to *not* be able to do it. -- you could explore the CAP_PROP_CONVERT_RGB flag but it's only implemented for some backends. – Christoph Rackwitz Oct 17 '22 at 14:25
  • 1
    You may use PyAV or FFmpeg sub-process or FFmpeg Python binding. Here are [examples](https://stackoverflow.com/a/59996030/4926757) for using FFmpeg and for using PyAV. If you decide to use PyAV, replace `format='bgr24'` with `format='bgra'`. What is the video codec and pixel format of `piku01.mov`? – Rotem Oct 17 '22 at 20:52
  • Thank you very much for your answer!! I was able to get the Alpha value using PyAV!! – Shimeji Kajiru Oct 18 '22 at 13:47

2 Answers2

0

Thank you all!!
I could get alpha channel from video using PyAV
The code is as below

import av
import numpy as np

print("PyAV:", av.__version__)

container = av.open("./piku01.mov")
for frame in container.decode(video=0):
    frame = frame.to_ndarray(format="bgra") # BGRA
    print("frame:", frame.shape) # Well done!!

Output:

frame: (1080, 1920, 4)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '22 at 23:55
-1
print("OpenCV:", cv2.__version__)
file_name = "../assets/piku01.mov"

cap = cv2.VideoCapture(file_name)
ret, frame = cap.read()
alpha = frame[:, :, 3]
cap.release()
Tomward Matthias
  • 127
  • 1
  • 13
  • please try that before posting it as an answer. VideoCapture will not give 4-channel frames. OP's question shows the shape of the data they're getting from the read() call. – Christoph Rackwitz Oct 17 '22 at 14:20