[英]Creating a 'drift/morph/melt' effect on webcam footage using python and opencv
我目前正在做一个学校项目,我正在尝试使用网络摄像头和视频处理效果来模拟 LSD 旅行。 我正在使用 python 和 opencv 来完成此操作,但我无法弄清楚如何创建/应用某种效果,这种效果类似于网络摄像头镜头的“漂移/变形/融化/流动”。
我附上了一个我想要达到的效果的例子。 看起来图像正在慢慢融化和扭曲,几乎就像同时被拉向多个方向一样。
我研究了各种图像处理技术,例如变形、仿射变换和图像混合,但我不确定哪种方法最适合创建这种特定效果。 下面是我尝试过的一些代码行(我对编码很陌生,所以我一直在玩我已经在 inte.net 上找到的东西):
import cv2
import numpy as np
# Capture video from webcam
cap = cv2.VideoCapture(0)
while True:
# Read frame from webcam
ret, frame = cap.read()
# Apply swirling effect
rows, cols = frame.shape[:2]
for i in range(rows):
for j in range(cols):
dx = i - rows // 2
dy = j - cols // 2
distance = np.sqrt(dx**2 + dy**2)
angle = np.arctan2(dy, dx) + distance * 0.1
x = int(rows // 2 + distance * np.cos(angle))
y = int(cols // 2 + distance * np.sin(angle))
if x >= 0 and x < rows and y >= 0 and y < cols:
frame[i, j] = frame[x, y]
# Display the resulting frame
cv2.imshow('Video', frame)
# Break the loop if the user hits 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy the window
cap.release()
cv2.destroyAllWindows()
import cv2
from skimage.transform import swirl
# Create a VideoCapture object to access the webcam
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the webcam
_, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Apply the swirl effect to the frame
swirled = swirl(gray, rotation=0, strength=10, radius=120)
# Display the swirled frame in a window
cv2.imshow('Swirled', swirled)
# Wait for the user to press a key
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
# Release the VideoCapture object and destroy all windows
cap.release()
cv2.destroyAllWindows()
我还找到了一个与某人的链接,该链接使用名为 TouchDesigner 的程序实现了与我正在寻找的效果类似的效果
对于如何使用 python 和 opencv 以及我可能需要的任何其他库来完成此操作的任何建议或指导,我们将不胜感激。
这是一种在 Python/OpenCV/PIL 中从单个图像(或视频帧)创建动画 GIF 的方法。
输入:
import numpy as np
import cv2
from PIL import Image
img = cv2.imread("bluecar_sm.jpg")
# get dimensions
h, w = img.shape[:2]
# set wavelength
wave_x = 2*w
wave_y = h
# set amount, number of frames and delay
amount_x = 10
amount_y = 5
num_frames = 100
delay = 50
border_color = (128,128,128)
# create X and Y ramps
x = np.arange(w, dtype=np.float32)
y = np.arange(h, dtype=np.float32)
frames = []
# loop and change phase
for i in range(0,num_frames):
# compute phase to increment over 360 degree for number of frames specified so makes full cycle
phase_x = i*360/num_frames
phase_y = phase_x
# create sinusoids in X and Y, add to ramps and tile out to fill to size of image
x_sin = amount_x * np.sin(2 * np.pi * (x/wave_x + phase_x/360)) + x
map_x = np.tile(x_sin, (h,1))
y_sin = amount_y * np.sin(2 * np.pi * (y/wave_y + phase_y/360)) + y
map_y = np.tile(y_sin, (w,1)).transpose()
# do the warping using remap
result = cv2.remap(img.copy(), map_x, map_y, cv2.INTER_CUBIC, borderMode = cv2.BORDER_CONSTANT, borderValue=border_color)
# show result
cv2.imshow('result', result)
cv2.waitKey(delay)
# convert to PIL format and save frames
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
pil_result = Image.fromarray(result)
frames.append(pil_result)
# write animated gif from frames using PIL
frames[0].save('bluecar_sm_animation.gif',save_all=True, append_images=frames[1:], optimize=False, duration=delay, loop=0)
这是一个缩小版本,文件大小足够小,可以直接显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.