Created
November 10, 2017 10:11
-
-
Save Deepayan137/7ff58b5268fc7cd1f0a91f2a2234f1fc to your computer and use it in GitHub Desktop.
image deblurring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import argparse | |
import numpy as np | |
import cmath | |
import pdb | |
import numpy.matlib | |
from math import e | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-i", "--image1", required=True, help="Path to the image") | |
args = vars(ap.parse_args()) | |
from matplotlib import pyplot as plt | |
def deblur(image,**kwargs): | |
H = np.zeros(image.shape) | |
D = np.zeros(image.shape) | |
Do=40 | |
T, a, b = kwargs['T'], kwargs['a'], kwargs['b'] | |
for u in range(1,fshift.shape[0]): | |
for v in range(1,fshift.shape[1]): | |
k = 3.14*(u*a+v*b) | |
j = -cmath.sqrt(-1) | |
si = np.sin(k) | |
H[u,v] = (T/k)*np.sin(k) | |
#*e**(j*k) | |
return H | |
if __name__ == '__main__': | |
img = cv2.imread(args['image1'], 0) | |
kernel = np.zeros((img.shape)) | |
#img = cv2.resize(img, (300,300)) | |
f = np.fft.fft2(img) | |
size= 15 | |
fshift = np.fft.fftshift(f) | |
kernel_motion_blur = np.zeros((size, size)) | |
kernel_motion_blur[int((size-1)/2), :] = np.ones(size) | |
kernel_motion_blur = kernel_motion_blur / size | |
offset_1 = img.shape[0]//2 - kernel_motion_blur.shape[0]//2 | |
offset_2 = img.shape[1]//2 - kernel_motion_blur.shape[0]//2 | |
kernel[offset_1:offset_1+size,offset_1:offset_1+size] = kernel_motion_blur | |
# cv2.imshow('k',kernel) | |
# cv2.waitKey(0) | |
kernel = np.fft.fft2(kernel) | |
kernel = np.fft.fftshift(kernel) | |
magnitude_spectrum = 20*np.log(np.abs(fshift)) | |
h, w = fshift.shape[0], fshift.shape[1] | |
center = (h//2, w//2) | |
H = deblur(fshift, T=1, a=0.01, b=0.01 ) | |
#filtered = np.multiply(fshift, np.linalg.pinv(H)) | |
#filtered = np.multiply(fshift, H) | |
filtered = np.multiply(fshift, kernel) | |
#pdb.set_trace() | |
f_ishift = np.fft.ifftshift(filtered) | |
img_back = np.fft.ifft2(f_ishift) | |
img_back = np.abs(img_back) | |
img_back = np.roll(img_back, img_back.shape[1]//2, axis=1) | |
img_back = np.roll(img_back, img_back.shape[0]//2, axis=0) | |
img_f = np.fft.fftshift(np.fft.fft2(img_back)) | |
filtered_new = np.divide(img_f, kernel) | |
img_back_new = np.abs(np.fft.ifftshift(np.fft.ifft2(filtered_new))) | |
#img_back = 20*np.log(np.abs(filtered)) | |
plt.subplot(131),plt.imshow(img, cmap = 'gray') | |
plt.title('Input Image'), plt.xticks([]), plt.yticks([]) | |
plt.subplot(132),plt.imshow(img_back, cmap = 'gray') | |
plt.title('Output Image'), plt.xticks([]), plt.yticks([]) | |
plt.subplot(133),plt.imshow(img_back_new, cmap = 'gray') | |
plt.title('Fourier'), plt.xticks([]), plt.yticks([]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment