Nekshay commited on
Commit
3d58410
·
verified ·
1 Parent(s): e5fac57

Update tem.txt

Browse files
Files changed (1) hide show
  1. tem.txt +33 -29
tem.txt CHANGED
@@ -1,31 +1,35 @@
1
- import cv2
2
  import numpy as np
3
 
4
- # Read the mask image
5
- mask = cv2.imread('mask_image.png', cv2.IMREAD_GRAYSCALE)
6
-
7
- # Threshold the mask to obtain binary image
8
- _, thresh = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)
9
-
10
- # Find contours in the binary image
11
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
12
-
13
- # Sort contours by area and keep the largest one
14
- contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
15
-
16
- # Approximate the contour to reduce the number of points
17
- epsilon = 0.1 * cv2.arcLength(contours[0], True)
18
- approx = cv2.approxPolyDP(contours[0], epsilon, True)
19
-
20
- # Draw the contour and its approximated shape
21
- cv2.drawContours(mask, [approx], -1, (0, 255, 0), 2)
22
-
23
- # Display the image with the contours
24
- cv2.imshow('Contour', mask)
25
- cv2.waitKey(0)
26
- cv2.destroyAllWindows()
27
-
28
- # Print the coordinates of the approximated corners
29
- print("Approximated corner points:")
30
- for point in approx:
31
- print("({}, {})".format(point[0][0], point[0][1]))
 
 
 
 
 
 
 
1
  import numpy as np
2
 
3
+ def calculate_endpoint(A_start, A_end, B, angle_deg):
4
+ # Calculate vector representing line A
5
+ vec_A = np.array(A_end) - np.array(A_start)
6
+
7
+ # Calculate length of line A
8
+ length_A = np.linalg.norm(vec_A)
9
+
10
+ # Calculate unit vector of line A
11
+ unit_vec_A = vec_A / length_A
12
+
13
+ # Convert angle from degrees to radians
14
+ angle_rad = np.deg2rad(angle_deg)
15
+
16
+ # Calculate rotation matrix for 30-degree angle
17
+ rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)],
18
+ [np.sin(angle_rad), np.cos(angle_rad)]])
19
+
20
+ # Rotate unit vector of line A by 30 degrees
21
+ rotated_unit_vec_A = np.dot(rotation_matrix, unit_vec_A)
22
+
23
+ # Scale rotated unit vector by length of line A and add to point B to get endpoint coordinates
24
+ endpoint = np.array(B) + length_A * rotated_unit_vec_A
25
+
26
+ return endpoint
27
+
28
+ # Example usage
29
+ A_start = (0, 0)
30
+ A_end = (3, 4)
31
+ B = (1, 1)
32
+ angle_deg = 30
33
+
34
+ endpoint = calculate_endpoint(A_start, A_end, B, angle_deg)
35
+ print("Coordinates of the endpoint:", endpoint)