Language: EN

como-usar-opencv-en-raspberry-pi

How to Use OpenCV on Raspberry Pi

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library that provides tools for image and video processing.

OpenCV is used in a wide variety of applications, from facial recognition and image analysis to robotics and automation.

Some of the most important features of OpenCV are

  • Image Processing: Allows applying filters, detecting edges, and performing basic operations on images.
  • Object Recognition: Facilitates the detection and tracking of objects in images and videos.
  • Motion Analysis: Allows analyzing and tracking motion in video sequences.
  • User Interface: Offers tools to create graphical interfaces and display images and videos.

OpenCV allows performing advanced image processing tasks on the Raspberry Pi, in projects such as implementing motion detection or facial recognition systems, robots that can navigate and recognize objects in their environment, or creating intelligent systems that respond to visual stimuli.

How to Install OpenCV on Raspberry Pi

To use OpenCV with Python on Raspberry Pi, we first need to install the library. There are several ways to do this, but the simplest and recommended option is to use the pip package system. We will cover the installation process step by step.

Before installing any software, it is good practice to update the system. Run the following commands in the terminal:

sudo apt update
sudo apt upgrade

OpenCV requires several dependencies that we need to install first. Run the following command to install the necessary libraries:

sudo apt install build-essential cmake git libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev

Now that we have all the dependencies, we can proceed to install OpenCV. First, we will install pip and numpy, which are necessary to handle Python packages and arrays:

sudo apt install python3-pip
pip3 install numpy

With pip installed, we can install OpenCV using the following command:

pip3 install opencv-python

To ensure that OpenCV has been installed correctly, we can check the version of the library. Open a Python interpreter and run the following code:

import cv2
print(cv2.__version__)

If everything is correct, you should see the version of OpenCV printed on the screen.

Using OpenCV with Python

Now that we have OpenCV installed, we can start using it in our projects. Let’s look at some basic examples to illustrate how OpenCV is used with Python.

Read and Display an Image

First, we will create a simple script to read and display an image using OpenCV. Create a Python file named show_image.py with the following content:

import cv2

# Read an image
image = cv2.imread('path/to/your/image.jpg')

# Display the image in a window
cv2.imshow('Image', image)

# Wait until a key is pressed
cv2.waitKey(0)

# Close all windows
cv2.destroyAllWindows()

Make sure to replace 'path/to/your/image.jpg' with the path to an image on your system. To run the script, use:

python3 show_image.py

Capture Video from the Camera

Now we will capture real-time video from your Raspberry Pi’s camera. Create a Python file named capture_video.py with the following content:

import cv2

# Start capturing video from the camera
capture = cv2.VideoCapture(0)

while True:
    # Capture frame by frame
    ret, frame = capture.read()

    # Display the frame in a window
    cv2.imshow('Live Video', frame)

    # Exit if the 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the capture and close the windows
capture.release()
cv2.destroyAllWindows()

Run this script with:

python3 capture_video.py

Press ‘q’ to exit the video window.

Face Detection

One of the most common uses of OpenCV is face detection. We will use a pre-trained cascade classifier to detect faces in an image. Create a Python file named face_detection.py with the following content:

import cv2

# Load the cascade classifier for face detection
classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Read the image
image = cv2.imread('path/to/your/image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Display the image with detected faces
cv2.imshow('Detected Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Make sure to replace 'path/to/your/image.jpg' with the path to your image. Run the script with:

python3 face_detection.py