Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import scipy.fftpack as fp
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
|
@@ -18,7 +19,7 @@ def ideal_filter(rows, cols, D0, filtr):
|
|
| 18 |
if filtr == "High Pass":
|
| 19 |
H = 1-H
|
| 20 |
#H = H*255
|
| 21 |
-
cv2.imwrite('
|
| 22 |
return H
|
| 23 |
|
| 24 |
def butterworth_filter(rows, cols, n_order, D0, filtr):
|
|
@@ -31,12 +32,12 @@ def butterworth_filter(rows, cols, n_order, D0, filtr):
|
|
| 31 |
if filtr == "High Pass":
|
| 32 |
H = 1-H
|
| 33 |
|
| 34 |
-
cv2.imwrite('
|
| 35 |
|
| 36 |
return H
|
| 37 |
|
| 38 |
def gaussian_filter(rows, cols, filtr):
|
| 39 |
-
|
| 40 |
H = np.zeros(shape = (rows, cols))
|
| 41 |
for i in range(rows):
|
| 42 |
for j in range(cols):
|
|
@@ -45,44 +46,62 @@ def gaussian_filter(rows, cols, filtr):
|
|
| 45 |
if filtr == "High Pass":
|
| 46 |
H = 1-H
|
| 47 |
#H = H*255
|
| 48 |
-
cv2.imwrite('
|
| 49 |
return H
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
uploaded_file
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
img = Image.open(uploaded_file)
|
| 62 |
-
img.save("./read_image.jpg")
|
| 63 |
-
st.write("Source Image")
|
| 64 |
-
st.image("./read_image.jpg", width = 300)
|
| 65 |
-
img = cv2.imread("./read_image.jpg", 0)
|
| 66 |
-
rows, cols = img.shape
|
| 67 |
-
if kernel == "Ideal":
|
| 68 |
-
H = ideal_filter(rows, cols, D0, filtr)
|
| 69 |
-
elif kernel == "Gaussian":
|
| 70 |
-
H = gaussian_filter(rows, cols, filtr)
|
| 71 |
-
elif kernel == "Butterworth":
|
| 72 |
-
H = butterworth_filter(rows, cols, n_order, D0, filtr)
|
| 73 |
-
|
| 74 |
-
H = fp.fft2(fp.ifftshift(H)) # fast fourier transform
|
| 75 |
-
f_img = fp.fft2(img) # fast fourier transform
|
| 76 |
-
conv_img = np.multiply(H, f_img)
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 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
|
|
|
|
| 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):
|
|
|
|
| 32 |
if filtr == "High Pass":
|
| 33 |
H = 1-H
|
| 34 |
|
| 35 |
+
cv2.imwrite('filter.jpg',np.abs(H*255))
|
| 36 |
|
| 37 |
return H
|
| 38 |
|
| 39 |
def gaussian_filter(rows, cols, filtr):
|
| 40 |
+
|
| 41 |
H = np.zeros(shape = (rows, cols))
|
| 42 |
for i in range(rows):
|
| 43 |
for j in range(cols):
|
|
|
|
| 46 |
if filtr == "High Pass":
|
| 47 |
H = 1-H
|
| 48 |
#H = H*255
|
| 49 |
+
cv2.imwrite('filter.jpg',np.abs(H*255))
|
| 50 |
return H
|
| 51 |
|
| 52 |
+
def calculate_distance(rows, cols):
|
| 53 |
+
|
| 54 |
+
dist =np.zeros((rows,cols))
|
| 55 |
+
u=np.arange(0, rows, 1)
|
| 56 |
+
v=np.arange(0, cols, 1)
|
| 57 |
+
|
| 58 |
+
for i in range(rows):
|
| 59 |
+
for j in range(cols):
|
| 60 |
+
dist[i,j]=np.sqrt(((u[i]-rows/2)**2)+((v[j]-cols/2)**2))
|
| 61 |
+
dist = np.float32(dist)
|
| 62 |
+
return dist
|
| 63 |
|
| 64 |
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
#image = Image.open(file).convert("L")
|
| 67 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
| 68 |
+
uploaded_file = st.sidebar.file_uploader("Upload image", type = ["jpeg", "jpg", "png"])
|
| 69 |
+
filtr = st.sidebar.radio("Filters", ("Low Pass", "High Pass"))
|
| 70 |
+
kernel = st.sidebar.radio("Kernels", ("Ideal", "Butterworth", "Gaussian"))
|
| 71 |
+
D0 = st.sidebar.slider("Cutoff Frequency", min_value = 0, max_value = 120)
|
| 72 |
+
n_order = st.sidebar.number_input(label = "Order", min_value = 0, max_value = 5)
|
| 73 |
|
| 74 |
+
if uploaded_file is not None:
|
| 75 |
+
img = Image.open(uploaded_file)
|
| 76 |
+
img.save("read_image.jpg")
|
| 77 |
+
st.subheader("Source Image")
|
| 78 |
+
st.image("read_image.jpg", width = 300)
|
| 79 |
+
img = cv2.imread("read_image.jpg", 0)
|
| 80 |
+
rows, cols = img.shape
|
| 81 |
+
if kernel == "Ideal":
|
| 82 |
+
k = ideal_filter(rows, cols, D0, filtr)
|
| 83 |
+
elif kernel == "Gaussian":
|
| 84 |
+
k = gaussian_filter(rows, cols, filtr)
|
| 85 |
+
elif kernel == "Butterworth":
|
| 86 |
+
k = butterworth_filter(rows, cols, n_order, D0, filtr)
|
| 87 |
+
|
| 88 |
+
H = fp.fft2(fp.ifftshift(k)) # fast fourier transform
|
| 89 |
+
f_img = fp.fft2(img) # fast fourier transform
|
| 90 |
+
conv_img = np.multiply(H, f_img)
|
| 91 |
|
| 92 |
+
inv_img = fp.ifft2(conv_img).real
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
output_img = ((inv_img - np.min(inv_img))/np.max(inv_img))*255
|
| 95 |
|
| 96 |
+
#output_img = fp.ifft2(conv_img)
|
| 97 |
|
| 98 |
+
cv2.imwrite('output_image.jpg',output_img)
|
| 99 |
+
st.subheader(f"Target Image with {filtr} {kernel} Filter, and Filter itself")
|
| 100 |
+
st.image(["filter.jpg", "output_image.jpg"], width = 320)
|
| 101 |
+
dist = calculate_distance(rows, cols)
|
| 102 |
+
st.subheader("Graph of Distance against Function")
|
| 103 |
+
plt.plot(dist.ravel(), k.ravel())
|
| 104 |
+
plt.xlabel('Distance from the Center')
|
| 105 |
+
plt.ylabel('Filter Function')
|
| 106 |
+
st.pyplot()
|
| 107 |
|