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

Update tempo.txt

Browse files
Files changed (1) hide show
  1. tempo.txt +56 -38
tempo.txt CHANGED
@@ -1,43 +1,61 @@
1
  import cv2
2
  import numpy as np
3
 
4
- # Load the mask image
5
- mask = cv2.imread('mask_image.png', cv2.IMREAD_GRAYSCALE) # Replace 'mask_image.png' with the path to your mask image
6
-
7
- # Find contours in the mask
8
- contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
9
-
10
- # Initialize variables to store the last two bottom-right points
11
- last_two_points = []
12
-
13
- # Iterate through each contour
14
- for contour in contours:
15
- # Extract the bottom-right points of the contour
16
- bottom_right_points = [point[0] for point in contour if point[0][0] == max(contour[:, :, 0]) and point[0][1] == max(contour[:, :, 1])]
17
 
18
- # Sort the bottom-right points based on their x-coordinate
19
- bottom_right_points.sort(key=lambda point: point[0])
20
 
21
- # Choose the last two points
22
- if len(bottom_right_points) >= 2:
23
- last_two_points.extend(bottom_right_points[-2:])
24
-
25
- # Check if the distance between the last two points is more than 200 pixels
26
- if len(last_two_points) == 2 and np.linalg.norm(np.array(last_two_points[0]) - np.array(last_two_points[1])) > 200:
27
- # Fit a line passing through the last two points
28
- vx, vy, x0, y0 = cv2.fitLine(np.array(last_two_points), cv2.DIST_L2, 0, 0.01, 0.01)
29
-
30
- # Calculate the start and end points of the line
31
- start_point = (int(x0 - 1000*vx), int(y0 - 1000*vy))
32
- end_point = (int(x0 + 1000*vx), int(y0 + 1000*vy))
33
-
34
- # Draw the line on a blank image
35
- line_image = np.zeros_like(mask)
36
- cv2.line(line_image, start_point, end_point, (255, 255, 255), thickness=2)
37
-
38
- # Display the line image
39
- cv2.imshow('Line Passing Through Last Two Points', line_image)
40
- cv2.waitKey(0)
41
- cv2.destroyAllWindows()
42
- else:
43
- print("Unable to find suitable points or distance constraint not met.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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),