add HoughlinesP folder and demo

This commit is contained in:
qiurui 2025-01-16 14:39:54 +08:00
parent 31b980ce63
commit 2442bc395c
9 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,77 @@
import cv2,os
import numpy as np
dir_path = "pic/"
files = os.listdir(dir_path)
print(files)
for file in files:
png_name = "pic/" + file
if os.path.isfile(png_name):
# Read image
image = cv2.imread(png_name)
cv2.namedWindow('Non-Adjacent Lines', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
# Convert image to grayscale
# gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# Use canny edge detection
edges = cv2.Canny(image,10,50,apertureSize=3)
# 使用HoughLinesP检测线段
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=250, maxLineGap=1)
# 如果lines不为空则处理检测到的线段
if lines is not None:
# 将检测到的线段转换为整数坐标
lines = lines.reshape(-1, 4)
# 设置相邻线段的距离阈值
distance_threshold = 20 # 根据实际情况调整这个值
# 用于存储非相邻线段的列表
non_adjacent_lines = []
# 遍历检测到的线段
for i in range(len(lines)):
x1, y1, x2, y2 = lines[i]
is_adjacent = False
# 检查当前线段是否与已存储的非相邻线段相邻
for line in non_adjacent_lines:
lx1, ly1, lx2, ly2 = line
# 计算两条线段之间的最小距离(这里使用简单的欧几里得距离作为示例)
# 注意:这种方法可能不够精确,因为它没有考虑线段的方向和长度
# 在实际应用中,你可能需要使用更复杂的几何方法来计算线段之间的距离
distance = min(
np.linalg.norm([x1 - lx1, y1 - ly1]),
np.linalg.norm([x1 - lx2, y1 - ly2]),
np.linalg.norm([x2 - lx1, y2 - ly1]),
np.linalg.norm([x2 - lx2, y2 - ly2])
)
# 如果距离小于阈值,则标记为相邻
if distance < distance_threshold:
is_adjacent = True
break
# 如果当前线段不是相邻的,则添加到非相邻线段列表中
if not is_adjacent:
non_adjacent_lines.append((x1, y1, x2, y2))
# 在图像上绘制非相邻线段
for line in non_adjacent_lines:
x1, y1, x2, y2 = line
print(x1,x2,y1,y2)
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 显示结果图像
pic_name='pic/result/houghlinesP-distance'+file
cv2.imwrite(pic_name,image)
#cv2.imshow('Non-Adjacent Lines', image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

View File

@ -0,0 +1,49 @@
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
# Read image
image = cv2.imread('pic/1.png')
# Convert image to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# Use canny edge detection
edges = cv2.Canny(gray,100,150,apertureSize=3)
# Apply HoughLinesP method to
# to directly obtain line end points
lines_list =[]
lines = cv2.HoughLinesP(
edges, # Input edge image
1, # Distance resolution in pixels
np.pi/180, # Angle resolution in radians
threshold=100, # Min number of votes for valid line
minLineLength=250, # Min allowed length of line
maxLineGap=1 # Max allowed gap between line for joining them
)
for line in lines:
for x1, y1, x2, y2 in line:
#if x1 == x2 or y1 == y2:
print(x1,x2,y1,y2)
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
'''
# Iterate over points
for points in lines:
# Extracted points nested in the list
x1,y1,x2,y2=points[0]
# Draw the lines joing the points
# On the original image
cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2)
# Maintain a simples lookup list for points
lines_list.append([(x1,y1),(x2,y2)])
'''
# Save the result image
#cv2.imwrite('detectedLines.png',image)
#cv2.imshow("image", gray) ##显示灰度图
#cv2.imshow("image", edges) ##显示canny图
cv2.imshow('img', image)
cv2.waitKey(0)

3
HoughlinesP/README.md Normal file
View File

@ -0,0 +1,3 @@
# line-detection-project
## HoughlinesP
### HoughlinesP为测试程序实际实用程序为HoughlinesP-distance.py

BIN
HoughlinesP/pic/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

BIN
HoughlinesP/pic/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

BIN
HoughlinesP/pic/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB