如何使用python opencv测量同一图像中两条线之间的角度? [英] How to measure the angle between 2 lines in a same image using python opencv?

查看:274
本文介绍了如何使用python opencv测量同一图像中两条线之间的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用霍夫变换检测到了一条不是笔直的车道边界线,然后分别提取了该线.然后与另一条具有直线的图像混合.现在,我需要计算这两条线之间的角度,但是我不知道这两条线的坐标.因此,我尝试使用给出垂直线坐标的代码,但是它不能专门标识那些坐标.有没有一种方法可以测量这些线之间的角度?这是我的坐标计算代码和两行混合图像

I have detected a lane boundary line which is not straight using hough transform and then extracted that line separately. Then blended with another image that has a straight line. Now I need to calculate the angle between those two lines, but I do not know the coordinates of those lines. So I tried with code that gives the coordinates of vertical lines, but it can not specifically identify those coordinates. Is there a way to measure the angle between those lines? Here is my coordinate calculation code and blended image with two lines

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)

推荐答案

一种方法是使用霍夫变换来检测直线并获取每条直线的角度.然后,通过减去两条线之间的差可以找到两条线之间的角度.

One approach is to use the Hough Transform to detect the lines and obtain the angle of each line. The angle between the two lines can then be found by subtracting the difference between the two lines.

我们首先使用np.mean进行算术平均,从本质上对产生该图像的图像进行阈值处理.

We begin by performing an arithmetic average using np.mean to essentially threshold the image which results in this.

image = cv2.imread('2.png')

# Compute arithmetic mean
image = np.mean(image, axis=2)

现在,我们执行 skimage.transform.hough_line 检测线

Now we perform skimage.transform.hough_line to detect lines

# Perform Hough Transformation to detect lines
hspace, angles, distances = hough_line(image)

# Find angle
angle=[]
for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):
    angle.append(a)

接下来,我们获得每条线的角度,并找到差值以获得结果

Next we obtain the angle for each line and find the difference to obtain our result

# Obtain angle for each line
angles = [a*180/np.pi for a in angle]

# Compute difference between the two lines
angle_difference = np.max(angles) - np.min(angles)
print(angle_difference)

16.08938547486033

16.08938547486033

完整代码

from skimage.transform import (hough_line, hough_line_peaks)
import numpy as np
import cv2

image = cv2.imread('2.png')

# Compute arithmetic mean
image = np.mean(image, axis=2)

# Perform Hough Transformation to detect lines
hspace, angles, distances = hough_line(image)

# Find angle
angle=[]
for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):
    angle.append(a)

# Obtain angle for each line
angles = [a*180/np.pi for a in angle]

# Compute difference between the two lines
angle_difference = np.max(angles) - np.min(angles)
print(angle_difference)

这篇关于如何使用python opencv测量同一图像中两条线之间的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