2

I would like to add artificial smudge / motion blur effects in a specific direction to images with OpenCV to simulate blurring caused by shaking/moving cameras while recording images.

What would be an appropriate way to do so in OpenCV (with Python)?

Example image:

enter image description here

daniel451
  • 10,626
  • 19
  • 67
  • 125
  • 3
    translate the image according to some camera movement and use alpha blending with fixed alpha. Use cv::addWeighted – Micka Nov 26 '16 at 11:28
  • Here is implementation (in C++) http://stackoverflow.com/questions/40713929/weiner-deconvolution-using-opencv/40789986#40789986 check convolveDFT method. Kernel will describe trajectory of motion. – Andrey Smorodov Nov 27 '16 at 10:17

1 Answers1

2

To achieve this effect, you convolve the image with a line segment-like kernel (or PSF, a Point Spread Function), like this:

img = cv2.imread("Lenna.png")

psf = np.zeros((50, 50, 3))
psf = cv2.ellipse(psf, 
                  (25, 25), # center
                  (22, 0), # axes -- 22 for blur length, 0 for thin PSF 
                  15, # angle of motion in degrees
                  0, 360, # ful ellipse, not an arc
                  (1, 1, 1), # white color
                  thickness=-1) # filled

psf /= psf[:,:,0].sum() # normalize by sum of one channel 
                        # since channels are processed independently

imfilt = cv2.filter2D(img, -1, psf)

To be more realistic you need to perform blurring in linear domain (i.e. invert then re-apply sRGB gamma, see here, it's a whole another can of worms), but this simple code gets you the following:

Source:

lenna orig

Result:

lenna blurred

ansgri
  • 2,126
  • 5
  • 25
  • 37