擦除在视频帧边界移动的对象的白色像素(GMM 算法)

Erase white pixels from an object moving at the border of the videoframe (GMM algorithm)

我正在使用 GMM 算法 (BackgroundSubtractorMOG2) 查找视频中的移动对象。这个 GMM 算法的输出是一个二值图像,白色像素作为移动的像素,黑色像素作为背景。我正在寻找一种方法来忽略进入视野的新物体,只要它们没有完全在视野中。这是我创建的示例图片:

这里,所有的白色像素代表移动像素。圆圈已经完全进入视野,而2个三角形刚好进入视野-view.The左下三角几乎完全进入视野,但还不到100 % 在视野中。因此,我仍然希望从框架中完全删除左下角的三角形。有谁知道解决这个问题的方法吗?

谢谢

试试这个:

import numpy as np
import cv2


def edge_filter(image):
    stats = cv2.connectedComponentsWithStats(image, connectivity=8)[2][1:]
    s_x, s_y, s_w, s_h = stats[:, 0], stats[:, 1], stats[:, 2], stats[:, 3]
    res_y, res_x = image.shape
    to_erase = stats[((res_x - s_w - s_x) * (res_y - s_h - s_y) * s_x * s_y) == 0]
    for stat in to_erase:
        x, y, w, h = stat[:4]
        image[y:y+h, x:x+w] = np.zeros((h, w)).astype(np.uint8)


img_in = cv2.imread('bubbles.png',0)
img_out = np.copy(img_in)

edge_filter(img_out)

cv2.imshow('input_image', img_in)
cv2.imshow('output_image', img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()

如果其他多边形不与三角形周围的矩形重叠,则有效。

但是,如果你只需要保持物体的坐标,不接触边缘,那么代码就更简单了。

import numpy as np
import cv2


def edge_filter(stats):
    s_x, s_y, s_w, s_h = stats[:, 0], stats[:, 1], stats[:, 2], stats[:, 3]
    res_y, res_x = img.shape
    return stats[((res_x - s_w - s_x) * (res_y - s_h - s_y) * s_x * s_y) != 0]

img = cv2.imread('bubbles.png',0)
stats = cv2.connectedComponentsWithStats(img, connectivity=8)[2][1:]
filtered_stats = edge_filter(stats)

print('stats:\n', stats)
print('filtered stats:\n', filtered_stats)

输出:

stats:
[[  627    61   169    61  8145]
 [  171   159    85    91  6053]
 [  309   385    41    25   807]
 [  585   385   221   129 22380]
 [    0   457    80   139  6488]
 [  482   599   225   121 13785]]
filtered stats:
[[  627    61   169    61  8145]
 [  171   159    85    91  6053]
 [  309   385    41    25   807]
 [  585   385   221   129 22380]]