Skip to content

Instantly share code, notes, and snippets.

@vettukal
Created November 1, 2022 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vettukal/2ba81d41c6a56e53635c9a3518c17b18 to your computer and use it in GitHub Desktop.
Save vettukal/2ba81d41c6a56e53635c9a3518c17b18 to your computer and use it in GitHub Desktop.
Simple function to fill foreground object after subtraction of two images.
# @njit
def fill_vertical_box2(img, kr, kc):
is0, is1, ks0, ks1 = *img.shape, kr, kc
is0, is1, ks0, ks1 # (5, 4, 3, 1)
rs0, rs1 = is0 - ks0 + 1, is1 - ks1 + 1
rs0, rs1 # (3, 4)
for i in range(rs0):
for j in range(rs1):
le = (i+ks0)
for kle in range(le-1, i-1, -1):
if img[kle,j] == 1:
le = kle
break
last_elem = img[le-1,j]
if last_elem == 1:
# print(f'found 1 at {i}, {j}')
min_row = img.shape[0] + 1
for kk in range(le-2, i-1, -1):
if img[kk,j] == 1:
min_row = kk
# print(f' {i}, {j}, {min_row}')
for kj in range(min_row, le):
img[kj,j] = 1
return img
### to fill vertically with size of 32
result = fill_vertical_box2(result, 32, 1)
plt.figure(figsize = (8,8))
plt.imshow(result)
## to fill horizontally with size of 32
result = result.T
i12 = fill_vertical_box2(result, 16, 1)
result = result.T
plt.figure(figsize = (8,8))
plt.imshow(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment