Advertisement

【python】OpenCV—Extract Horizontal and Vertical Lines—Morphology

阅读量:
在这里插入图片描述

文章目录

  • 1、功能描述
  • 2、代码实现
  • 3、效果展示
  • 4、完整代码
  • 5、参考
  • 6、其他的直线检测算法

更多有趣的代码示例,可参考【Programming】


1、功能描述

基于 opencv-python 库,利用形态学的腐蚀和膨胀,提取图片中的水平或者竖直线条

2、代码实现

导入基本的库函数

复制代码
    import numpy as np
    import cv2 as cv
    
    
    python
    
    

读入图片(https://raw.githubusercontent.com/opencv/opencv/5.x/doc/tutorials/imgproc/morph_lines_detection/images/src.png),增加读错图片的判断机制

1.jpg
在这里插入图片描述

复制代码
    def main(save=False):
    # Load the image
    src = cv.imread("./1.jpg", cv.IMREAD_COLOR)
    
    # Check if image is loaded fine
    if src is None:
        print('Error opening image')
        return -1
    
    
    python
    
    

可视化图片,并将其转化为灰度图

复制代码
    # Show source image
    cv.imshow("src", src)
    # [load_image]
    
    # [gray]
    # Transform source image to gray if it is not already
    if len(src.shape) != 2:
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    else:
        gray = src
    
    if save:
        cv.imwrite("gray.jpg", gray)
        
    # Show gray image
    show_wait_destroy("gray", gray)
    # [gray]
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/ime3uvks5bYVtaGyAMJgdOQIK01r.png)

gray.jpg
在这里插入图片描述

show_wait_destroy 实现如下 ,关闭图片后才后续代码

复制代码
    def show_wait_destroy(winname, img):
    cv.imshow(winname, img)
    cv.moveWindow(winname, 500, 0)
    cv.waitKey(0)
    cv.destroyWindow(winname)
    
    
    python
    
    

二进制求反灰度图, 并自适应阈值二值化

复制代码
    # [bin]
    # Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
    gray = cv.bitwise_not(gray)
    if save:
        cv.imwrite("bitwise_not_gray.jpg", gray)
    
    bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                              cv.THRESH_BINARY, 15, -2)
    if save:
        cv.imwrite("adaptiveThreshold.jpg", bw)
        
    # Show binary image
    show_wait_destroy("binary", bw)
    # [bin]
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/qT3Cxg7yJsUrkpcHo1nWiuwN6XPY.png)

bitwise_not_gray.jpg
在这里插入图片描述
adaptiveThreshold.jpg
在这里插入图片描述

复制图片 adaptiveThreshold.jpg ,准备提取水平线和竖直线

复制代码
    # [init]
    # Create the images that will use to extract the horizontal and vertical lines
    horizontal = np.copy(bw)
    vertical = np.copy(bw)
    # [init]
    
    
    python
    
    

提取水平线

复制代码
    # [horiz]
    # Specify size on horizontal axis
    cols = horizontal.shape[1]  # 1024 cols
    horizontal_size = cols // 30  # 34
    
    # Create structure element for extracting horizontal lines through morphology operations
    horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
    
    # Apply morphology operations
    horizontal = cv.erode(horizontal, horizontalStructure)
    if save:
        cv.imwrite("erode-horizontal.jpg", horizontal)
    
    horizontal = cv.dilate(horizontal, horizontalStructure)
    if save:
        cv.imwrite("dilate-horizontal.jpg", horizontal)
    
    # Show extracted horizontal lines
    show_wait_destroy("horizontal", horizontal)
    # [horiz]
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/1tiFZRnHaN9EALPhzI6W8cOgfBwm.png)

首先会构建结构元素 horizontalStructure(定义了形态学操作的邻域形状和大小)

图片列数 // 30 得到全为 1 的数组

复制代码
    array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=uint8)
    
    
    python
    
    

接着腐蚀操作 erode-horizontal.jpg
在这里插入图片描述

最后膨胀操作 dilate-horizontal.jpg
在这里插入图片描述

至此我们就提取到了图片中的水平方向的线条

接下来我们提取竖直方向的线条

复制代码
     	# [vert]
    # Specify size on vertical axis
    rows = vertical.shape[0]  # 134
    verticalsize = rows // 30  # 4
    
    # Create structure element for extracting vertical lines through morphology operations
    verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
    
    # Apply morphology operations
    vertical = cv.erode(vertical, verticalStructure)
    if save:
        cv.imwrite("erode-vertical.jpg", vertical)
    
    vertical = cv.dilate(vertical, verticalStructure)
    if save:
        cv.imwrite("dilate-vertical.jpg", vertical)
    
    # Show extracted vertical lines
    show_wait_destroy("vertical", vertical)
    # [vert]
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/io6U8OkSpHBxrwDEIaL7ZzmYTNne.png)

