Update tempo.txt
Browse files
tempo.txt
CHANGED
|
@@ -1,61 +1,51 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
m = (y2 - y1) / (x2 - x1)
|
| 8 |
-
c = y1 - m * x1
|
| 9 |
-
else:
|
| 10 |
-
# The line is vertical
|
| 11 |
-
m = None
|
| 12 |
|
| 13 |
-
|
| 14 |
-
points = []
|
| 15 |
-
|
| 16 |
-
# Intersection with the left border (x=0)
|
| 17 |
-
if m is not None:
|
| 18 |
-
y_at_left = c
|
| 19 |
-
if 0 <= y_at_left <= img_height:
|
| 20 |
-
points.append((0, int(y_at_left)))
|
| 21 |
-
|
| 22 |
-
# Intersection with the right border (x=img_width)
|
| 23 |
-
if m is not None:
|
| 24 |
-
y_at_right = m * img_width + c
|
| 25 |
-
if 0 <= y_at_right <= img_height:
|
| 26 |
-
points.append((img_width, int(y_at_right)))
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
points.append((int(x_at_top), 0))
|
| 33 |
-
else:
|
| 34 |
-
# The line is vertical and intersects the top border if y1=0 or y2=0
|
| 35 |
-
if y1 == 0:
|
| 36 |
-
points.append((x1, 0))
|
| 37 |
-
elif y2 == 0:
|
| 38 |
-
points.append((x2, 0))
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
extended_points = points
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
+
def detect_half_circles(binary_image, threshold=80):
|
| 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 |
|
| 10 |
+
for contour in contours:
|
| 11 |
+
# Approximate the contour
|
| 12 |
+
epsilon = 0.04 * cv2.arcLength(contour, True)
|
| 13 |
+
approx = cv2.approxPolyDP(contour, epsilon, True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Check for a half-circle shape
|
| 16 |
+
if len(approx) >= 5:
|
| 17 |
+
# Fit an ellipse to the contour
|
| 18 |
+
ellipse = cv2.fitEllipse(contour)
|
| 19 |
+
(x, y), (MA, ma), angle = ellipse
|
| 20 |
+
|
| 21 |
+
# Calculate the aspect ratio
|
| 22 |
+
aspect_ratio = min(MA, ma) / max(MA, ma)
|
| 23 |
+
|
| 24 |
+
# Check if the shape is approximately a half-circle
|
| 25 |
+
if 0.4 < aspect_ratio < 0.6: # Adjust these values as needed
|
| 26 |
+
# Calculate the match score (this is a simplistic approach, you might need a more robust method)
|
| 27 |
+
match_score = (1 - abs(aspect_ratio - 0.5) / 0.5) * 100
|
| 28 |
+
|
| 29 |
+
if match_score >= threshold:
|
| 30 |
+
detected_shapes.append(approx)
|
| 31 |
+
|
| 32 |
+
# Draw a bounding rectangle around the detected half-circle
|
| 33 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 34 |
+
cv2.rectangle(binary_image, (x, y), (x + w, y + h), (255, 255, 255), 2)
|
| 35 |
+
|
| 36 |
+
return binary_image, detected_shapes
|
| 37 |
|
| 38 |
+
# Example usage
|
| 39 |
+
# Load the binary masked image (make sure it's a binary image)
|
| 40 |
+
binary_image = cv2.imread('path_to_your_image.png', cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Threshold the image to make sure it's binary
|
| 43 |
+
_, binary_image = cv2.threshold(binary_image, 127, 255, cv2.THRESH_BINARY)
|
| 44 |
|
| 45 |
+
# Detect half-circles and draw rectangles
|
| 46 |
+
output_image, detected_shapes = detect_half_circles(binary_image)
|
| 47 |
+
|
| 48 |
+
# Show the result
|
| 49 |
+
cv2.imshow('Detected Half-Circles', output_image)
|
| 50 |
+
cv2.waitKey(0)
|
| 51 |
+
cv2.destroyAllWindows()
|