Update 'red.py'
This commit is contained in:
parent
b16a4bbf63
commit
d2bbf53cc1
2 changed files with 129 additions and 2 deletions
|
@ -1,2 +0,0 @@
|
|||
# Circle_Detection_OpenCV
|
||||
|
129
red.py
Normal file
129
red.py
Normal file
|
@ -0,0 +1,129 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# Set up the camera capture
|
||||
cap = cv2.VideoCapture(0)
|
||||
|
||||
while True:
|
||||
# Capture the frame from the camera
|
||||
_, frame = cap.read()
|
||||
|
||||
# Convert the frame to HSV color space
|
||||
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||
|
||||
# Define the lower and upper bounds for the red color
|
||||
lower_red = np.array([0,100,100])
|
||||
upper_red = np.array([10,255,255])
|
||||
|
||||
# Create a mask for the red color
|
||||
mask = cv2.inRange(hsv, lower_red, upper_red)
|
||||
|
||||
# Use the mask to detect red circles in the frame
|
||||
circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
|
||||
|
||||
# Check if any circles were detected
|
||||
if circles is not None:
|
||||
# Convert the circles array to integer coordinates
|
||||
circles = np.round(circles[0, :]).astype("int")
|
||||
|
||||
# Loop through all circles
|
||||
for (x, y, r) in circles:
|
||||
# Calculate the area of the circle
|
||||
area = np.pi * r**2
|
||||
|
||||
# Draw the circle on the frame
|
||||
cv2.circle(frame, (x, y), r, (0, 255, 0), 2)
|
||||
|
||||
# Print the area and position of the circle
|
||||
print("Circle at (x={}, y={}) with area={:.2f}".format(x, y, area))
|
||||
|
||||
# Show the frame
|
||||
cv2.imshow("Frame", frame)
|
||||
|
||||
# Wait for the user to press a key
|
||||
key = cv2.waitKey(1) & 0xFF
|
||||
|
||||
# If the user pressed "q", break the loop
|
||||
if key == ord("q"):
|
||||
break
|
||||
|
||||
# Release the camera and destroy the windows
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# Set up the camera capture
|
||||
cap = cv2.VideoCapture(0)
|
||||
|
||||
while True:
|
||||
# Capture the frame from the camera
|
||||
_, frame = cap.read()
|
||||
|
||||
# Convert the frame to HSV color space
|
||||
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||
|
||||
# Define the lower and upper bounds for the red color
|
||||
lower_red = np.array([0,100,100])
|
||||
upper_red = np.array([10,255,255])
|
||||
|
||||
# Create a mask for the red color
|
||||
mask = cv2.inRange(hsv, lower_red, upper_red)
|
||||
|
||||
# Use the mask to detect red circles in the frame
|
||||
circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
|
||||
|
||||
# Check if any circles were detected
|
||||
if circles is not None:
|
||||
# Convert the circles array to integer coordinates
|
||||
circles = np.round(circles[0, :]).astype("int")
|
||||
|
||||
# Initialize the highest intensity to zero
|
||||
highest_intensity = 0
|
||||
|
||||
# Initialize the highest intensity circle to None
|
||||
highest_intensity_circle = None
|
||||
|
||||
# Loop through all circles
|
||||
for (x, y, r) in circles:
|
||||
# Calculate the area of the circle
|
||||
area = np.pi * r**2
|
||||
|
||||
# Crop the region of interest around the circle
|
||||
roi = frame[y-r:y+r, x-r:x+r]
|
||||
|
||||
# Calculate the mean intensity of the ROI
|
||||
intensity = np.mean(roi)
|
||||
|
||||
# Check if this circle has the highest intensity so far
|
||||
if intensity > highest_intensity:
|
||||
# Update the highest intensity and highest intensity circle
|
||||
highest_intensity = intensity
|
||||
highest_intensity_circle = (x, y, r)
|
||||
|
||||
# Check if a highest intensity circle was found
|
||||
if highest_intensity_circle is not None:
|
||||
# Unpack the highest intensity circle coordinates
|
||||
x, y, r = highest_intensity_circle
|
||||
|
||||
# Draw the circle on the frame
|
||||
cv2.circle(frame, (x, y), r, (0, 255, 0), 2)
|
||||
|
||||
# Print the area and position of the circle
|
||||
print("Circle at (x={}, y={}) with area={:.2f} and intensity={:.2f}".format(x, y, area, highest_intensity))
|
||||
|
||||
# Show the frame
|
||||
cv2.imshow("Frame", frame)
|
||||
|
||||
# Wait for the user to press a key
|
||||
key = cv2.waitKey(1) & 0xFF
|
||||
|
||||
# If the user pressed "q", break the loop
|
||||
if key == ord("q"):
|
||||
break
|
||||
|
||||
# Release the camera and destroy the windows
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
Loading…
Reference in a new issue