Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Perform Image Processing on Raspberry Pi

Image processing is a crucial technology in various fields such as robotics, surveillance, and medical imaging. The Raspberry Pi, a versatile and affordable single-board computer, is well-suited for image processing tasks due to its portability, low power consumption, and sufficient computational power for basic to intermediate tasks. This article will demonstrate how to set up and run image processing tasks on a Raspberry Pi using Python and the OpenCV library.

Examples:

  1. Setting Up the Environment:

    First, ensure your Raspberry Pi is up to date:

    sudo apt-get update
    sudo apt-get upgrade

    Install the necessary libraries:

    sudo apt-get install python3-opencv python3-pip
    pip3 install numpy
  2. Simple Image Processing Example:

    Create a Python script to read an image, convert it to grayscale, and display it:

    import cv2
    
    # Load an image from file
    image = cv2.imread('example.jpg')
    
    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Display the original and grayscale images
    cv2.imshow('Original Image', image)
    cv2.imshow('Grayscale Image', gray_image)
    
    # Wait for a key press and close the image windows
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    Save this script as image_processing.py and run it:

    python3 image_processing.py
  3. Edge Detection Example:

    Extend the previous script to perform edge detection using the Canny algorithm:

    import cv2
    
    # Load an image from file
    image = cv2.imread('example.jpg')
    
    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Perform edge detection
    edges = cv2.Canny(gray_image, 100, 200)
    
    # Display the original, grayscale, and edge-detected images
    cv2.imshow('Original Image', image)
    cv2.imshow('Grayscale Image', gray_image)
    cv2.imshow('Edge Detected Image', edges)
    
    # Wait for a key press and close the image windows
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    Save this script as edge_detection.py and run it:

    python3 edge_detection.py
  4. Real-time Video Processing:

    To process video in real-time from the Raspberry Pi camera module, use the following script:

    import cv2
    
    # Open a connection to the camera
    cap = cv2.VideoCapture(0)
    
    while True:
       # Capture frame-by-frame
       ret, frame = cap.read()
    
       # Convert the frame to grayscale
       gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
       # Perform edge detection
       edges = cv2.Canny(gray_frame, 100, 200)
    
       # Display the resulting frame
       cv2.imshow('Edge Detected Video', edges)
    
       # Break the loop on 'q' key press
       if cv2.waitKey(1) & 0xFF == ord('q'):
           break
    
    # Release the camera and close all OpenCV windows
    cap.release()
    cv2.destroyAllWindows()

    Save this script as realtime_edge_detection.py and run it:

    python3 realtime_edge_detection.py

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.