[英]How can I apply effects like a swirl to a webcam stream using Python and OpenCV?
我正在做一个学校项目,我想通过实时对网络摄像头 stream 应用各种效果来模拟 LSD 旅行。 我正在使用Python和OpenCV来捕获和显示网络摄像头stream。我想应用的效果是:鬼影效果(类似于这个https://www.learnpythonwithrune.org/opencv-python-webcam-create-a-ghost -effect/ ), colors 中更多的活力/饱和度, 轻微的模糊, 扭曲的外部边缘和一种漩涡效果 (类似于这个https://www.alamy.com/stock-photo-checkered-pattern-with-spiral-旋转漩涡失真效果 104721121.html )
我已经能够使用 OpenCV 的 cv2.VideoCapture 和 cv2.imshow 函数捕获和显示网络摄像头 stream。 但是,我无法弄清楚如何将漩涡等效果应用到 stream。
我曾尝试使用 OpenCV 的 cv2.getRotationMatrix2D 和 cv2.warpAffine 函数来应用漩涡效果,但 output 不是我所期望的。
有什么解决办法吗? 或者我应该 go 换个方式。
这是我到目前为止所得到的:
`
import cv2
import numpy as np
# Open the webcam
cap = cv2.VideoCapture(0)
# Initialize the previous frame to None
frame_prev = None
while True:
# Capture a frame from the webcam
ret, frame = cap.read()
# If this is the first frame, store it as the previous frame
if frame_prev is None:
frame_prev = frame.copy()
# Apply the ghost effect by blending the current frame with a slightly transparent previous frame
alpha = 0.5
frame = cv2.addWeighted(frame, 1-alpha, frame_prev, alpha, 0)
# Update the previous frame
frame_prev = frame.copy()
# Apply slight blur
frame = cv2.GaussianBlur(frame, (5, 5), 0)
# Increase saturation
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frame[:,:,1] = np.clip(frame[:,:,1] * 1.5, 0, 255)
frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
# Distort outer edges
height, width = frame.shape[:2]
center = (width//2, height//2)
radius = min(width, height) // 2
mask = np.zeros((height, width), np.uint8)
cv2.circle(mask, center, radius, (255, 255, 255), thickness=-1)
mask = cv2.dilate(mask, kernel=np.ones((radius//10, radius//10), np.uint8))
frame = cv2.GaussianBlur(frame, (3, 3), 0)
frame = cv2.seamlessClone(frame, frame_prev, mask, center, cv2.NORMAL_CLONE)
# Display the result
cv2.imshow('Webcam', frame)
# Check for user input
key = cv2.waitKey(1)
if key == 27: # Esc key to exit
break
# Release the webcam and close all windows
cap.release()
cv2.destroyAllWindows()
`
感谢您的帮助,非常感谢,因为我对编码非常陌生。
以下是如何使用 Python/OpenCV/Skimage 制作漩涡 animation。
输入:
import numpy as np
import cv2
from skimage.transform import swirl
from PIL import Image
img = cv2.imread("mandril.jpg")
# get dimensions
h, w = img.shape[:2]
# get center
cx = w//2
cy = h//2
# set amount, number of frames and delay
max_amount = 20
dist = min(h,w)/2
angle = 0
num_frames = 50
delay = 50
border_color = (128,128,128)
frames = []
# loop and increase swirl
for i in range(0,num_frames):
# compute phase to increment over 360 degree for number of frames specified so makes full cycle
amount = i*max_amount/num_frames
# do swirl
result = swirl(img, center=(cx,cy), rotation=angle, strength=amount, radius=dist, preserve_range=True).astype(np.uint8)
# 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)
# loop and decrease swirl
for i in range(0,num_frames):
# compute phase to increment over 360 degree for number of frames specified so makes full cycle
amount = (num_frames-i)*max_amount/num_frames
# do swirl
result = swirl(img, center=(cx,cy), rotation=angle, strength=amount, radius=dist, preserve_range=True).astype(np.uint8)
# 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('mandril_swirl_animation.gif',save_all=True, append_images=frames[1:], optimize=False, duration=delay, loop=0)
缩小尺寸 Animation:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.