Python 大致将图像上的线条分类为垂直或水平
假设我有一个使用Python 大致将图像上的线条分类为垂直或水平,python,opencv,math,image-processing,computational-geometry,Python,Opencv,Math,Image Processing,Computational Geometry,假设我有一个使用cv2.HoughLinesP从cv2.Canny边缘检测器获得的边缘掩码提取的线坐标列表 lines = [[x1,y1,x2,y2] , ... ] 如果直线的坡度在±60以内,则将其归类为水平◦ 水平面 方向。所有其他斜坡将被丢弃 import numpy as np import cv2 def detect_line_angle(line): x1, y1, x2, y2 = line angle = np.arctan2(x2 - x1, y2
cv2.HoughLinesP
从cv2.Canny
边缘检测器获得的边缘掩码提取的线坐标列表
lines = [[x1,y1,x2,y2] , ... ]
如果直线的坡度在±60以内,则将其归类为水平◦ 水平面
方向。所有其他斜坡将被丢弃
import numpy as np
import cv2
def detect_line_angle(line):
x1, y1, x2, y2 = line
angle = np.arctan2(x2 - x1, y2 - y1)
# angle = angle * 180 / 3.14
return angle
def get_lines_from_edge_mask(edge_mask):
result = []
lines = cv2.HoughLinesP(edge_mask, 1, np.pi / 180, 30, maxLineGap=5)
for line in lines:
result.append(line[0])
return result
def is_horizontal(theta, delta=1.05):
return True if (np.pi - delta) <= theta <= (np.pi + delta) or (-1 * delta) <= theta <= delta else False
def is_vertical(theta, delta=0.09):
return True if (np.pi / 2) - delta <= theta <= (np.pi / 2) + delta or (
3 * np.pi / 2) - delta <= theta <= (
3 * np.pi / 2) + delta else False
def distance(line):
dist = np.sqrt(((line[0] - line[2]) ** 2) + ((line[1] - line[3]) ** 2))
return dist
def split_lines(lines):
v_lines = []
h_lines = []
for line in lines:
line_angle = detect_line_angle(line)
dist = distance(line)
if dist > 30:
if is_vertical(line_angle):
v_lines.append(line)
if is_horizontal(line_angle):
h_lines.append(line)
return v_lines, h_lines
如果直线的坡度在±5°范围内,则将其归类为垂直线◦ 垂直的
方向。所有其他斜坡将被丢弃
import numpy as np
import cv2
def detect_line_angle(line):
x1, y1, x2, y2 = line
angle = np.arctan2(x2 - x1, y2 - y1)
# angle = angle * 180 / 3.14
return angle
def get_lines_from_edge_mask(edge_mask):
result = []
lines = cv2.HoughLinesP(edge_mask, 1, np.pi / 180, 30, maxLineGap=5)
for line in lines:
result.append(line[0])
return result
def is_horizontal(theta, delta=1.05):
return True if (np.pi - delta) <= theta <= (np.pi + delta) or (-1 * delta) <= theta <= delta else False
def is_vertical(theta, delta=0.09):
return True if (np.pi / 2) - delta <= theta <= (np.pi / 2) + delta or (
3 * np.pi / 2) - delta <= theta <= (
3 * np.pi / 2) + delta else False
def distance(line):
dist = np.sqrt(((line[0] - line[2]) ** 2) + ((line[1] - line[3]) ** 2))
return dist
def split_lines(lines):
v_lines = []
h_lines = []
for line in lines:
line_angle = detect_line_angle(line)
dist = distance(line)
if dist > 30:
if is_vertical(line_angle):
v_lines.append(line)
if is_horizontal(line_angle):
h_lines.append(line)
return v_lines, h_lines
将numpy导入为np
进口cv2
def检测线角度(线):
x1,y1,x2,y2=直线
角度=np.arctan2(x2-x1,y2-y1)
#角度=角度*180/3.14
返回角
def从边缘遮罩(边缘遮罩)获取线:
结果=[]
lines=cv2.HoughLinesP(边缘遮罩,1,np.pi/180,30,maxLineGap=5)
对于行中的行:
result.append(第[0]行)
返回结果
def水平(θ,δ=1.05):
如果(np.pi-delta)没有使用坐标差的密集三角函数,则返回真值
t5 = tan(5*Pi/180) calculated once
t60 = Sqrt(3)/2 calculated once
Vertical: dy != 0 and abs(dx/dy) < t5
Horizontal: dx != 0 and abs(dy/dx) < t60
t5=tan(5*Pi/180)计算一次
t60=Sqrt(3)/2计算一次
垂直:dy!=0和abs(dx/dy)
@MarkSetchell这不是我想要的…假设检测线角度做了它声称的事情,你需要在距离上进行测试做什么?为什么水平度和垂直度的测试如此复杂,而不是一个简单的-60@YvesDaoust,这家伙提供了一个设计师网站链接。正如我前面提到的-这不是我想要的。那么如果numpy返回弧度,就可以了?@arturkuchynski:我想你误解了他为什么提供这个链接。简单而优雅的回答。非常感谢你。