Nekshay commited on
Commit
570d958
·
verified ·
1 Parent(s): ab4e97b

Update tempo.txt

Browse files
Files changed (1) hide show
  1. tempo.txt +42 -52
tempo.txt CHANGED
@@ -1,61 +1,51 @@
1
  import cv2
2
  import numpy as np
3
 
4
- def extend_line_to_image_edges(x1, y1, x2, y2, img_width, img_height):
5
- # Calculate the slope (m) and intercept (c) of the line
6
- if x2 != x1:
7
- m = (y2 - y1) / (x2 - x1)
8
- c = y1 - m * x1
9
- else:
10
- # The line is vertical
11
- m = None
12
 
13
- # Find intersection points with image borders
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
- # Intersection with the top border (y=0)
29
- if m is not None:
30
- x_at_top = -c / m
31
- if 0 <= x_at_top <= img_width:
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
- # Intersection with the bottom border (y=img_height)
41
- if m is not None:
42
- x_at_bottom = (img_height - c) / m
43
- if 0 <= x_at_bottom <= img_width:
44
- points.append((int(x_at_bottom), img_height))
45
- else:
46
- # The line is vertical and intersects the bottom border if y1=img_height or y2=img_height
47
- if y1 == img_height:
48
- points.append((x1, img_height))
49
- elif y2 == img_height:
50
- points.append((x2, img_height))
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Choose the two valid intersection points that extend the line
53
- if len(points) >= 2:
54
- extended_points = points[:2]
55
- else:
56
- extended_points = points
57
 
58
- return extended_points
 
59
 
60
- # Example usage
61
- img = np.zeros((500, 500, 3),
 
 
 
 
 
 
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()