We can use opencv to read in a video, and extract each frame as an image. Then we can use Python Image Library to further analyze or edit
the image. But when you open the image, you might find it the color seems totally wrong.
This is just because opencv and Python Image Library (PIL) use different order of color code. The order in opencv is BGR, while in PIL it’s RGB. Fortunately, it’s easy to correct it as you will find in the following example
Read a short video of Robin bird using opencv
import cv2 |
Now we use PIL to view the image, and find the Robin in blue color. If you checked the original video, or you have seen a Robin before,
you know there is something wrong.
from PIL import Image |
Switch from BGR to RGB using opencv
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
Now we get the correct image of Robin, much better!
display(Image.fromarray(image_rgb)) |