Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4825

General discussion • Re: Problem with real-time circle detection using OpenCV on Raspberry Pi 4

$
0
0
You could bypass cv2.videocapture, and use picamera2 instead to capture the frames for you.
Something like this.

Code:

import cv2import numpy as npfrom picamera2 import Picamera2picam2=Picamera2()picam2.configure (picam2.create_video_configuration(main={'format': 'RGB888', 'size' :(320,240)}))picam2.start()# Defines a color range to detect red circleslower_red = np.array([160, 50, 50])upper_red = np.array([180, 255, 255])while True:     # Get the current frame of the camera     frame = picam2.capture_array()     # Convert the frame to grayscale     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)     # Apply Gaussian smoothing filter     blurred = cv2.GaussianBlur(gray, (5, 5), 0)     # Detect edges in blurred image     edges = cv2.Canny(blurred, 50, 150)     # Search for circles in the border image     circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=100, param1=100, param2=30)     # Draw the circles detected in the original frame     if circles is not None:         for (x, y, r) in circles[0]:             cv2.circle(frame, (int(x), int(y)), int(r), (0, 255, 0), 2)     # Shows the frame with the detected circles     cv2.imshow('Circle detection', frame)     # If the 'q' key is pressed, the window is closed     if cv2.waitKey(1) & 0xFF == ord('q'):         break# Release the camera and close all the windowspicam2.close()cv2.destroyAllWindows()

Statistics: Posted by sandyol — Sat Apr 13, 2024 4:34 am



Viewing all articles
Browse latest Browse all 4825

Trending Articles