【问题标题】:Get darker lines of an image using opencv使用opencv获取图像的较暗线条
【发布时间】:2016-08-31 21:28:10
【问题描述】:

我们如何去除图像中较亮的线条而只得到较暗的线条?

下图中有一个数独游戏(取自here)。 Adaptive Gaussian Thresholding 提供了非常好的结果,但它也包括灰色(较浅)线。

注意:我试过 canny,但它也会删除数字。

【问题讨论】:

    标签: opencv adaptive-threshold


    【解决方案1】:

    如果我先进行背景均衡,我会得到最好的结果,这样我就可以使用全局阈值,然后使用扩张和侵蚀等形态学操作。

    以下示例在我的 iPython notebook 中运行,在 Ubuntu 15.10 上使用 python 3.4 和 opencv 3.1-dev:

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    image = cv2.imread('sudokubig.jpg', 0)
    
    if image is None:
        raise ValueError('Image not found!')
    
    # background equalization
    max_value = np.max(image)
    backgroundRemoved = image.astype(float)
    blur = cv2.GaussianBlur(backgroundRemoved, (151,151), 50)
    backgroundRemoved = backgroundRemoved/blur
    backgroundRemoved = (backgroundRemoved*max_value/np.max(backgroundRemoved)).astype(np.uint8)
    
    
    fig = plt.figure(figsize=(20, 20))
    plt.subplot(311),plt.imshow(image, 'gray'),plt.title('Input'),plt.axis('off')
    plt.subplot(312),plt.imshow(backgroundRemoved, 'gray'),plt.title('Background Removed'),plt.axis('off')
    
    ret, thres = cv2.threshold(backgroundRemoved,130,255,cv2.THRESH_BINARY)
    
    # remove horizontal lines
    kernel = np.ones((4, 1),np.uint8)
    dilation1 = cv2.dilate(thres, kernel, iterations = 1)
    
    # remove vertical lines
    kernel = np.ones((1, 4),np.uint8)
    dilation2 = cv2.dilate(dilation1, kernel, iterations = 1)
    
    kernel = np.ones((3, 3),np.uint8)
    erosion = cv2.erode(dilation2, kernel, iterations = 1)
    
    plt.subplot(313),plt.imshow(erosion, 'gray'),plt.title('Final'),plt.axis('off')
    plt.show()
    
    kernel = np.ones((1, 4),np.uint8)
    dilation = cv2.dilate(dilation, kernel, iterations = 1)
    
    kernel = np.ones((3, 3),np.uint8)
    erosion = cv2.erode(dilation, kernel, iterations = 1)
    
    fig = plt.figure() 
    plt.imshow(erosion, cmap='gray'),plt.title('missmatch')
    plt.show()
    

    也许你会找到更聪明的方法或更好的参数。我很想在这里看到你的改进,但我希望这个简短的 sn-p 对你有所帮助。

    【讨论】:

    • 哇完美。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2020-12-05
    • 2018-08-19
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多