0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use OpenCV on Raspberry Pi: Installation and Example

To use OpenCV on Raspberry Pi, first install it using pip or build from source. Then, write Python code to capture and process images or video using the cv2 module.
📐

Syntax

Here is the basic syntax to capture video from the Raspberry Pi camera or USB webcam using OpenCV in Python:

  • cv2.VideoCapture(0): Opens the default camera (0 is usually the first camera).
  • cap.read(): Reads a frame from the camera.
  • cv2.imshow(): Displays the image in a window.
  • cv2.waitKey(): Waits for a key press to control the display.
  • cap.release(): Releases the camera resource.
  • cv2.destroyAllWindows(): Closes all OpenCV windows.
python
import cv2

cap = cv2.VideoCapture(0)  # Open the first camera

while True:
    ret, frame = cap.read()  # Capture frame-by-frame
    if not ret:
        break
    cv2.imshow('Camera', frame)  # Show the frame
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()  # Release the camera
cv2.destroyAllWindows()  # Close windows
💻

Example

This example shows how to install OpenCV on Raspberry Pi and run a simple program to display live video from the camera.

First, install OpenCV with:

sudo apt update
sudo apt install python3-opencv

Then run this Python code:

python
import cv2

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("Cannot open camera")
    exit()

while True:
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    cv2.imshow('Camera Feed', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Output
Camera window opens showing live video feed until 'q' is pressed to quit.
⚠️

Common Pitfalls

  • Camera not opening: Make sure the camera is connected and enabled in Raspberry Pi settings.
  • Permission errors: Run your script with proper permissions or use sudo if needed.
  • Wrong camera index: If VideoCapture(0) fails, try VideoCapture(1) or check connected devices.
  • Missing OpenCV: Install OpenCV before running code, or import will fail.
  • Display issues: Raspberry Pi may need desktop environment for cv2.imshow() to work.
python
import cv2

# Wrong way: Not checking if camera opened
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('Frame', frame)  # May crash if camera not opened

# Right way: Check if camera opened
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print('Camera not accessible')
else:
    ret, frame = cap.read()
    if ret:
        cv2.imshow('Frame', frame)
    cap.release()
    cv2.destroyAllWindows()
📊

Quick Reference

Summary tips for using OpenCV on Raspberry Pi:

  • Install OpenCV with sudo apt install python3-opencv or pip3 install opencv-python.
  • Use cv2.VideoCapture(0) to access the camera.
  • Always check if the camera opened successfully with cap.isOpened().
  • Use cv2.imshow() to display images; requires desktop environment.
  • Press q to quit the video window loop.

Key Takeaways

Install OpenCV on Raspberry Pi using apt or pip before running code.
Use cv2.VideoCapture(0) to access the camera and always check if it opened.
Display images with cv2.imshow() and handle window events with cv2.waitKey().
Ensure the camera is enabled and connected properly in Raspberry Pi settings.
Press 'q' to exit the camera display loop safely.