1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import cv2 from cv2 import norm import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import balanced_accuracy_score from numpy import fft
image = cv2.imread('test.jpg') img_h, img_w = image.shape[:2]
def normal(array): array = np.where(array < 0, 0, array) array = np.where(array > 255, 255, array) array = array.astype(np.int16) return array
def MotionKernel(img_h, img_w,size): blur_kernel=np.zeros((img_h, img_w )) blur_kernel[int((img_h - 1)/2):int((img_h - 1)/2)+size, int((img_w - 1)/2)] = np.ones(size) return blur_kernel/size
def InvFilter(img,kernel): input_fft = fft.fft2(img) PSF_fft = fft.fft2(kernel) result = fft.ifft2(input_fft / PSF_fft) result = np.abs(fft.fftshift(result)) return result
def WienerFilter(img,kernel,K=0.01): input_fft = fft.fft2(img) PSF_fft = fft.fft2(kernel) PSF_fft_1 = np.conj(PSF_fft) / (np.abs(PSF_fft) ** 2 + K) result = fft.ifft2(input_fft * PSF_fft_1) result = np.abs(fft.fftshift(result)) return result
blur_kernel=MotionKernel(img_h,img_w,11) blurred = cv2.filter2D(image, -1, blur_kernel)
b_gray, g_gray, r_gray = cv2.split(blurred.copy()) Result=[] Result2=[] for gray in [b_gray, g_gray, r_gray]: channel =InvFilter(gray,blur_kernel) channel2 =WienerFilter(gray,blur_kernel) Result.append(normal(channel)) Result2.append(normal(channel2)) deblur_inv=cv2.merge([Result[2],Result[1],Result[0]]) deblur_wie=cv2.merge([Result2[2],Result2[1],Result2[0]])
|