merve HF Staff commited on
Commit
065f4e5
·
1 Parent(s): 1a3858b

Application File

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ import scipy.fftpack as fp
5
+ import cv2
6
+ from PIL import Image
7
+
8
+
9
+ # ideal filter
10
+ def ideal_filter(rows, cols, D0, filtr):
11
+ H = np.zeros(shape = (rows, cols))
12
+ for i in range(rows):
13
+ for j in range(cols):
14
+ # euclidean distance from u,v to origin of frequency
15
+
16
+ Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2))
17
+ if Duv < D0:
18
+ H[i,j] = 1.0
19
+ if filtr == "High Pass":
20
+ H = 1-H
21
+ #H = H*255
22
+ cv2.imwrite('filter.jpg',np.abs(H*255))
23
+ return H
24
+
25
+ def butterworth_filter(rows, cols, n_order, D0, filtr):
26
+ H = np.zeros(shape = (rows, cols))
27
+ for i in range(rows):
28
+ for j in range(cols):
29
+
30
+ Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2))
31
+ H[i,j] = 1/(1+((Duv/D0)**(2*n_order)))
32
+ #import pdb;pdb.set_trace()
33
+ if filtr == "High Pass":
34
+ H = 1-H
35
+
36
+ cv2.imwrite('filter.jpg',np.abs(H*255))
37
+
38
+ return H
39
+
40
+ def gaussian_filter(rows, cols, filtr):
41
+ #import pdb;pdb.set_trace()
42
+ H = np.zeros(shape = (rows, cols))
43
+ for i in range(rows):
44
+ for j in range(cols):
45
+ Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2))
46
+ H[i,j] = np.exp(-((Duv**2)/(2*(D0**2))))
47
+ if filtr == "High Pass":
48
+ H = 1-H
49
+ #H = H*255
50
+ cv2.imwrite('filter.jpg',np.abs(H*255))
51
+ return H
52
+
53
+
54
+
55
+
56
+ uploaded_file = st.sidebar.file_uploader("Upload image", type = ["jpeg", "jpg", "png"])
57
+ filtr = st.sidebar.radio("Filters", ("Low Pass", "High Pass"))
58
+ kernel = st.sidebar.radio("Kernels", ("Ideal", "Butterworth", "Gaussian"))
59
+ D0 = st.sidebar.slider("Cutoff Frequency", min_value = 0, max_value = 75)
60
+ n_order = st.sidebar.number_input(label = "Order", min_value = 0, max_value = 5)
61
+
62
+ if uploaded_file is not None:
63
+ img = Image.open(uploaded_file)
64
+ img.save("read_image.jpg")
65
+ st.write("Source Image")
66
+ st.image("read_image.jpg", width = 300)
67
+ img = cv2.imread("read_image.jpg", 0)
68
+ rows, cols = img.shape
69
+ if kernel == "Ideal":
70
+ H = ideal_filter(rows, cols, D0, filtr)
71
+ elif kernel == "Gaussian":
72
+ H = gaussian_filter(rows, cols, filtr)
73
+ elif kernel == "Butterworth":
74
+ H = butterworth_filter(rows, cols, n_order, D0, filtr)
75
+
76
+ H = fp.fft2(fp.ifftshift(H)) # fast fourier transform
77
+ f_img = fp.fft2(img) # fast fourier transform
78
+ conv_img = np.multiply(H, f_img)
79
+
80
+ inv_img = fp.ifft2(conv_img).real
81
+
82
+ output_img = ((inv_img - np.min(inv_img))/np.max(inv_img))*255
83
+
84
+ #plt.imshow(output_img, cmap='gray')
85
+ st.write("Kernel")
86
+ st.image("filter.jpg", width = 300)
87
+ cv2.imwrite('output_image.jpg',output_img)
88
+ st.write(f"Target Image with {filtr} {kernel} Filter")
89
+ st.image("output_image.jpg", width = 300)
90
+