我试图得到每条直线上每个端点的坐标,但我想不出一个解决方案,这是我目前得到的,但它是找到直线的轮廓,而不是直线本身

floorplan

enter image description here

import cv2
import numpy as np

img = cv2.imread('out copy.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(0,255,0),5)
        
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

cv2.imshow('out copy.png', lines_edges)
cv2.waitKey(0) ```

推荐答案

hit-or-miss transform可用于在骨骼化后查找直线的端点.

代码:

img = cv2.imread('image.png')
img2 = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# inverse binary image, to make the lines in white
th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]    

enter image description here

# obtain binary skeleton
sk = cv2.ximgproc.thinning(th, None, 1)  

enter image description here

# kernels to find endpoints in all 4 directions
k1 = np.array(([0, 0, 0], [-1, 1, -1], [-1, -1, -1]), dtype="int")
k2 = np.array(([0, -1, -1], [0, 1, -1], [0, -1, -1]), dtype="int")
k3 = np.array(([-1, -1, 0],  [-1, 1, 0], [-1, -1, 0]), dtype="int")
k4 = np.array(([-1, -1, -1], [-1, 1, -1], [0, 0, 0]), dtype="int")

# perform hit-miss transform for every kernel
o1 = cv2.morphologyEx(sk, cv2.MORPH_HITMISS, k1)
o2 = cv2.morphologyEx(sk, cv2.MORPH_HITMISS, k2)
o3 = cv2.morphologyEx(sk, cv2.MORPH_HITMISS, k3)
o4 = cv2.morphologyEx(sk, cv2.MORPH_HITMISS, k4)

# add results of all the above 4
out = o1 + o2 + o3 + o4

# find points in white (255) and draw them on original image
pts = np.argwhere(out == 255)
for pt in pts:
    img2 = cv2.circle(img2, (pt[1], pt[0]), 15, (0,255,0), -1)

enter image description here

Python相关问答推荐

Python中两个矩阵的自定义Hadamard风格产物

如何使用Tkinter创建两个高度相同的框架(顶部和底部)?

模型序列化器中未调用现场验证器

Polars:使用列值引用when / then表达中的其他列

如何处理嵌套的SON?

如何自动抓取以下CSV

查找两极rame中组之间的所有差异

Pandas:将多级列名改为一级

如何根据一列的值有条件地 Select 前N个组,然后按两列分组?

如何在Python中找到线性依赖mod 2

不允许访问非IPM文件夹

joblib:无法从父目录的另一个子文件夹加载转储模型

名为__main__. py的Python模块在导入时不运行'

通过ManyToMany字段与Through在Django Admin中过滤

在Python中使用yaml渲染(多行字符串)

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?

人口全部乱序 - Python—Matplotlib—映射

基于另一列的GROUP-BY聚合将列添加到Polars LazyFrame

如何将数据帧中的timedelta转换为datetime

统计numpy. ndarray中的项目列表出现次数的最快方法