Home Contents Start Prev 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Next

Phase 3: Camera and OpenCV Research

This phase involves experimenting with the Pi camera. I am using a standard model 3 camera. See: Camera Details.

Phase 3 Aims

  • Load up Pi with openCV and get it working.
  • Use a Pi camera to take images
  • Install Pi on kupe
  • Loom wiring from power to Pi
  • Physically install Camera on Kupe
  • Wire camera into Pi

Camera Software

The software I needed to install was:

  • PiCamera2
  • Libcamera
  • OpenCv

It was found that camera software installation for the Pi5 required some adjustments, as not everything was available as prebuilt binaries. Ultimately, libcamera had to be installed globally (using apt install), although a local version of OpenCV could be added to the virtual environment (using pip install). The goal was to invert the image during capture, which necessitated the use of libcamera libraries, as this feature was not available with the Picamera2 version on the Raspberry Pi 5 installation.

There seem to be several APIs I can use to stream data and preview or display it on-the fly. Initially, I am going to keep it simple, I will capture and image and store it to disk. Then I will open it for processing. When it comes to visual odometry, I would typically do something like this: (pseudocode)

while True:
    store_image1 = capture_and_store_image(image1)
    // locomotion or explicit delay
    store_image2 = capture_and_store_image(image2)

    images[0] = fetch_and_preprocess_image(store_image1)
    images[1] = fetch_and_preprocess_image(store_image2)

    current_pose[] = perform_visual_odometry(images, cfg)

    action = determine_action_required(previous_pose, current_pose)
    // do something with action.

// previous pose will likely be overwritten with current pose depending on action taken.

Maybe later I can stream data but this slow synchronous mechanism allows me to monitor what is happening more easily. Baby steps..

Using Picamera2 to capture an image

This is a very simple example showing how to capture an image with the camera upside-down:

from picamera2 import Picamera2
from libcamera import Transform

"""
Note that create_preview_configuration() defaults to low res.
whereas create_still_configuration() defaults to high res.
"""

picam2 = Picamera2()
# vflip=True or vflip=1 flips vertically
# This does not work due to Picamera2 version restriction. We need to use libcamera to Transform instead
#config = picam2.create_preview_configuration(transform=Transform(vflip=1))
#picam2.configure(config)

# Libcamera transform support
config = picam2.create_preview_configuration()
config["transform"] = Transform(vflip=True)  # Vertically flip

picam2.configure(config)

picam2.start()
frame = picam2.capture_file("capture.jpg")
picam2.stop()

Using OpenCV to display a captured image


import cv2

# Load the image
image = cv2.imread('capture.jpg')

# Check if the image was loaded successfully
if image is None:
    print("Error: Could not load the image.")
else:
    # Display the image in a window
    cv2.imshow('Image Window', image)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()

It is not a large step to move from image capture to video capture. I am not going to investiage video for now as I will end up using single frames taken from the video stream. I will stay with simple image capture.


May 2026


Home Contents Start Prev 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Next