如何将运动模糊添加到numpy数组

qqq*_*qqq 3 python image blur python-imaging-library

我有一个图像的numpy数组

因此,有没有一种好的方法: from PIL import Image a = Image.open('img') a = a.filter(MOTION_BLUR)

小智 8

import cv2
import numpy as np

img = cv2.imread('input.jpg')
cv2.imshow('Original', img)

size = 15

# generating the kernel
kernel_motion_blur = np.zeros((size, size))
kernel_motion_blur[int((size-1)/2), :] = np.ones(size)
kernel_motion_blur = kernel_motion_blur / size

# applying the kernel to the input image
output = cv2.filter2D(img, -1, kernel_motion_blur)

cv2.imshow('Motion Blur', output)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到解释


ipe*_*rov 5

绘制一条旋转线作为内核,然后将卷积滤波器应用于具有该内核的图像。

下面的代码使用 opencv 框架。

import cv2
import numpy as np

#size - in pixels, size of motion blur
#angel - in degrees, direction of motion blur
def apply_motion_blur(image, size, angle):
    k = np.zeros((size, size), dtype=np.float32)
    k[ (size-1)// 2 , :] = np.ones(size, dtype=np.float32)
    k = cv2.warpAffine(k, cv2.getRotationMatrix2D( (size / 2 -0.5 , size / 2 -0.5 ) , angle, 1.0), (size, size) )  
    k = k * ( 1.0 / np.sum(k) )        
    return cv2.filter2D(image, -1, k) 
Run Code Online (Sandbox Code Playgroud)