From 559d717766b1beaf22572ae5c0e2cab2f25b43fb Mon Sep 17 00:00:00 2001 From: Tyrion <1216776075@qq.com> Date: Fri, 22 Jul 2022 14:31:52 +0800 Subject: [PATCH] the final --- djangoProject/views.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/djangoProject/views.py b/djangoProject/views.py index 45fd66e..b61e54a 100644 --- a/djangoProject/views.py +++ b/djangoProject/views.py @@ -827,8 +827,7 @@ def IdealLowPassFiltering(request): D = np.sqrt((i - x0) ** 2 + (j - y0) ** 2) if D <= D0: h1[i][j] = 1 - result = np.multiply(f_shift, h1) - cv2.imwrite(img_path, result) + cv2.imwrite(img_path, frequency_filter(f_shift, h1)) try: data = {'state': 'static/media/' + name} except: @@ -849,6 +848,7 @@ def butterworth_low_filter(request): for c in image.chunks(): pic.write(c) img = cv2.imread(img_path) + img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) D0 = 20 rank = 2 h, w = img.shape[:2] @@ -859,7 +859,7 @@ def butterworth_low_filter(request): for j in range(w): d = np.sqrt((i - u) ** 2 + (j - v) ** 2) filter_img[i, j] = 1 / (1 + 0.414 * (d / D0) ** (2 * rank)) - cv2.imwrite(img_path, filter_img) + cv2.imwrite(img_path, frequency_filter(img, filter_img)) try: data = {'state': 'static/media/' + name} except: @@ -898,8 +898,7 @@ def IdealHighPassFiltering(request): D = np.sqrt((i - x0) ** 2 + (j - y0) ** 2) if D >= D0: h1[i][j] = 1 - result = np.multiply(f_shift, h1) - cv2.imwrite(img_path, result) + cv2.imwrite(img_path, frequency_filter(f_shift, h1)) try: data = {'state': 'static/media/' + name} except: @@ -920,6 +919,7 @@ def butterworth_high_filter(request): for c in image.chunks(): pic.write(c) img = cv2.imread(img_path) + img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) D0 = 40 rank = 2 h, w = img.shape[:2] @@ -930,9 +930,25 @@ def butterworth_high_filter(request): for j in range(w): d = np.sqrt((i - u) ** 2 + (j - v) ** 2) filter_img[i, j] = 1 / (1 + (D0 / d) ** (2 * rank)) - cv2.imwrite(img_path, filter_img) + cv2.imwrite(img_path, frequency_filter(img, filter_img)) try: data = {'state': 'static/media/' + name} except: data = {'state': 0} return JsonResponse(data) + + +def frequency_filter(image, filter): + """ + :param image: + :param filter: 频域变换函数 + :return: + """ + fftImg = np.fft.fft2(image) #对图像进行傅里叶变换 + fftImgShift = np.fft.fftshift(fftImg)#傅里叶变换后坐标移动到图像中心 + handle_fftImgShift1 = fftImgShift*filter # 对傅里叶变换后的图像进行频域变换 + + handle_fftImgShift2 = np.fft.ifftshift(handle_fftImgShift1) + handle_fftImgShift3 = np.fft.ifft2(handle_fftImgShift2) + handle_fftImgShift4 = np.real(handle_fftImgShift3)#傅里叶反变换后取频域 + return np.uint8(handle_fftImgShift4) \ No newline at end of file