By the end of this lesson, you will be able to:
:information_source: OpenCV-Python is a Python library that helps you solve computer vision problems. It combines the power of OpenCV (Open Source Computer Vision) with Python's simplicity.
:bulb: Want to explore more? Visit the OpenCV-Python Documentation for advanced tutorials!
:emoji:️ What We'll Learn
We'll explore three main topics with hands-on examples:
- Installing and importing OpenCV-Python - Set up your environment
- Reading and showing images - Work with pictures
- Capturing and showing video/webcam - Process video streams
:computer: Install and Import OpenCV-Python
First, let's set up our environment! We'll use VSCode for learning OpenCV-Python. (Set up VSCode for Python following the guidelines here)
Installing the Library
Since OpenCV-Python is a library, you need to install it using pip:
- For Windows:
bashpy -m pip install opencv-contrib-python
- For macOS:
bashpip3 install opencv-python
Installing NumPy
NumPy helps OpenCV work with arrays and matrices. Install it too:
- For Windows:
bashpy -m pip install numpy
- For macOS:
bashpip3 install numpy
Importing the Libraries
After installation, import both libraries in your Python code:
python
import numpy as np import cv2 as cv
note We use short names (
np
for NumPy andcv
for OpenCV) to make our code cleaner!
Let's learn how to work with images! OpenCV provides three main functions:
cv.imread(image)
- Reads an image from a filecv.imshow(window_name, image_readed)
- Shows the image in a windowcv.waitKey(0)
- Keeps the window open (0 means wait forever)Here's how to read and show an image called "image.jpg":
# read image
img = cv.imread("image.jpg")
# show image
cv.imshow("Image", img)
# showing the image forever
cv.waitKey(0)
:bulb: The window will stay open until you press any key. This gives you time to look at your image!
:movie_camera: Read and Show Video/Webcam
Understanding Video
A video is like a flipbook - it's made of many pictures (called frames) shown quickly one after another!
:information_source: Frames Per Second (FPS) tells us how many pictures show in one second:
- Less than 24 FPS: Video looks choppy
- 24-30 FPS: Smooth for movies
- 60+ FPS: Super smooth for games
The image below shows how different FPS values affect video quality:
Working with Video
OpenCV handles video by processing it frame by frame. Here are the key functions:
- Capture video source:
cv.VideoCapture("video.mp4")
- Opens a video filecv.VideoCapture(0)
- Opens your webcam (0 = default camera)- Process frames in a loop:
ret, frame = video.read()
- Gets the next framecv.imshow(window_name, frame)
- Shows the framecv.waitKey(milliseconds)
- Controls playback speed- Clean up when done:
video.release()
- Stops capturingcv.destroyAllWindows()
- Closes all windowsExample: Play a Video File
Here's how to play a video file called "video.mp4":
python
# capture video video = cv.VideoCapture("video.mp4") # Show video frame by frame in a while loop while True: # capture frame-by-frame ret, frame = video.read() # show video frame-by-frame every 25 millisecond cv.imshow('frame', frame) cv.waitKey(25) # Release everything if job is finished video.release() cv.destroyAllWindows()
Stopping Video Playback
We need to stop the video in two situations:
- Video ends - No more frames to show
- User wants to quit - Press a key to stop
Example: Video with Stop Controls
This example shows how to stop when the video ends or when you press 'Q':
python
# capture video video = cv.VideoCapture("video.mp4") while True: # capture frame-by-frame ret, frame = video.read() # Quit when no more frame if not ret: print("Video Ended") break # show video frame-by-frame cv.imshow('frame', frame) # stop when Q is pressed if cv.waitKey(25) == ord("q"): break # Release everything if job is finished video.release() cv.destroyAllWindows()
note Remember:
ord("q")
converts the letter 'q' into a number that the computer understands!
In this lesson, you learned:
OpenCV-Python opens the door to computer vision projects. You can now work with images and videos in your Python programs!
Watch this video to see OpenCV-Python in action:
Use AI to explore OpenCV further! Try these prompts:
Getting Started:
Going Deeper:
Try these hands-on exercises to master OpenCV-Python:
Image Explorer: Create a program that:
Webcam Fun: Build a simple webcam app that:
Video Player: Make a basic video player that:
:bulb: Tip Start with the first exercise and work your way up. Each one builds on what you learned before!