opencv画圆与棋盘图
程序员文章站
2022-05-22 11:21:39
...
画出的效果如下:
代码
import os
import cv2
import numpy as np
board_pixel_width=200
ncols=9
nrows=6
if __name__=="__main__":
image_width=(ncols+1)*board_pixel_width
image_height=(nrows+1)*board_pixel_width
res=np.zeros((image_height,image_width,1),np.uint8)
res[:,:,:]=255
list=[]
for i in range(4):
for j in range(nrows):
if i==0 and j==0:
x_point=int( #最左上角的圆心位置
(image_width-board_pixel_width)/ncols*i+board_pixel_width)
y_point=int(
(image_height-board_pixel_width)/nrows*j+board_pixel_width)
else: #其它点的圆心位置,在9*6网格图的点中会有上下左右偏移最多50像素的可能性
x_point=int(
(image_width-board_pixel_width)/ncols*i+board_pixel_width+np.random.randint(-board_pixel_width//4,
board_pixel_width//4))
y_point=int((image_height-board_pixel_width)/nrows*j+board_pixel_width+np.random.randint(
-board_pixel_width//4,board_pixel_width//4))
list.append([x_point,y_point]) #将这个点加入list中
cv2.circle(res,(x_point,y_point),50,0,-1) #画这个圆,半径50,颜色黑,实心的
for i in range(-10,11): #画左上角十字
res[50+i,50]=0
res[50,50+i]=0
for i in range(-10,11): #画左下角十字
res[image_height-50+i,50]=0
res[image_height-50,50+i]=0
for i in range(-10,11): #画中间十字
res[image_height//2+i,image_width//2]=0
res[image_height//2,image_width//2+i]=0
with open("dot1.txt","w") as file: #写文件,写入所有圆心
for p in list:
file.writelines("{} {}\n".format(p[0],p[1]))
with open("world.txt","w") as file:
file.writelines("{} {}\n".format(50,50))
file.writelines("{} {}\n".format(image_height-50,50))
file.writelines("{} {}\n".format(image_height//2,image_width//2))
nnrows=6
nncols=4
for row in range(0, nnrows):
for col in range(0, nncols):
if (row + col) % 2 == 0:
prow = row#空出行边界
pcol = col#空出列边界
# print row, col
res[100+int(prow * board_pixel_width):100+int((prow + 1) * board_pixel_width),1100+int(pcol * board_pixel_width):1100+int((pcol + 1) * board_pixel_width), :] = 0#把相应块的行列范围变成黑色
cv2.imwrite("picture.jpg",res)
print(50,image_height-50)
上一篇: 线程的同步与互斥
下一篇: 在OpenCV里实现棋盘生成
推荐阅读