美文网首页
扑克牌、纸牌识别实现4-数字区域的查找

扑克牌、纸牌识别实现4-数字区域的查找

作者: 风一般的男人 | 来源:发表于2022-08-24 15:42 被阅读0次

通过二值化后findContours查找区域,简单的过滤长宽比,和大小,可以标识出可能是数字的区域。方便后续进一步处理。

1 Contours函数

当使用findcontours后一般返回是contours, hierarchy两个,contours内包含的是根据设定模式所返回的轮廓。

最常使用:cv2.RETR_EXTERNAL,此模式只检测外轮廓。(输入图像必须先转灰度图再转二值图,这部分的阈值处理必须做好,调整好系数,使图像达到最理想的状态)

2 代码

import numpy as np
import cv2

src = cv2.imread('12.JPG')

image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

//自行调整参数达到最好的效果
threshold_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
    cv2.THRESH_BINARY_INV,11,5)
cv2.imshow('threshold_image',threshold_image)  
contours, w1 = cv2.findContours(threshold_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
for  c in contours:
    rect = cv2.minAreaRect(c)
    w = rect[1][0]
    h = rect[1][1]
    if w*h < 80 or w*h > 500:
        # 过滤太小或太大的运动物体,这类误检概率比较高                
        continue    
    # print(rect[2])
    if w == 0 or h == 0:
        continue 
    wh = w / h
    if wh < 1:
        wh = 1/wh
    c_area = w * h
    box = cv2.boxPoints(rect)
    
    box = np.int0(box)
    # box1 = box            
    if wh > 3:
        continue 
    if wh < 1.5:
        continue     
    cv2.drawContours(src, [box], 0, (0, 0, 255), 2)
cv2.imshow('src',src)    
cv2.waitKey()
image.png

相关文章

网友评论

      本文标题:扑克牌、纸牌识别实现4-数字区域的查找

      本文链接:https://www.haomeiwen.com/subject/jubywrtx.html