python - 如何连接侵 eclipse 和膨胀无法连接的虚线?

标签 python opencv image-processing

我有一个这样的图像,它有多个塞子,有些线断了。为了连接这条折线,我使用了如下形态操作:

import cv2
import numpy as np

img = cv2.imread('sample.png', cv2.IMREAD_GRAYSCALE)
morph = cv2.morphologyEx(im, cv2.MORPH_CLOSE, np.ones((10,10),np.uint8))
但这并没有连接我的虚线。如何连接线路而不影响其他线路?
img
换行符是图像中心的两条小线之间的换行符。仅不连续部分没有圆形末端。
image
应用了形态学操作
applied morphological operation

最佳答案


  • 您可以使用createFastLineDetector来检测每行。


  • 计算当前线和相邻线的斜率。


  • 如果当前线和相邻线的斜率是同一绘制线。


  • 初始化线路检测器

    我们将使用ximgproc库检测行。
    import cv2
    
    img = cv2.imread("lines.png")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    d = cv2.ximgproc.createFastLineDetector()
    lines = d.detect(gray)
    
  • lines变量返回类似的值,例如[[14.82, 78.90, 90.89, 120.78]],其中x1=14.82y1=78.90x2=90.89y2=120.78分别。

  • 计算斜率
  • 使用以下公式计算直线的斜率:m =(y2-y1)/(x2-x1)
  • 对于给定的线对象,获取坐标并返回斜率。

  • def calculate_slope(line_object):
        x_point1 = line_object[0]
        y_point1 = line_object[1]
        x_point2 = line_object[2]
        y_point2 = line_object[3]
    
        m = abs((y_point2 - y_point1) / (x_point2 - x_point1))
        m = float("{:.2f}".format(m))
        return m
    

  • 比较坡度

  • 检查行的相等性。如果点相等,则表示它们是同一条线。

  • for current_line in lines:
         current_slope = calculate_slope(current_line[0])
    
         for neighbor_line in lines:
             current_x1 = int(current_line[0][0])
             current_y1 = int(current_line[0][1])
             current_x2 = int(current_line[0][2])
             current_y2 = int(current_line[0][3])
    
             compare_lines = current_line == neighbor_line[0]
             equal_arrays = compare_lines.all()
    


  • 如果线不相等,请计算邻居的线斜率。
    if not equal_arrays:
        neighbor_slope = calculate_slope(neighbor_line[0])
    


  • 如果斜率相等,则画一条线。从neighborcurrentcurrentneighbor
    if abs(current_slope - neighbor_slope) < 1e-3:
        neighbor_x1 = int(neighbor_line[0][0])
        neighbor_y1 = int(neighbor_line[0][1])
        neighbor_x2 = int(neighbor_line[0][2])
        neighbor_y2 = int(neighbor_line[0][3])
    
        cv2.line(img,
                 pt1=(neighbor_x1, neighbor_y1),
                 pt2=(current_x2, current_y2),
                 color=(255, 255, 255),
                 thickness=3)
        cv2.line(img,
                 pt1=(current_x1, current_y1),
                 pt2=(neighbor_x2, neighbor_y2),
                 color=(255, 255, 255),
                 thickness=3)
    


  • 结果

    enter image description here
    可能的问题但是,为什么不能连接以下部分?
    enter image description here
    回答
    好吧,红色虚线的斜率不相等。因此,我无法连接它们。
    可能的问题您为什么不使用dilateerode方法?如here所示
    回答
    我试过了,但结果并不令人满意。

    关于python - 如何连接侵 eclipse 和膨胀无法连接的虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63727525/

    相关文章:

    Python:在二叉搜索树中寻找共同祖先的递归

    c++ - 我有可能使用与查找轮廓相关的编码

    python - 如何使用同一图片的二进制蒙版图像裁剪图像以去除python中的背景?

    python - Python/Numpy 中的扫描线填充算法

    image-processing - 考虑到噪音,在泳池中对游泳者进行对比的最有效方法

    python - Python中是否没有通用的对象序列化方法(转为json)?

    Python错误: need more than one value to unpack

    python-3.x - 如何将 PyCairo 表面转换为 OpenCV numpy 并在 Python 3 中返回

    c++ - OpenCV vector<point> - 没有用于初始化 'cv::Point_<int>' 的匹配构造函数

    python - 如何在 vim 中获取 Python 帮助?就像vim一样使用:h