opencv:分割透明边框

标签 opencv computer-vision image-segmentation

我正在尝试在游戏屏幕中查找事件对象,如下所示:

enter image description here

事件意味着它们有灰白色边框,所以这里的正方形位于左上角,五张卡片位于中间。

这乍一看很简单,但边框是半透明的并且是渐变的,因此实际的灰度值在很大程度上取决于背景,范围可以从 ~180 到 240。只需 inRange() 处理所有这些值就会产生很多噪音。这是边框特写供引用:

enter image description here

然后我尝试为每条边使用一个模板进行模板匹配,例如对于右边缘,我采用一堆黑色边框像素和旁边 4 个灰色像素的渐变,例如

enter image description here

然后我在模板匹配结果上添加阈值,它有点起作用:

    k = ['right', 'left', 'top', 'bottom']
    mode = cv2.TM_CCOEFF_NORMED
    matches = {}
    addimg = []

    for side in k:
        template = cv2.imread('./img/ab_' + side + '.png', cv2.IMREAD_GRAYSCALE)
        matches[side] = cv2.matchTemplate(im0, template, mode)
        v  = cv2.inRange(matches[side], 0.987, 1)
        #Tools.show(side, v)
        addimg.append(v)

    im1 = sum(addimg)

enter image description here

但是获取 TM 系数的正确值仍然很困难。此外,当对象较大时,边框渐变比我在模板中使用的灰色像素更宽,因此匹配会变得更糟。

总而言之,我认为我缺少一种可以匹配不同大小和强度的梯度的智能算法。有什么好主意吗?

PS https://github.com/rc9000/modoscrape/tree/master/img 中有更多这样的屏幕截图

最佳答案

好的,这是我的两分钱。这与梯度检测无关,而是如何检测这些卡的另一个想法。
我认为关于如何检测事件卡的唯一线索就是这个边框。当然,您可以尝试检测梯度之类的东西,但我的解决方案依赖于这样一个事实
a/边框可以通过简单的“inRange()”与图像的其余部分明显分离(作为一个组件)[在 Piglet 的评论后进行编辑:这也可以在黑色上工作 - 并且可能会更容易边框而不是渐变] b/边框具有特定的形状,尤其是其周围的边界矩形将是直的并且具有特定的比例。我的意思是,由于您总是选择一张扑克牌,因此它的高/宽比将始终相同。

所以我的想法是
1/阈值
enter image description here
2/查找组件
3/找到这些组件的边界矩形
enter image description here
4/仅选择具有特定比例的边界矩形 enter image description here

代码如下。它有点“快速而肮脏”,有些东西可能会被优化。例如,我没有检查矩形方向,这是一个很好的线索。此外,您可能对卡片的尺寸有所了解,即使一张图像与另一张图像的尺寸可能有所不同。另外,您可以消除其他矩形内的矩形,或者明显小于其他矩形的矩形...

将此视为探索的“另一种方式”,而不是交 key 解决方案:)

import cv2
import sys
import numpy as np
import csv

#just converting formats of numpy arrays to pass it from one cv2 function to another.
def convert_for_bounding(coords):
    nb_pts=len(coords[0])
    coordz=np.zeros((nb_pts,2))
    for i in range(nb_pts):
        coordz[i,:]=np.array([int(coords[0][i]),int(coords[1][i])])
    return coordz

#finding width and length of bounding boxes
def find_wid(xs):
    maxx=0
    for i in range(4):
        for j in range(i+1,4):
            if abs(xs[i]-xs[j])>=maxx:
                maxx=abs(xs[i]-xs[j])
    return maxx


img=cv2.imread(your image)
orig=np.copy(img)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
h,w=img.shape

#thresholding with your "180 - 240" range
img = cv2.inRange(img, 180, 240)

#finding all components
nb_edges, output, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8)
size_edges = stats[1:, -1]; nb_edges = nb_edges - 1
contours=[]
for i in range(0, nb_edges):
    #eliminating small components
    if size_edges[i]>=100:
        img2=np.zeros((h,w))
        img2[output == i + 1] = 255
        contours.append(convert_for_bounding(np.nonzero(img2)))



#finding bounding rectangle for each component
for i in range(0,len(contours)):
    c=np.array(contours[i]).astype(int)
    ar=cv2.minAreaRect(c)
    box = cv2.boxPoints(ar)
    box = np.int0([box[:,1],box[:,0]]).T
    xs=box[:,0]
    ys=box[:,1]
    wid=find_wid(xs)
    hei=find_wid(ys)

    #for each rectangle, we'll check if its ratio is like a card one
    card_ratio = 285 / 205
    if hei!=0:
        if hei/wid <=card_ratio*1.05 and hei/wid >= card_ratio*0.95:
            cv2.drawContours(orig, [box], -1, (0,0,255), 2)

结果(必须缩小尺寸才能在此答案中上传): enter image description here

关于opencv:分割透明边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43116220/

相关文章:

python - SWIG Python 绑定(bind)到本地代码不适用于 OpenCV 2.1

image-processing - 全卷积层如何用于分割任务?

c++ - 'compareHist' 不适用于类似图像

opencv - 非连接形态过滤器

machine-learning - 解释 Caffe FCN 输出类

python - 在 OpenCV-Python 中绘制直方图

machine-learning - 具有多个掩模的 U-Net 图像分割

matlab - 如何显示具有某些特定属性阈值(例如 'Area')的连接组件图像

image-processing - 如何将 x,y 像素映射到世界坐标

python - "error: (-215) ssize.area() > 0 in function cv::resize"的大图像上的 OpenCV 调整大小失败