Update tempo.txt
Browse files
tempo.txt
CHANGED
|
@@ -1,42 +1,35 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
def detect_half_circles(binary_image, threshold=
|
|
|
|
|
|
|
|
|
|
| 5 |
# Find contours in the binary image
|
| 6 |
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 7 |
|
| 8 |
detected_shapes = []
|
| 9 |
-
output_image = cv2.cvtColor(binary_image, cv2.COLOR_GRAY2BGR)
|
| 10 |
|
| 11 |
for contour in contours:
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
approx = cv2.approxPolyDP(contour, epsilon, True)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
if len(
|
| 18 |
-
# Fit an ellipse to the contour
|
| 19 |
ellipse = cv2.fitEllipse(contour)
|
| 20 |
(x, y), (MA, ma), angle = ellipse
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Check if the shape is approximately a half-circle
|
| 26 |
-
if 0.4 < aspect_ratio < 0.6: # Adjust these values as needed
|
| 27 |
-
# Calculate the match score (this is a simplistic approach, you might need a more robust method)
|
| 28 |
-
match_score = (1 - abs(aspect_ratio - 0.5) / 0.5) * 100
|
| 29 |
|
| 30 |
-
if
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Draw a bounding rectangle around the detected half-circle
|
| 34 |
-
x, y, w, h = cv2.boundingRect(contour)
|
| 35 |
-
cv2.rectangle(output_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 36 |
|
| 37 |
# Draw the contour with a different color
|
| 38 |
-
cv2.drawContours(output_image, [
|
| 39 |
-
|
| 40 |
return output_image, detected_shapes
|
| 41 |
|
| 42 |
# Example usage
|
|
@@ -46,7 +39,7 @@ binary_image = cv2.imread('path_to_your_image.png', cv2.IMREAD_GRAYSCALE)
|
|
| 46 |
# Threshold the image to make sure it's binary
|
| 47 |
_, binary_image = cv2.threshold(binary_image, 127, 255, cv2.THRESH_BINARY)
|
| 48 |
|
| 49 |
-
# Detect half-circles and draw
|
| 50 |
output_image, detected_shapes = detect_half_circles(binary_image)
|
| 51 |
|
| 52 |
# Show the result
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
+
def detect_half_circles(binary_image, threshold=0.8):
|
| 5 |
+
# Convert binary image to BGR for drawing colored shapes
|
| 6 |
+
output_image = cv2.cvtColor(binary_image, cv2.COLOR_GRAY2BGR)
|
| 7 |
+
|
| 8 |
# Find contours in the binary image
|
| 9 |
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 10 |
|
| 11 |
detected_shapes = []
|
|
|
|
| 12 |
|
| 13 |
for contour in contours:
|
| 14 |
+
# Calculate the perimeter of the contour
|
| 15 |
+
perimeter = cv2.arcLength(contour, True)
|
|
|
|
| 16 |
|
| 17 |
+
# Fit an ellipse to the contour
|
| 18 |
+
if len(contour) >= 5: # Need at least 5 points to fit an ellipse
|
|
|
|
| 19 |
ellipse = cv2.fitEllipse(contour)
|
| 20 |
(x, y), (MA, ma), angle = ellipse
|
| 21 |
|
| 22 |
+
# Check if the major axis is approximately twice the minor axis (indicating a half-circle)
|
| 23 |
+
if 0.8 < (MA / ma) < 1.2: # This range can be adjusted based on specific requirements
|
| 24 |
+
aspect_ratio = float(MA) / ma
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Check if the aspect ratio is approximately 2:1 (indicating a half-circle shape)
|
| 27 |
+
if 0.4 < aspect_ratio < 0.6: # This range can also be adjusted
|
| 28 |
+
detected_shapes.append(contour)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Draw the contour with a different color
|
| 31 |
+
cv2.drawContours(output_image, [contour], -1, (0, 0, 255), 2)
|
| 32 |
+
|
| 33 |
return output_image, detected_shapes
|
| 34 |
|
| 35 |
# Example usage
|
|
|
|
| 39 |
# Threshold the image to make sure it's binary
|
| 40 |
_, binary_image = cv2.threshold(binary_image, 127, 255, cv2.THRESH_BINARY)
|
| 41 |
|
| 42 |
+
# Detect half-circles and draw contours
|
| 43 |
output_image, detected_shapes = detect_half_circles(binary_image)
|
| 44 |
|
| 45 |
# Show the result
|