File size: 3,550 Bytes
065f4e5
 
33f3621
065f4e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33f3621
065f4e5
 
 
 
 
 
 
 
 
 
 
 
33f3621
065f4e5
 
 
 
33f3621
065f4e5
 
 
 
 
 
 
 
33f3621
065f4e5
 
33f3621
 
 
 
 
 
 
 
 
 
 
065f4e5
 
33f3621
 
 
 
 
 
 
 
065f4e5
33f3621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065f4e5
33f3621
065f4e5
33f3621
065f4e5
33f3621
065f4e5
33f3621
 
 
 
 
 
 
 
 
065f4e5
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fp
import cv2
from PIL import Image


# ideal filter
def ideal_filter(rows, cols, D0, filtr):
    H = np.zeros(shape = (rows, cols))
    for i in range(rows):
        for j in range(cols):
            # euclidean distance from u,v to origin of frequency

            Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2)) 
            if Duv < D0:
                H[i,j] = 1.0
    if filtr == "High Pass":
        H = 1-H
    #H = H*255
    cv2.imwrite('filter.jpg',np.abs(H*255))
    return H

def butterworth_filter(rows, cols, n_order, D0, filtr):
    H = np.zeros(shape = (rows, cols))
    for i in range(rows):
        for j in range(cols):
        
            Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2))
            H[i,j] = 1/(1+((Duv/D0)**(2*n_order)))
    if filtr == "High Pass":
        H = 1-H
    
    cv2.imwrite('filter.jpg',np.abs(H*255))
    
    return H

def gaussian_filter(rows, cols, filtr):

    H = np.zeros(shape = (rows, cols))
    for i in range(rows):
        for j in range(cols):
            Duv = np.sqrt(np.power(i - rows/2, 2) + np.power(j - cols/2, 2))
            H[i,j] = np.exp(-((Duv**2)/(2*(D0**2))))
    if filtr == "High Pass":
        H = 1-H
    #H = H*255
    cv2.imwrite('filter.jpg',np.abs(H*255))
    return H

def calculate_distance(rows, cols):
    
    dist =np.zeros((rows,cols))
    u=np.arange(0, rows, 1)
    v=np.arange(0, cols, 1)
    
    for i in range(rows):
        for j in range(cols):
            dist[i,j]=np.sqrt(((u[i]-rows/2)**2)+((v[j]-cols/2)**2))
    dist = np.float32(dist)
    return dist


if __name__ == "__main__":
    #image = Image.open(file).convert("L")
    st.set_option('deprecation.showPyplotGlobalUse', False)
    uploaded_file = st.sidebar.file_uploader("Upload image", type = ["jpeg", "jpg", "png"])
    filtr = st.sidebar.radio("Filters", ("Low Pass", "High Pass"))
    kernel = st.sidebar.radio("Kernels", ("Ideal", "Butterworth", "Gaussian"))
    D0 = st.sidebar.slider("Cutoff Frequency", min_value = 0, max_value = 120)
    n_order = st.sidebar.number_input(label = "Order", min_value = 0, max_value = 5)

    if uploaded_file is not None:
        img = Image.open(uploaded_file)
        img.save("read_image.jpg")
        st.subheader("Source Image")
        st.image("read_image.jpg", width = 300)
        img = cv2.imread("read_image.jpg", 0)
        rows, cols = img.shape
        if kernel == "Ideal":
            k = ideal_filter(rows, cols, D0, filtr)
        elif kernel == "Gaussian":
            k = gaussian_filter(rows, cols, filtr)
        elif kernel == "Butterworth":
            k = butterworth_filter(rows, cols, n_order, D0, filtr)
        
        H = fp.fft2(fp.ifftshift(k)) # fast fourier transform
        f_img = fp.fft2(img) # fast fourier transform
        conv_img = np.multiply(H, f_img)

        inv_img = fp.ifft2(conv_img).real

        output_img = ((inv_img - np.min(inv_img))/np.max(inv_img))*255

        #output_img = fp.ifft2(conv_img)

        cv2.imwrite('output_image.jpg',output_img)
        st.subheader(f"Target Image with {filtr} {kernel} Filter, and Filter itself")
        st.image(["filter.jpg", "output_image.jpg"], width = 320)
        dist = calculate_distance(rows, cols)
        st.subheader("Graph of Distance against Function")
        plt.plot(dist.ravel(), k.ravel())
        plt.xlabel('Distance from the Center')
        plt.ylabel('Filter Function')
        st.pyplot()