Hướng dẫn find angle between two lines opencv python - tìm góc giữa hai dòng opencv python

Tôi đã phát hiện ra một đường ranh giới làn đường không thẳng bằng cách sử dụng biến đổi Hough và sau đó trích xuất dòng đó một cách riêng biệt.Sau đó pha trộn với một hình ảnh khác có một đường thẳng.Bây giờ tôi cần tính toán góc giữa hai dòng đó, nhưng tôi không biết tọa độ của các dòng đó.Vì vậy, tôi đã thử với mã cung cấp tọa độ của các đường thẳng đứng, nhưng nó không thể xác định cụ thể các tọa độ đó.Có cách nào để đo góc giữa những dòng đó không?Đây là mã tính toán tọa độ của tôi và hình ảnh pha trộn với hai dòng

import cv2 as cv import numpy as np src = cv.imread("blended2.png", cv.IMREAD_COLOR) if len(src.shape) != 2: gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) else: gray = src gray = cv.bitwise_not(gray) bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 15, -2) horizontal = np.copy(bw) vertical = np.copy(bw) cols = horizontal.shape[1] horizontal_size = int(cols / 30) horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1)) horizontal = cv.erode(horizontal, horizontalStructure) horizontal = cv.dilate(horizontal, horizontalStructure) cv.imwrite("img_horizontal8.png", horizontal) h_transpose = np.transpose(np.nonzero(horizontal)) print("h_transpose") print(h_transpose[:100]) rows = vertical.shape[0] verticalsize = int(rows / 30) verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize)) vertical = cv.erode(vertical, verticalStructure) vertical = cv.dilate(vertical, verticalStructure) cv.imwrite("img_vertical8.png", vertical) v_transpose = np.transpose(np.nonzero(vertical)) print("v_transpose") print(v_transpose[:100]) img = src.copy() # edges = cv.Canny(vertical,50,150,apertureSize = 3) minLineLength = 100 maxLineGap = 200 lines = cv.HoughLinesP(vertical,1,np.pi/180,100,minLineLength,maxLineGap) for line in lines: for x1,y1,x2,y2 in line: cv.line(img,(x1,y1),(x2,y2),(0,255,0),2) cv.imshow('houghlinesP_vert', img) cv.waitKey(0)

Tôi có hai dòng sẽ được vẽ qua các sự kiện chuột và để tìm góc giữa chúng như nếu tôi vẽ hai đường song song, góc cho tôi 0

Những gì tôi đã thử:

import numpy as np import cv2 import math btn_down = False def get_points(im): data = {} data['im'] = im.copy() data['lines'] = [] cv2.imshow("Image", im) cv2.setMouseCallback("Image", mouse_handler,data) cv2.waitKey(0) points = np.uint16(data['lines']) return points, data['im'] def mouse_handler(event, x, y, flags, data): global btn_down if event == cv2.EVENT_LBUTTONUP and btn_down: btn_down = False data['lines'][0].append((x, y)) cv2.circle(data['im'], (x, y), 3, (0, 0, 255),5) cv2.line(data['im'], data['lines'][0][0], data['lines'][0][1], (0,0,255), 2) cv2.imshow("Image", data['im']) elif event == cv2.EVENT_MOUSEMOVE and btn_down: image = data['im'].copy() cv2.line(image, data['lines'][0][0], (x, y), (0,0,0), 1) cv2.imshow("Image", image) elif event == cv2.EVENT_LBUTTONDOWN: btn_down = True data['lines'].insert(0,[(x, y)]) cv2.circle(data['im'], (x, y), 3, (0, 0, 255), 5, 16) cv2.imshow("Image", data['im']) def gradient(pt1, pt2): return (pt2[1] - pt1[1]) / (pt2[0] - pt1[0]) def getangle(pt1,pt2,pt3,pt4): pt1,pt2,pt3,pt4 = data['im'][-4:] print(f"point1: {pt1} \n point2: {pt2} \n point3 : {pt3}") m1 = gradient(pt1, pt2) m2 = gradient(pt1, pt3) angR = math.atan((m2 - m1) / (1 + (m2 * m1))) angD = math.degrees(angR) cv2.putText(final_image, str(angD), (pt1[0] - 20, pt1[1] - 20), cv2.FONT_HERSHEY_COMPLEX,1, (0, 0, 255), 2) print(f"angle: {angD} \n angleR: {angR}") img = cv2.imread('2.jpg', 1) pts, final_image = get_points(img) while True: if len(pts) % 4 == 0 and len(pts) != 0: getangle( data['im']) cv2.imshow('Image', final_image) print(pts[-2:]) cv2.waitKey(0)

Chủ đề