[英]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.