gpt4 book ai didi

opencv - 使用opencv获取图像的深色线条

转载 作者:行者123 更新时间:2023-12-02 16:49:33 26 4
gpt4 key购买 nike

我们如何去除图像的较浅线条而仅获得较暗线条?

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

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

ee

最佳答案

如果先进行背景均衡,则可以得到最佳结果,这样我可以使用全局阈值,然后再使用诸如扩张和腐 eclipse 之类的形态运算。

以下示例在Ubuntu 15.10上的python 3.4和opencv 3.1-dev的iPython笔记本中运行:

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 output

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

关于opencv - 使用opencv获取图像的深色线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com