加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在opencv中过滤背景减法中的直线和曲线

发布时间:2020-12-16 07:08:52 所属栏目:百科 来源:网络整理
导读:我正在使用opencv中的背景减法进行对象跟踪.我拍了一个样本足球视频,我的目标是跟踪球员并过滤掉更大的场标记.由于非静态相机,大线也会像在此图像中一样被检测为移动: 我利用霍夫变换来检测线条,在设置了适当的阈值后,能够过滤中途线,图像显示如下: 现在我
我正在使用opencv中的背景减法进行对象跟踪.我拍了一个样本足球视频,我的目标是跟踪球员并过滤掉更大的场标记.由于非静态相机,大线也会像在此图像中一样被检测为移动:

我利用霍夫变换来检测线条,在设置了适当的阈值后,能够过滤中途线,图像显示如下:

现在我担心过滤这两个弧.

问题1.我可以采取哪些方法?如何利用弧度(长而细)和玩家(紧凑的blob)的“属性”的区别?

此外,Hough变换函数有时会报告许多误报(将一个高瘦的玩家视为直线,甚至连接两个玩家以显示更长的线).

问题2.以什么方式指定“待检测”线的最大厚度并保持严格的标准以“仅”检测线?

谢谢.

解决方法

我有一个旧脚本,周围有类似的功能.不幸的是,它是 Python并且不使用Hough变换函数.不过,你可能会发现它很有用.

get_blobs是重要的函数,而__main__是示例用法.

import cv2

def get_blobs(thresh,maxblobs,maxmu03,iterations=1):
    """
    Return a 2-tuple list of the locations of large white blobs.
    `thresh` is a black and white threshold image.
    No more than `maxblobs` will be returned.
    Moments with a mu03 larger than `maxmu03` are ignored.
    Before sampling for blobs,the image will be eroded `iterations` times.
    """
    # Kernel specifies an erosion on direct pixel neighbours.
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
    # Remove noise and thin lines by eroding/dilating blobs.
    thresh = cv2.erode(thresh,kernel,iterations=iterations)
    thresh = cv2.dilate(thresh,iterations=iterations-1)

    # Calculate the centers of the contours.
    contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]
    moments = map(cv2.moments,contours)

    # Filter out the moments that are too tall.
    moments = filter(lambda k: abs(k['mu03']) <= maxmu03,moments)
    # Select the largest moments.
    moments = sorted(moments,key=lambda k: k['m00'],reverse=True)[:maxblobs]
    # Return the centers of the moments.
    return [(m['m10'] / m['m00'],m['m01'] / m['m00']) for m in moments if m['m00'] != 0]

if __name__ == '__main__':
    # Load an image and mark the 14 largest blobs.
    image = cv2.imread('input.png')
    bwImage = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
    trackers = get_blobs(bwImage,14,50000,3)
    for tracker in trackers:
        cv2.circle(image,tuple(int(x) for x in tracker),3,(0,255),-1)
    cv2.imwrite('output.png',image)

从您的第一张图片开始:

该算法使用erosion将blob与线分开.

然后使用Moments过滤掉高大和小的斑点.时刻也用于定位每个斑点的中心.

get_blobs返回玩家位置的2元组列表.您可以在最后一张图片上看到它们.

就目前而言,剧本非常混乱.随意直接使用它,但我发布它主要是为了给你一些想法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读