簡體   English   中英

使用圖像處理的填字游戲數字化

[英]Crossword digitization using image processing

我是圖像處理的新手。我需要將圖像文件中的填字游戲網格轉換成相應的二進制等價物,即輸出應該是一個數組,其中黑色方塊為1,白色方塊為0。 此外,圖像中的所有其他無關信息(如文本等)都將被忽略,並僅對網格進行數字化處理。

角點檢測算法可以幫助我們檢測填字游戲的角落,然后使用強力數據相應地對像素塊進行數字化嗎?這是最好的方法嗎?還是有任何有效的方法來完成任務? 我更喜歡基於python的解決方案。

填字游戲示例

我想你不需要在這里使用角點檢測。 只需使用輪廓本身,您就可以解決它(如果您的圖像是直接的)。 下面是一個代碼,用於打印上面圖像的數組。 代碼評論如下:

import numpy as np
import cv2

img = cv2.imread('cross.jpg')    
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)    
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
thresh2 = cv2.bitwise_not(thresh)

contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 1)

max_area = -1

# find contours with maximum area
for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt,True), True)
    if len(approx) == 4:
        if cv2.contourArea(cnt) > max_area:
            max_area = cv2.contourArea(cnt)
            max_cnt = cnt
            max_approx = approx

# cut the crossword region, and resize it to a standard size of 130x130
x,y,w,h = cv2.boundingRect(max_cnt)
cross_rect = thresh2[y:y+h, x:x+w]
cross_rect = cv2.resize(cross_rect,(130,130))

# you need to uncomment these lines if your image is rotated
#new_pts = np.float32([[0,0], [0,129],[129,129],[129,0]])
#old_pts = max_approx.reshape(4,2).astype('float32')
#M = cv2.getPerspectiveTransform(old_pts,new_pts)
#cross_rect = cv2.warpPerspective(thresh2,M,(130,130))

cross = np.zeros((13,13))

# select each box, if number of white pixels is more than 50, it is white box
for i in xrange(13):
    for j in xrange(13):
        box = cross_rect[i*10:(i+1)*10, j*10:(j+1)*10]
        if cv2.countNonZero(box) > 50:
            cross.itemset((i,j),1)

print cross

我為上面的圖像得到了這樣的輸出:

[[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  0.  1.  0.  1.  0.  1.  0.  0.  0.  1.  0.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  0.  1.  1.  1.  1.]
 [ 1.  0.  1.  0.  1.  0.  1.  0.  1.  0.  1.  0.  1.]
 [ 1.  1.  1.  1.  1.  0.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  0.  0.  0.  1.  0.  1.  0.  1.  0.  1.  0.  1.]
 [ 1.  1.  1.  1.  0.  0.  0.  0.  0.  1.  1.  1.  1.]
 [ 1.  0.  1.  0.  1.  0.  1.  0.  1.  0.  0.  0.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  0.  1.  1.  1.  1.  1.]
 [ 1.  0.  1.  0.  1.  0.  1.  0.  1.  0.  1.  0.  1.]
 [ 1.  1.  1.  1.  0.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  0.  1.  0.  0.  0.  1.  0.  1.  0.  1.  0.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  0.]]

我編寫了一個代碼來從圖像中提取數獨細節,並在下面的鏈接中詳細解釋(教程不完整)。 您可以參考它們獲取更多詳細信息:

http://opencvpython.blogspot.com/2012/06/sudoku-solver-part-1.html

http://opencvpython.blogspot.com/2012/06/sudoku-solver-part-2.html

http://opencvpython.blogspot.com/2012/06/some-common-questions.html

角點檢測可能會在文本上找到角落等 - 您必須嘗試這樣做。

我首先使用Hough變換查找行http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html 一旦檢測到所有直線,就可以通過找到直線的交點來找到方形的角。 一旦知道了正方形的位置,就可以很容易地確定正方形是黑色還是白色。

您也可以編寫Hough變換來檢測矩形,但這可能是不必要的並發症。

無論你是用Python,C ++,Java做的,都沒有多大區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM