python识别围棋定位棋盘位置
作者:翟羽嚄 发布时间:2023-01-09 01:49:34
标签:python,围棋,定位,棋盘位置
最近需要做一个围棋识别的项目,首先要将棋盘位置定位出来,效果图如下:
效果图
原图
中间处理效果
最终结果
思路分析
我们利用python opencv的相关函数进行操作实现,根据棋盘颜色的特征,寻找到相关特征,将棋盘区域抠出来。最好从原始图像中将棋盘位置截取出来。
源码:定位棋盘位置
from PIL import ImageGrab
import numpy as np
import cv2
from glob import glob
imglist = sorted(glob("screen/*.jpg"))
for i in imglist:
# while 1:
img = cv2.imread(i)
image = img.copy()
w,h,c = img.shape
img2 = np.zeros((w,h,c), np.uint8)
img3 = np.zeros((w,h,c), np.uint8)
# img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower = np.array([10,0,0])
upper = np.array([40,255,255])
mask = cv2.inRange(hsv,lower,upper)
erodeim = cv2.erode(mask,None,iterations=2) # 腐蚀
dilateim = cv2.dilate(erodeim,None,iterations=2)
img = cv2.bitwise_and(img,img,mask=dilateim)
frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow("0",img)
i = 0
maxarea = 0
nextarea = 0
maxint = 0
for c in contours:
if cv2.contourArea(c)>maxarea:
maxarea = cv2.contourArea(c)
maxint = i
i+=1
#多边形拟合
epsilon = 0.02*cv2.arcLength(contours[maxint],True)
if epsilon<1:
continue
#多边形拟合
approx = cv2.approxPolyDP(contours[maxint],epsilon,True)
[[x1,y1]] = approx[0]
[[x2,y2]] = approx[2]
checkerboard = image[y1:y2,x1:x2]
cv2.imshow("1",checkerboard)
cv2.waitKey(1000)
cv2.destroyAllWindows()
带保存图像
from PIL import ImageGrab
import numpy as np
import cv2
from glob import glob
import os
imglist = sorted(glob("screen/*.jpg"))
a=0
for i in imglist:
# while 1:
a=a+1
img = cv2.imread(i)
image = img.copy()
w,h,c = img.shape
img2 = np.zeros((w,h,c), np.uint8)
img3 = np.zeros((w,h,c), np.uint8)
# img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower = np.array([10,0,0])
upper = np.array([40,255,255])
mask = cv2.inRange(hsv,lower,upper)
erodeim = cv2.erode(mask,None,iterations=2) # 腐蚀
dilateim = cv2.dilate(erodeim,None,iterations=2)
img = cv2.bitwise_and(img,img,mask=dilateim)
frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 保存图片的地址
img_file_1 = "./temp"
# 确认上述地址是否存在
if not os.path.exists(img_file_1):
os.mkdir(img_file_1)
cv2.imshow("0",img)
cv2.imwrite(img_file_1 + "/" + 'temp_%d.jpg'%a, img)
i = 0
maxarea = 0
nextarea = 0
maxint = 0
for c in contours:
if cv2.contourArea(c)>maxarea:
maxarea = cv2.contourArea(c)
maxint = i
i+=1
#多边形拟合
epsilon = 0.02*cv2.arcLength(contours[maxint],True)
if epsilon<1:
continue
#多边形拟合
approx = cv2.approxPolyDP(contours[maxint],epsilon,True)
[[x1,y1]] = approx[0]
[[x2,y2]] = approx[2]
checkerboard = image[y1:y2,x1:x2]
cv2.imshow("1",checkerboard)
cv2.waitKey(1000)
# 保存图片的地址
img_file_2 = "./checkerboard"
# 确认上述地址是否存在
if not os.path.exists(img_file_2):
os.mkdir(img_file_2)
cv2.imwrite(img_file_2 + "/" + 'checkerboard_%d.jpg'%a, checkerboard)
cv2.destroyAllWindows()
来源:https://blog.csdn.net/mao_hui_fei/article/details/118363347


猜你喜欢
- 背景Translation插件在mac上idea能够搜索到,但是在windows上却搜索不到解决勾选上成功了。来源:https://blog
- Pytorch的backward()函数假若有多个loss函数,如何进行反向传播和更新呢? x = torch.tensor(2.0, re
- 在建模时模型的超参数对精度有一定的影响,而设置和调整超参数的取值,往往称为调参。在实践中调参往往依赖人工来进行设置调整范围,然后使用机器在超
- 方法一:也是最简单的 直接使用pd.to_datetime函数实现data['交易时间'] = pd.to_datetime
- 本文的目的是探讨JS相关技术,并不是以杀毒为主要目的,杀毒只是为讲解一些JS做铺垫的,呵呵,文章有点长,倒杯咖啡或者清茶慢慢看,学习切勿急躁
- 前言相信大家在网上经常看到有人秀出各种各样的字符画,对于这个五彩斑斓的世界来说,我们日常看到的都是一些高清的彩色的图片,偶尔来个粗糙的黑白的
- 有时在处理文件时候,我们需要将中文姓名转换为拼音,或者是转换为拼音首字母,本文将介绍三种Python实现的方法,最后给一个批量操作的代码案例
- 如下所示://用普通文本文件方式打开和操作with open("'file.csv'") as cf:
- 1、事件概述在MySQL 5.1中新增了一个特色功能事件调度器(Event Scheduler),简称事件。它可以作为定时任务调度器,取代部
- Ø 基本常用查询 --select select * from student; --all 查询所有 select
- --按日 select sum(consume),day([date]) from consume_record where year([d
- 一、假设有这样一个原始dataframe二、提取索引(已经做了一些操作将Age为NaN的行提取出来并合并为一个dataframe,这里提取的
- 万事万物都有自身存在的道理,虽然在python中有些代码我们并不常见。经常会觉得不是常用,就可以避之,但是大家好像都遗忘了一件事情,就是“真
- socket解析HTTP请求内容思路1. 解析HTTP请求的头部HTTP请求头部的结束符行为"\r\n",可以按行读取H
- 问题详情:偶然在根目录创建文件夹的时候,突然显示错误,当时很惊讶,以前没遇见过这样的问题。当时界面是这样的。 用了一个 cd / 命令从用户
- 本文实例为大家分享了python实现烟花小程序的具体代码,供大家参考,具体内容如下'''FIREWORKS SIMU
- 一个简单的tokenizer分词(tokenization)任务是Python字符串处理中最为常见任务了。我们这里讲解用正则表达式构建简单的
- 废话不多说,直接开干!需要库pip install openaiimport openai# Set your API keyopenai.
- 之前,我介绍了学习安装并配置前端自动化工具Gulp,觉得gulp确实比grunt的配置简单很多,于是我决定再深入学习一下gulp,就去网上查
- 学完了net/http和fasthttp两个HTTP协议接口的客户端实现,接下来就要开始Server的开发,不学不知道一学吓一跳,居然这两个