Below is a Python program using OpenCV and NumPy that fulfills your requirements. This program allows you to load an image, draw a horizontal or vertical line, and then draw a second line that can be rotated interactively. The angle between the two lines is displayed in a text box.
import cv2
import numpy as np
class LineDrawer:
def __init__(self, image):
self.image = image.copy()
self.clone = image.copy()
self.first_line = None
self.second_line = None
self.angle = 0
def draw_first_line(self):
print("Draw the first line. Press 'h' for horizontal, 'v' for vertical.")
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", self.first_line_callback)
while True:
cv2.imshow("Image", self.image)
key = cv2.waitKey(1) & 0xFF
if key == ord('h') or key == ord('v'):
break
elif key == ord('q'):
return
cv2.setMouseCallback("Image", lambda *args: None)
if key == ord('h'):
self.first_line = [(0, int(self.image.shape[0] / 2)), (self.image.shape[1], int(self.image.shape[0] / 2))]
elif key == ord('v'):
self.first_line = [(int(self.image.shape[1] / 2), 0), (int(self.image.shape[1] / 2), self.image.shape[0])]
self.image = self.clone.copy()
cv2.line(self.image, self.first_line[0], self.first_line[1], (0, 255, 0), 2)
def first_line_callback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
pass
def draw_second_line(self):
print("Draw the second line. Click and drag to rotate.")
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", self.second_line_callback)
self.second_line_start = None
self.second_line_end = None
while True:
cv2.imshow("Image", self.image)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
cv2.setMouseCallback("Image", lambda *args: None)
def second_line_callback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
self.second_line_start = (x, y)
self.second_line_end = (x, y)
elif event == cv2.EVENT_MOUSEMOVE and self.second_line_start:
self.second_line_end = (x, y)
self.image = self.clone.copy()
cv2.line(self.image, self.first_line[0], self.first_line[1], (0, 255, 0), 2)
cv2.line(self.image, self.second_line_start, self.second_line_end, (255, 0, 0), 2)
self.calculate_angle()
cv2.putText(self.image, f"Angle: {self.angle:.2f} degrees", (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("Image", self.image)
def calculate_angle(self):
if self.first_line and self.second_line_start and self.second_line_end:
x1, y1 = self.first_line[0]
x2, y2 = self.first_line[1]
x3, y3 = self.second_line_start
x4, y4 = self.second_line_end
if x1 == x2: # vertical line
angle = np.arctan((x4 - x3) / (y4 - y3)) * 180 / np.pi
elif y1 == y2: # horizontal line
angle = np.arctan((y4 - y3) / (x4 - x3)) * 180 / np.pi
else:
m1 = (y2 - y1) / (x2 - x1)
m2 = (y4 - y3) / (x4 - x3)
angle = np.arctan((m2 - m1) / (1 + m1 * m2)) * 180 / np.pi
self.angle = abs(angle)
def main():
image_path = input("Enter the image path: ")
image = cv2.imread(image_path)
drawer = LineDrawer(image)
drawer.draw_first_line()
drawer.draw_second_line()
cv2.imshow("Image", drawer.image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
This program first loads an image and asks the user to draw a horizontal or vertical line by pressing 'h' or 'v' respectively. Then, it allows the user to draw a second line by clicking and dragging the mouse. The angle between the two lines is calculated and displayed on the image.
Please note that this program assumes that the image is in the same directory as the script, or you need to provide the full path to the image. Also, the angle calculation might not be accurate for all cases, especially when the lines are vertical or horizontal.