簡體   English   中英

從視頻幀邊界移動的 object 中擦除白色像素(GMM 算法)

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

我正在使用 GMM 算法 (BackgroundSubtractorMOG2) 來查找視頻中的移動對象。 這個 GMM 算法的 output 是一個二值圖像,白色像素作為移動的像素,黑色像素作為背景。 我正在尋找一種方法來忽略進入視野的新物體,只要它們沒有完全在視野中。 這是我創建的示例圖片: 在此處輸入圖像描述

這里,所有的白色像素代表移動像素。 圓圈完全進入視野,而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], 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)

output:

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]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM