Grace Under Pressure
  • Blog
  • About
  • Act Now
  • Updates
  • THESIS
  • Contact




BLOG BY GRACE C. YOUNG
                                                             
                                                      

Blur Image in Python Without openCV

7/19/2017

Comments

 
It's not secret that installing openCV is by far the most annoying part about using it... but once it's running on your machine it's great! That said, to make a simple script super user-friendly I'd rather not require (or assume) that a user already has compiled openCV. I recently found myself only needing the function cv2.blur from openCV. To implement the same function without openCV, you can use the function scipy.ndimage.filters.convolve; e.g.: 
import numpy as np
from scipy import ndimage

A = np.ones((10,10))
pix_blur = (5,5)
k = k = np.ones(pix_blur) / float(pix_blur[0]*pix_blur[1])
B = ndimage.convolve(A, k, mode='mirror')​
In the above example, the array B would be the same as from: 
import cv2 

A = np.ones((10,10))
pix_blur = (5,5)
B = cv2.blur(A, pix_blur) ​​
That said, cv2.blur is marginally faster (it's written C++). e.g., I found that with an 255 x 512 array, cv2.blur was on average 0.008 seconds faster than ndimage.convolve as used above (tested on n = 50 different arrays)---and of course that scales for your larger images. So you might consider writing your code with both options -- e.g., in your import statements have something like:
openCV = False 
try: 
    import cv2 
    openCV = True 
except: 
    from scipy import ndimage ​

This was developed with Chedy Raïssi. Hope it helps someone. Please post any comments below. 
Comments

    Author

    Same author, Grace. This is where I make technical notes, usually related to my PhD. My slightly more exciting blog, Grace Under Pressure, is here. 

Grace under pressure

 MORE BLOG (ALL)        WHAT'S M31 ABOUT?        UNDERSEA GRADUATION