Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active December 18, 2015 12:59
Show Gist options
  • Select an option

  • Save jayrambhia/5787222 to your computer and use it in GitHub Desktop.

Select an option

Save jayrambhia/5787222 to your computer and use it in GitHub Desktop.
Numpy Tricks
import numpy as np
# Get the numpy array
import cv2
img = cv2.imread("Lenna.png")
# Get 3 layers
layer1 = img[:,:,0]
layer2 = img[:,:,1]
layer3 = img[:,:,2]
# Stacking 3 layers
stackednp = np.dstack((layer1, layer2, layer3))
# Thresholding
threshVal = 128
binImg = (threshVal)*np.ones(img.shape)
threshImg = 255*np.array(img < binImg, dtype='uint8')
# Circular filters
size = (512, 512)
cen = (255, 255)
w, h = size
cen = (w/2, h/2)
a, b = cen
y, x = np.ogrid[-a:w-a, -b:h-b]
r = dia1/2
mask = x*x + y*y <= r*r
flt = np.ones((w, h))
flt[mask] = 255
# Gaussian Filters
size = (512, 512)
sz_x, sz_y = size
x0 = sz_x/2
y0 = sz_y/2
X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
D = np.sqrt((X-x0)**2+(Y-y0)**2)
flt = 255*np.exp(-0.5*(D/dia)**2)
# Butterworth filters
size = (512, 512)
sz_x, sz_y = size
x0 = sz_x/2
y0 = sz_y/2
X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
D = np.sqrt((X-x0)**2+(Y-y0)**2)
flt = 255/(1.0 + (D/dia)**(order*2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment