[英]Erase white pixels from an object moving at the border of the videoframe (GMM algorithm)
試試這個:
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.