002 棋盘切成小片+判断是否有圆+颜色提取+截取圆形

002 棋盘切成小片+判断是否有圆+颜色提取+截取圆形

棋盘切成小片:在之前已经是规范的图形了,所以这里切成小片也是规范的。

判断是否有圆:有圆的就是有棋子的位置,否则就是空位置。

颜色提取:棋子分红色和黑色的,但因为光影的影响,有一些经验数值,结合整个棋盘的颜色平均值,分类两种颜色的棋子。

截取圆形:根据之前判断是否有圆的函数(某个地方似乎我有理解错误,总是出现[0,0]坐标,打了个小补丁),将棋子的圆形规范的截取了出来。

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import Image
import sys
import os, math

import argparse

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片


def cut_image(image):
    print("把图片切成小块")
    width,height=image.size
    #print(width,height)
    item_width=int(width/11)
    #print(item_width)
    item_height=int(height/12)
    #print(item_height)
    box_list=[]
    count=0
    for j in range(12):
        for i in range(11):
            count+=1
            #box=(i*item_width,j*item_height,(i+1)*item_width,(j+1)*item_height)
            box=((i*item_width)-20,(j*item_height)-20,((i+1)*item_width)+20,((j+1)*item_height)+20)
            # 适当多切一点 以免摆放不规则的出了画面 上下左右多20个像素 方便把圆找出来
            #print(count)
            box_list.append(box)
    #print(count)
    image_list=[image.crop(box) for box in box_list]
    return image_list

def get_color_c():
    #获取这张图片的颜色
    img_path = 'c.png'
    image = Image.open(img_path) 
    # 要提取的主要颜色数量
    num_colors = 1
    small_image = image.resize((80, 80))
    result = small_image.convert('P', palette=Image.ADAPTIVE, colors=num_colors) 
    result = result.convert('RGB')
    # result.show() # 显示图像
    main_colors = result.getcolors(80*80)
    print(main_colors)
    return main_colors[0][1][0] #只返回红色 

def save_images(image_list):
    print("保存切出的小块")
    index=1
    RGB_list=[]    
    theR = get_color_c()+37 #在c.png的R值基础上加上37 感觉应该可以区分出黑色和红色
    for images in image_list:
        print(index)
        #images_yuan = img_to_circle(im
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值