Skip to content

Instantly share code, notes, and snippets.

@RicherMans
Created November 20, 2014 10:52
Show Gist options
  • Save RicherMans/f45ad569fc90b608e1d1 to your computer and use it in GitHub Desktop.
Save RicherMans/f45ad569fc90b608e1d1 to your computer and use it in GitHub Desktop.
Problemset5 DIP, Creating a motion blur filter, add some guassian noise and then restore the image
import argparse
from scipy import misc
from scipy.ndimage.filters import convolve as convolveim
import numpy as np
import sys
from numpy import real
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('inputimage', type=misc.imread)
parser.add_argument('-a',help='the parameter a in the equation, which is equal to b',default=0.1,type=float)
parser.add_argument('-T',help='the parameter T',default=1,type=float)
# parser.add_argument('-o',help='outputs the produced image')
return parser.parse_args()
def main():
args = parseArgs()
filteredimg = filterimg(args.inputimage,args.a,args.a,args.T)
misc.imsave('blurred.jpg',filteredimg )
blur_noise_img = filteredimg + gaussiannoise(0, 650, filteredimg)
misc.imsave('blurred_w_gauss.jpg',blur_noise_img)
def gaussiannoise(mean,var,data):
return np.random.normal(mean,var,data.shape)
def blurringkernel(shape,T,a,b):
m, n = [(ss - 1.) / 2. for ss in shape]
x, y= np.ogrid[-m:m + 1, -n:n + 1]
# x= np.zeros(shape)
# y = np.ones(shape)
# h=np.zeros(shape,dtype=complex)
# for u in range(len(h)):
# for v in range(len(h[0])):
# h[u][v] = (T/ np.pi*(u*a+v*b)) * np.pi * np.sin(u*a+v*b) * np.exp(1j*np.pi*(u*a+v*b))
# return h
return (T/np.pi*(x*a+y*b)) * np.sin(np.pi*(x*a+y*b)) * np.exp(-1j*np.pi*(x*a+y*b))
def blurringfilter(dftarr, a,b,T, shape):
kernel = blurringkernel(shape,T,a,b)
return applykernel(dftarr,kernel)
def wienerkernel(shape,T,a,b):
# Currently hardcoded the value K
h = blurringkernel(shape, T, a, b)
return h.T/((h*h)+0.1)
def wienerfilter(dftarr,a,b,T,shape):
kernel = wienerkernel(shape,T,a,b)
return applykernel(dftarr, kernel)
'''
Inverse filter is calculated using F^ = G(u,v) H(u,v), since we know H(u,v) , which is the blurring function
we can simply return the inverse of H(u,v)
'''
def inversekernel(shape,T,a,b):
return np.linalg.inv(blurringkernel(shape, T, a, b))
def inversefilter(dftarr, a,b,T, shape):
kernel = inversekernel(a,b,T, shape)
def applykernel(arr,kernel):
transformedimg = np.copy(arr)
transformedimg *= kernel
# offset = len(kernel)/2
# for i in range(offset,len(arr)-offset):
# for j in range(offset,len(arr[0])-offset):
# weightedavg = 0
# for p in range(len(kernel)):
# for q in range(len(kernel)):
# weightedavg += transformedimg[i+(p-offset)][j+(q-offset)] * kernel[p][q]
# if weightedavg<0:
# weightedavg = 0
# transformedimg[i][j] = weightedavg
return transformedimg
def extractReal(img):
return np.array([[real(img[j][i]) for i in range(len(img))] for j in range(len(img[0]))])
def filterimg(imagearray,a,b,T):
dftrans = np.fft.fft2(imagearray)
filteredimg = blurringfilter(dftrans, a,b,T, imagearray.shape)
inversefft= np.fft.ifft2(filteredimg)
return extractReal(inversefft)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment