This guide provides instructions for installing OpenCV on Windows, macOS, and Linux, and demonstrates basic usage scenarios such as reading an image, reading a video, and extracting and saving frames from a video.
- Open a command prompt and ensure that Python and pip are installed:
python --version
pip --version
If Python is not installed, download and install it from the official website.
- Install OpenCV and OpenCV-contribute using pip:
pip install opencv-python
pip install opencv-contrib-python
- Open a terminal window and ensure that Python and pip are installed:
python3 --version
pip3 --version
If Python is not installed, install it using Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python
- Install OpenCV and OpenCV-contribute using pip:
pip3 install opencv-python
pip3 install opencv-contrib-python
- Open a terminal window and ensure that Python and pip are installed:
python3 --version
pip3 --version
If Python is not installed, install it using your distribution's package manager (e.g., apt for Ubuntu, dnf for Fedora).
- Install OpenCV and OpenCV-contribute using pip:
pip3 install opencv-python
pip3 install opencv-contrib-python
To read an image and display it:
import cv2
# Load an image
image = cv2.imread('path_to_your_image.jpg')
# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Replace 'path_to_your_image.jpg' with the path to your image.
To read a video and play it:
import cv2
# Open the video
cap = cv2.VideoCapture('path_to_your_video.mp4')
# Read and display each frame
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object
cap.release()
cv2.destroyAllWindows()
Replace 'path_to_your_video.mp4' with the path to your video.
To extract and save frames from a video:
import cv2
import os
def save_video_frames(video_path, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
cap = cv2.VideoCapture(video_path)
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imwrite(os.path.join(output_folder, f"frame_{frame_count:04d}.jpg"), frame)
frame_count += 1
cap.release()
print(f"Extracted {frame_count} frames.")
save_video_frames('path_to_your_video.mp4', 'frames_output')
Replace 'path_to_your_video.mp4' with the path to your video, and 'frames_output' with your desired output directory.
This guide has introduced how to install OpenCV and use it for basic image and video processing tasks. For more advanced features and usage, you can refer to the OpenCV documentation.