[英]how to get 4 points of the sudoku grid(sudoku solver project)
我正在研究數獨求解器的項目,但我被卡住了。 首先我必須從圖像中提取網格。 我正在測試的圖像是:我剛剛測試我的代碼的數獨圖像!
為了從圖像中得到最大網格,我應用了drawcontours、cannyedge檢測和houghlines。 在應用了輪廓線之后,我得到了所有的線條。
這是代碼: import cv2 import numpy as np def get_sudo_rid(name,size):
img=name
original=img.copy()
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
graymain=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
ath=cv2.adaptiveThreshold(graymain,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,39,10)
contours,hierarchy=cv2.findContours(ath,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
area=[]
maxarea=0
cnt=contours[0]
for i in contours:
if cv2.contourArea(i)>maxarea:
cnt=i
maxarea=cv2.contourArea(i)
blank= np.zeros(img.shape,np.uint8)
image=cv2.drawContours(blank,[cnt],-1,(255,255,255),5)
edges=cv2.Canny(image,40,150)
lines=cv2.HoughLines(edges,1,np.pi/180,100)
print(len(lines))
print(lines)
createhor=[]
createver=[]
created=[]
anglediff= 800
rhodiff= 800
flag=0
count = 2
for line in lines:
for(rho,theta) in line:
flag=0
for (rho1,theta1) in created:
if(abs(rho-rho1) < rhodiff and abs(theta-theta1)<anglediff):
flag=1
if(flag==0):
a=np.cos(theta)
b=np.sin(theta)
xo=a*rho
yo=b*rho
x1=int( xo + 1000*(-b))
y1=int( yo + 1000*(a))
x2=int( xo + 1000*(-b))
y2=int( yo +1000*(a))
print(x1,x2,y1,y2)
從中我得到 11 分:-957 -957 532 532、-956 -956 539 539、-1002 -1002 115 115、-1002 -1002 108 108、116 116 -1004 -1004、435 435 -1015 -1015、428 428 -1015 -1015, 123 123 -1004 -1004, -1000 -1000 138 138, -946 -946 560 560, -1000 -1000 131 131,
之后,為了獲得水平和垂直線,我應用了基本的 if-else 條件:但它不起作用:
d=np.linalg.norm(np.array((x1,y1,0))-np.array((x2,y2,0)))
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
m=abs(1/np.tan(theta))
if(m<1):
createhor.append((rho,theta))
else:
createver.append((rho,theta))
print(len(createhor))
print(len(createver))
points=[]
for (rho,theta) in createhor:
for (rho1,theta1) in createver:
if(rho,theta)!=(rho1,theta1):
a=[[np.cos(theta),np.sin(theta)],[np.cos(theta1),np.sin(theta1)]]
b=[rho,rho1]
cos=np.linalg.solve(a,b)
if list(cos) not in points:
points.append(list(cos))
#print(a,b)
print(len(points))
points.sort()
output - createhor- 7,
創建者 - 4
我試圖 append 圖像中數獨的四點列表:但我沒有得到 4 點,點的大小是:28
我什至試圖消除那些anglediff和rhoddiff超過10的線條,但即使在那個長度之后點仍然保持28
你能幫我得到 sudoky gird 的 4 個角點嗎?
這是使用cv2.findContours
、 cv2.approxPolyDP
和最后透視變換的一種方法:
import cv2
from imutils.perspective import four_point_transform
image = cv2.imread('test_sudoku.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)[:,:,2]
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(gray, 50, 200)
cnts = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:1]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
warped = four_point_transform(image, screenCnt.reshape(4, 2))
cv2.imshow("Warped", warped)
cv2.waitKey(0)
output 變形網格:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.