同理,也是先构建一个结构元素 verticalStructure

复制代码
    array([[1],
       [1],
       [1],
       [1]], dtype=uint8)
    
    
    python
    
    

腐蚀 erode-vertical.jpg
在这里插入图片描述

膨胀 dilate-vertical.jpg
在这里插入图片描述
至此我们提取出了竖直方向的线条


可以拓展一下,

As you can see we are almost there. However, at that point you will notice that the edges of the notes are a bit rough. For that reason we need to refine the edges in order to obtain a smoother result

复制代码
    '''
    Extract edges and smooth image according to the logic
    1. extract edges
    2. dilate(edges)
    3. src.copyTo(smooth)
    4. blur smooth img
    5. smooth.copyTo(src, edges)
    '''
    
    
    python
    
    

dilate-vertical.jpg 二进制求反,

复制代码
    # [smooth]
    # Inverse vertical image
    vertical = cv.bitwise_not(vertical)
    if save:
        cv.imwrite("bitwise_not_vertical.jpg", vertical)
    
    show_wait_destroy("vertical_bit", vertical)
    
    
    python
    
    

bitwise_not_vertical.jpg
在这里插入图片描述
cv2.adaptiveThreshold 适应性阈值二值化

复制代码
    # Step 1
    edges = cv.adaptiveThreshold(vertical, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                                 cv.THRESH_BINARY, 3, -2)
    if save:
        cv.imwrite("step1_edges.jpg", edges)
    show_wait_destroy("edges", edges)
    
    
    python
    
    

得到 step1_edges.jpg,实现了边缘检测
在这里插入图片描述

看看 cv2.adaptiveThreshold 的介绍仔细分析下实现过程

复制代码
    dst = cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
    
    
    python
    
    
在这里插入图片描述

形参 C 从邻域像素的平均值或加权平均值中减去的常数,配置的为负数,附近颜色相近的变黑(eg 纯白区域,像素 255,阈值 255-(-2)=257,都变黑,再 eg,纯黑区域,像素 0,阈值 0-(-2)=2,也是黑),附近颜色变动的变白(黑白交替,白色的部分保留,黑色的部分变黑),可以实现边缘提取,妙

膨胀强化边缘

复制代码
    # Step 2
    kernel = np.ones((2, 2), np.uint8)
    edges = cv.dilate(edges, kernel)
    if save:
        cv.imwrite("step2_edges.jpg", edges)
    show_wait_destroy("dilate", edges)
    
    
    python
    
    

kernel

复制代码
    array([[1, 1],
       [1, 1]], dtype=uint8)
    
    
    python
    
    

step2_edges.jpg
在这里插入图片描述

复制 bitwise_not_vertical.jpg

复制代码
    # Step 3
    smooth = np.copy(vertical)
    
    
    python
    
    

模糊处理 step4_smooth.jpg

复制代码
    # Step 4
    smooth = cv.blur(smooth, (2, 2))
    if save:
        cv.imwrite("step4_smooth.jpg", smooth)
    
    
    python
    
    
在这里插入图片描述

记录下 step2_edges.jpg 中像素不为零的部分的坐标,也即边缘部分坐标

边缘部分用平滑后的像素替换原来的像素

复制代码
    # Step 5
    (rows, cols) = np.where(edges != 0)
    vertical[rows, cols] = smooth[rows, cols]
    
    # Show final result
    show_wait_destroy("smooth - final", vertical)
    if save:
        cv.imwrite("smooth_final.jpg", vertical)
    # [smooth]
    
    
    python
    
    
在这里插入图片描述

3、效果展示

输入
在这里插入图片描述

水平线条
在这里插入图片描述

竖直线条
在这里插入图片描述

平滑竖直线条后的结果
在这里插入图片描述

输入图片
在这里插入图片描述

水平线
在这里插入图片描述

竖直线
在这里插入图片描述

平滑竖直线条后的结果
在这里插入图片描述

4、完整代码

复制代码
    """
    @brief Use morphology transformations for extracting horizontal and vertical lines sample code
    """
    import numpy as np
    import cv2 as cv
    
    
    def show_wait_destroy(winname, img):
    cv.imshow(winname, img)
    cv.moveWindow(winname, 500, 0)
    cv.waitKey(0)
    cv.destroyWindow(winname)
    
    
    def main(save=False):
    # Load the image
    src = cv.imread("./1.jpg", cv.IMREAD_COLOR)
    
    # Check if image is loaded fine
    if src is None:
        print('Error opening image')
        return -1
    
    # Show source image
    cv.imshow("src", src)
    # [load_image]
    
    # [gray]
    # Transform source image to gray if it is not already
    if len(src.shape) != 2:
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    else:
        gray = src
    
    if save:
        cv.imwrite("gray.jpg", gray)
    
    # Show gray image
    show_wait_destroy("gray", gray)
    # [gray]
    
    # [bin]
    # Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
    gray = cv.bitwise_not(gray)  # (134, 1024)
    if save:
        cv.imwrite("bitwise_not_gray.jpg", gray)
    
    bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                              cv.THRESH_BINARY, 15, -2)
    if save:
        cv.imwrite("adaptiveThreshold.jpg", bw)
    
    # Show binary image
    show_wait_destroy("binary", bw)
    # [bin]
    
    # [init]
    # Create the images that will use to extract the horizontal and vertical lines
    horizontal = np.copy(bw)
    vertical = np.copy(bw)
    # [init]
    
    # [horiz]
    # Specify size on horizontal axis
    cols = horizontal.shape[1]  # 1024 cols
    horizontal_size = cols // 30  # 34
    
    # Create structure element for extracting horizontal lines through morphology operations
    horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
    
    # Apply morphology operations
    horizontal = cv.erode(horizontal, horizontalStructure)
    if save:
        cv.imwrite("erode-horizontal.jpg", horizontal)
    
    horizontal = cv.dilate(horizontal, horizontalStructure)
    if save:
        cv.imwrite("dilate-horizontal.jpg", horizontal)
    
    # Show extracted horizontal lines
    show_wait_destroy("horizontal", horizontal)
    # [horiz]
    
    # [vert]
    # Specify size on vertical axis
    rows = vertical.shape[0]  # 134
    verticalsize = rows // 30  # 4
    
    # Create structure element for extracting vertical lines through morphology operations
    verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
    
    # Apply morphology operations
    vertical = cv.erode(vertical, verticalStructure)
    if save:
        cv.imwrite("erode-vertical.jpg", vertical)
    
    vertical = cv.dilate(vertical, verticalStructure)
    if save:
        cv.imwrite("dilate-vertical.jpg", vertical)
    
    # Show extracted vertical lines
    show_wait_destroy("vertical", vertical)
    # [vert]
    
    # [smooth]
    # Inverse vertical image
    vertical = cv.bitwise_not(vertical)
    if save:
        cv.imwrite("bitwise_not_vertical.jpg", vertical)
    
    show_wait_destroy("vertical_bit", vertical)
    
    '''
    Extract edges and smooth image according to the logic
    1. extract edges
    2. dilate(edges)
    3. src.copyTo(smooth)
    4. blur smooth img
    5. smooth.copyTo(src, edges)
    '''
    
    # Step 1
    edges = cv.adaptiveThreshold(vertical, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                                 cv.THRESH_BINARY, 3, -2)
    if save:
        cv.imwrite("step1_edges.jpg", edges)
    show_wait_destroy("edges", edges)
    
    # Step 2
    kernel = np.ones((2, 2), np.uint8)
    edges = cv.dilate(edges, kernel)
    if save:
        cv.imwrite("step2_edges.jpg", edges)
    show_wait_destroy("dilate", edges)
    
    # Step 3
    smooth = np.copy(vertical)
    
    # Step 4
    smooth = cv.blur(smooth, (2, 2))
    if save:
        cv.imwrite("step4_smooth.jpg", smooth)
    
    # Step 5
    (rows, cols) = np.where(edges != 0)
    vertical[rows, cols] = smooth[rows, cols]
    
    # Show final result
    show_wait_destroy("smooth - final", vertical)
    if save:
        cv.imwrite("smooth_final.jpg", vertical)
    # [smooth]
    
    return 0
    
    
    if __name__ == "__main__":
    main(save=True)
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/tQSgZ9IqX3T6mwf4Ynr0MEGbHdeJ.png)

5、参考

6、其他的直线检测算法

来自 直线检测算法汇总

  • LSD 直线检测算法,cv2.createLineSegmentDetector
  • FLD 直线检测算法,cv2.ximgproc.createFastLineDetector
  • EDlines 直线检测算法
  • LSWMS 直线检测算法
  • CannyLines 直线检测算法
  • MCMLSD 直线检测算法
  • LSM 直线检测算法

更多有趣的代码示例,可参考【Programming】

全部评论 (0)

还没有任何评论哟~