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
0
投稿
猜你喜欢
- 之一:torchvision 中包含了很多预训练好的模型,这样就使得 fine-tune 非常容易。本文主要介绍如何 fine-tune t
- 不是炒冷饭,我添加了很多新的功能哦演示地址: xwinhtcdemo.htmCSS: global.cssHTC: xwin.htc特点:1
- 问题问题1Python是一种动态语言,不支持类型检查。当需要对一个对象执行类型检查时,可能会采用下面的方式:class Foo(object
- 进程池代码演示方式一from multiprocessing import Pooldef deal_task(n): n -= 1 ret
- SqlBulkCopy 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nvarchar。 在网上找了下,大都说是因为数
- 可以用作一些资源的释放。1.在一个函数内的defer执行顺序是先写的后执行,后写的先执行(遵循栈结构)func DeferTest1(){
- 一.图像灰度线性变换原理图像的灰度线性变换是通过建立灰度映射来调整原始图像的灰度,从而改善图像的质量,凸显图像的细节,提高图像的对比度。灰度
- array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组。 当几个数组元素的值相等时
- 下面的各种屏蔽网页鼠标或键盘的代码都是我以前收集的,挺实用的,防一般的访客还是很有用的。1.禁止鼠标选中捕捉网页文字图片等元素在<bo
- CSV文件简单来说CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗
- 在使用DB2以来,碰到了几次出现提示SQL1032N错误,每次出错时出错信息大概如下:11/21/2004 22:15:33 0 0 SQL
- 一、引出问题假如有这两张表,它们中的课程可能价格不一样、周期不一样、等等...不一样...,现在有一张价格策略表,怎么就用一张表报保存它们之
- 动态语言与静态语言有很多不同,最大的特性之一就是可以实现动态的对类和实例进行修改,在Python中,我们创建了一个类后可以对实例和类绑定心的
- 不知各位是否有手写代码的习惯。例如:要在一个单元格插入一段CSS代码,或者一段Javascript代码,怎么做才比较快捷方便呢?虽然Drea
- Hihi, 大家好~ 最近有不少人都提及了网页上该如何选择字体的问题。问题虽然小,但是却是前端开发中的基本,因为目前的网页,还是以文字信息
- 代码: import os while True: dynamic = input('输入计算表达式:') if dynam
- 今天的博客是直接来源于我自己的个人工具函数库。过去几个月,有些PyImageSearch读者电邮问我:“如何获取URL指向的图片并将其转换成
- 前言不要在用手敲生成Excel数据报表了,用Python自动生成Excel数据报表!废话不多说让我们愉快地开始吧~开发工具Python版本:
- 最近在看流畅的python,在看第14章节的itertools模块,对其itertools中的相关函数实现的逻辑的实现其中在zip_long
- php mysql获取表字段名称和字段信息的三种方法先给出本实例中使用的表的信息:使用desc获取表字段信息php代码如下:<?php