Skip to content

Commit

Permalink
Added Hologram effect by Benjamin Elder
Browse files Browse the repository at this point in the history
  • Loading branch information
allo- committed May 22, 2021
1 parent 1d1b68c commit e754576
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ def apply_filters(frame, mask, part_masks, heatmap_masks, image_filters):
from . import video
from . import webcam
from . import anonymize
from . import hologram
45 changes: 45 additions & 0 deletions filters/hologram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import cv2
import filters
import numpy as np

# Based on the blog post of Benjamin Elder
# https://elder.dev/posts/open-source-virtual-background/
# License: CC-BY 4.0

def shift_image(input_img, dx, dy):
img = np.roll(input_img, dy, axis=0)
img = np.roll(img, dx, axis=1)
if dy>0:
img[:dy, :] = 0
elif dy<0:
img[dy:, :] = 0
if dx>0:
img[:, :dx] = 0
elif dx<0:
img[:, dx:] = 0
return img

class Hologram:
def __init__(self, *args, **kwargs):
pass

def apply(self, *args, **kwargs):
frame = kwargs['frame'].astype(np.uint8)
frame[:,:,:3] = cv2.applyColorMap(frame[:,:,:3], cv2.COLORMAP_WINTER)
frame[:,:,:3] = cv2.cvtColor(frame[:,:,:3], cv2.COLOR_BGR2RGB)
# add a halftone effect
bandLength, bandGap = 2, 3
for y in range(frame.shape[0]):
if y % (bandLength+bandGap) < bandLength:
frame[y,:,:3] = frame[y,:,:3] * np.random.uniform(0.1, 0.3)
# add some ghosting
holo_blur = cv2.addWeighted(frame[:,:,:3], 0.2,
shift_image(frame[:,:,:3], 5, 5), 0.8, 0)
holo_blur = cv2.addWeighted(holo_blur, 0.4,
shift_image(frame[:,:,:3], -5, -5), 0.6, 0)
# combine with the original color, oversaturated
frame[:,:,:3] = cv2.addWeighted(frame[:,:,:3], 0.5, holo_blur, 0.6, 0)
return frame.astype(np.float)


filters.register_filter("hologram", Hologram)

0 comments on commit e754576

Please sign in to comment.