Должен быть лучший способ.Но здесь, используя интерполяцию и итерацию.
import matplotlib.pyplot as plt
from scipy import misc, interpolate
# Show the image ---------------- |
im = misc.face().copy()
plt.imshow(f)
# Make the V shape ---------------- |
x1 = [200, 400, 600]
y1 = [0, 300, f.shape[0]]
# Fit spline
tck = interpolate.splrep(x1, y1, k=2)
xx1 = range(min(x1), max(x1))
yy1 = interpolate.splev(xx1, tck)
# Repeat
x2 = [700, 850, 960]
y2 = [f.shape[0], 200, 0]
# Fit spline
tck = interpolate.splrep(x2, y2, k=2)
xx2 = range(min(x2), max(x2))
yy2 = interpolate.splev(xx2, tck)
# Plot splines ---------------- |
plt.plot(xx1, yy1, 'r-', lw=4)
plt.plot(xx2, yy2, 'r-', lw=4)
# Solution - Mask the sides
xx_interp = range(im.shape[0])
yy_interp1 = np.round(np.interp(xx_interp, yy1, xx1)).astype(int)
yy_interp2 = np.round(np.interp(xx_interp, yy2[::-1], xx2[::-1])).astype(int)
for y, x1, x2 in list(zip(xx_interp, yy_interp1, yy_interp2)):
im[y, :x1, :] = 0
im[y, x2:, :] = 0
plt.imshow(im);