opencv with cuda GaussianBlur on a video in python
时间: 2023-12-03 21:44:58 浏览: 195
To apply GaussianBlur on a video using OpenCV with CUDA in Python, you can use the following code:
```
import cv2
# Load the video
cap = cv2.VideoCapture('input_video.mp4')
# Create a CUDA enabled GaussianBlur object
blur = cv2.cuda.createGaussianFilter(cap.get(cv2.CAP_PROP_FRAME_HEIGHT), cap.get(cv2.CAP_PROP_FRAME_WIDTH), (3, 3), 0, cv2.CV_32F)
# Process each frame of the video
while True:
# Capture a frame
ret, frame = cap.read()
if not ret:
break
# Convert the frame to a CUDA enabled matrix
src = cv2.cuda_GpuMat(frame)
# Apply GaussianBlur on the frame
dst = blur.apply(src)
# Convert the CUDA enabled matrix to a numpy array
result = dst.download()
# Display the resulting frame
cv2.imshow('frame', result)
# Wait for key press
if cv2.waitKey(1) == ord('q'):
break
# Release the video and cleanup
cap.release()
cv2.destroyAllWindows()
```
In this code, we first load the video using `cv2.VideoCapture()`. We then create a CUDA enabled GaussianBlur object using `cv2.cuda.createGaussianFilter()`. We then process each frame of the video in a loop, where we first convert the frame to a CUDA enabled matrix using `cv2.cuda_GpuMat()`. We then apply GaussianBlur on the CUDA enabled matrix using `blur.apply()`. We then convert the CUDA enabled matrix to a numpy array using `dst.download()`. Finally, we display the resulting frame using `cv2.imshow()` and wait for a key press using `cv2.waitKey()`.
阅读全文