2016-08-30 158 views

回答

2

我得到的最好的結果,如果我做一個背景均衡第一,所以我可以使用全局閾值,然後利用形態學操作,如擴張和侵蝕。

下面的示例運行在我的IPython的筆記本使用Python 3.4和OpenCV 3.1-dev的在Ubuntu 15.10:

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

也許你會找到一個更聰明的方式或更好PARAMS。我很想在這裏看到你的改進,但我希望這個短片能夠幫助你一點點。

+0

哇完美。謝謝。 –