如何使用python opencv测量同一图像中两条线之间的角度?

我使用霍夫变换检测到一条不直的车道边界线,然后分别提取该线。然后与另一个具有直线的图像混合。现在我需要计算这两条线之间的角度,但我不知道这些线的坐标。所以我尝试使用给出垂直线坐标的代码,但它无法具体识别这些坐标。有没有办法测量这些线之间的角度?这是我的坐标计算代码和两行混合图像


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)

http://img2.mukewang.com/622898870001bab506220473.jpg

料青山看我应如是
浏览 396回答 1
1回答

桃花长相依

一种方法是使用霍夫变换来检测线并获得每条线的角度。然后可以通过减去两条线之间的差来找到两条线之间的角度。我们首先使用算术平均来np.mean对导致此结果的图像进行阈值处理。image = cv2.imread('2.png')# Compute arithmetic meanimage = np.mean(image, axis=2)现在我们执行skimage.transform.hough_line检测线# Perform Hough Transformation to detect lineshspace, angles, distances = hough_line(image)# Find angleangle=[]for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):    angle.append(a)接下来我们获得每条线的角度并找到差异以获得我们的结果# Obtain angle for each lineangles = [a*180/np.pi for a in angle]# Compute difference between the two linesangle_difference = np.max(angles) - np.min(angles)print(angle_difference)16.08938547486033完整代码from skimage.transform import (hough_line, hough_line_peaks)import numpy as npimport cv2image = cv2.imread('2.png')# Compute arithmetic meanimage = np.mean(image, axis=2)# Perform Hough Transformation to detect lineshspace, angles, distances = hough_line(image)# Find angleangle=[]for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):    angle.append(a)# Obtain angle for each lineangles = [a*180/np.pi for a in angle]# Compute difference between the two linesangle_difference = np.max(angles) - np.min(angles)print(angle_difference)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python