import numpy as np
import cv2 as cv
# 生成棋盘格
def generateChessBoard(xSize=32,ySize=32,w=6,h=6):
'''
:param xSize: 棋盘尺寸
:param ySize: 棋盘尺寸
:param w: 横向角点个数
:param h: 纵向角点个数
:return:
'''
w,h=w+1,h+1
board=np.zeros((xSize*(w),ySize*(h),3),np.uint8)
board.fill(255)
for i in range(h):
for j in range(w):
ty = i * ySize
if i&1: # 奇数行
tx=xSize+j*2*xSize
board[tx:tx+xSize,ty:ty+ySize,:]=0
else: # 偶数行
tx=j*2*xSize
board[tx:tx+xSize,ty:ty+ySize,:]=0
# 添加边缘
y=np.zeros((xSize*w,ySize,3))
x=np.zeros((xSize,ySize*(h+2),3))
y.fill(255)
x.fill(255)
board=np.hstack([y,board,y])
board=np.vstack([x,board,x])
return board
b=generateChessBoard(100,100,6,6)
cv.imshow("name",b)
cv.waitKey(0)
cv.imwrite(r"F:\newChessBoard.jpg",b)
结果如下图所示: