问题描述
我正在使用 GMM 算法 (BackgroundSubtractormog2) 在视频中查找移动对象。这个 GMM 算法的输出是一个二值图像,白色像素作为移动的像素,黑色像素作为背景。我正在寻找一种方法来忽略进入视野的新物体,只要它们不完全在视野中。这是我创建的示例图片:
这里,所有白色像素都代表移动像素。圆圈完全在视野内,而2个三角形即将进入视野。左下三角几乎完全进入视野,但仍然不是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],2],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,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]]