0

我们怎样才能去除图像中较亮的线条而只得到较暗的线条?

下图中有一个数独游戏(取自此处)。自适应高斯阈值提供了非常好的结果,但它也包括灰色(较浅)线。

注意:我尝试了 canny,但它也删除了数字。

ee

4

1 回答 1

4

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

以下示例在我的 iPython 笔记本中运行,在 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()

jupyter 输出

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

于 2016-08-31T13:28:10.857 回答