In [1]:
import numpy as np
import skimage
import skimage.io
import skimage.transform
import skimage.morphology
import poie as ps
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
import matplotlib.animation as animation
import cv2
%matplotlib notebook
Loading the background image.
In [2]:
back = skimage.io.imread('backgrounds/Starwars.jpg').astype(float)
Interface for area selection
In [3]:
def onselect(click, release):
pass
fig, ax = plt.subplots(figsize=(10, 7))
ax.imshow(ps.to_uint8(back))
toggle_selector = RectangleSelector(ax, onselect, interactive=True)
cid = fig.canvas.mpl_connect('key_press_event', toggle_selector)
plt.show()
We need this to capture images from webcam:
In [4]:
cam = cv2.VideoCapture(0)
Put the image from webcam proccessed with maximum Laplacian method into the marked area. Unfortunately, the frame is not displayed in real-time. You can take a picture by stopping the cell (click on the shutdown button next to the title Figure 2 ) and then just save the image.
In [5]:
fig, ax = plt.subplots(figsize=(5, 3))
#coordinates of top-left and down-right corners
xlim = np.array(toggle_selector.corners[0][:2]).astype(int)
ylim = np.array(toggle_selector.corners[1][1:3]).astype(int)
shape = (ylim[1] - ylim[0], xlim[1] - xlim[0])
def animate(i):
_, image = cam.read()
if image is not None:
mask = np.zeros(back.shape[:2])
mask[ylim[0]: ylim[1], xlim[0]: xlim[1]] = 1
mask = skimage.morphology.binary_erosion(mask != 0)
#resize image to background shape
image = skimage.transform.resize(image, back.shape).astype(float) * 400
#insert transformed image into background
final = np.zeros_like(image)
final[ylim[0]: ylim[1], xlim[0]: xlim[1]] = skimage.transform.resize(image, shape)[..., ::-1]
# res, _ = ps.poisson(300, mask, back.copy(), ps.laplacian_absmax(final[..., ::-1], back))
res = ps.image_cloning(300, mask, back, final)
ax.imshow(ps.to_uint8(res))
else:
return
ani = animation.FuncAnimation(fig, animate, repeat=0, interval=25)
Maybe you will need to release the webcam:
In [6]:
cam.release()
In [16]:
import os
from IPython.display import Image, display
for file in os.scandir('results'):
if not file.name.startswith('.'):
display(Image(file.path))