<jupyter_start><jupyter_text>Task 2: Frequency Domain Analysis - FilteringIn this task, we will implement high-pass, low-pass, and band-reject filters using frequency domain analysis.<jupyter_code>%%bash
pip install git+https://github.com/ee604/ee604_plugins
# Importing required libraries

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.draw import circle as circ

from ee604_plugins import download_dataset, cv2_imshow

download_dataset(assignment_no=3, task_no=2) # download data for this assignment
def getFrequencyDomainSignal(img):
    '''
    Write a program to extract magnitude and phase spectrum of the input image
    
    Inputs:
    + img - grayscaled image of size N x N
          - values between [0, 255] - 'uint8'
    
    Outputs:
    + mag_spectrum - magnitude spectrum of size N x N
    + phase_spectrum - phase spectrum of size N x N
    
    Allowed external package:
    + You are allowed to use any cv2.dft or cv2.idft module or any other suitable module in numpy and cv2 
    + Just make sure you do not use any direct implementation.
    
    Hint:
    + not needed
    
    '''
    
    #############################
    # Start your code from here #
    #############################
    
    # Replace with your code...
    
    #############################
    # End your code here ########
    ############################# 
    
    return mag_spectrum, phase_spectrum
def reconstruction(mag_spectrum, phase_spectrum):
    '''
    Write a program to reconstruct the original image using its magnitude and phase spectrum
    
    Inputs:
    + mag_spectrum - magnitude spectrum of size N x N
    + phase_spectrum - phase spectrum of size N x N
    
    Outputs:
    + out_img - reconstructed image of size N x N
              - values between [0, 255] - 'uint8'
    
    Allowed external package:
    + You are allowed to use any cv2.dft or cv2.idft module or any other suitable module in numpy and cv2 
    + Just make sure you do not use any direct implementation.
    
    Hint:
    + not needed
    
    '''
    
    #############################
    # Start your code from here #
    #############################
    
    # Replace with your code...
    
    #############################
    # End your code here ########
    ############################# 
    
    return out_img
def applyLPF(img, fc):
    '''
    Write a program to apply an ideal low pass filter (having cutoff frequency 'fc') to the input image.
    You must use frquency domain analysis to do this.
    
    Inputs:
    + img - grayscaled image of size N x N
          - values between [0, 255] - 'uint8'
    + fc - cutoff frequency 
        - 0 < fc < sqrt(img.shape[0]*img.shape[1])/2
    
    Outputs:
    + out_img - filetered image of size N x N
              - values between [0, 255] - 'uint8'
    
    Allowed external package:
    + You are allowed to use any cv2.dft or cv2.idft module or any other suitable module in numpy and cv2 
    + Just make sure you do not use any direct implementation.
    
    Hint:
    + use getFrequencyDomainSignal() and reconstruction()
    
    '''
    
    #############################
    # Start your code from here #
    #############################
    
    # Replace with your code...
    
    #############################
    # End your code here ########
    #############################    
    
    return out_img
def applyHPF(img, fc):
    '''
    Write a program to apply an ideal high pass filter (having cutoff frequency 'fc') to the input image.
    You must use frquency domain analysis to do this.
    
    Inputs:
    + img - grayscaled image of size N x N
          - values between [0, 255] - 'uint8'
    + fc - cutoff frequency 
        - 0 < fc < sqrt(img.shape[0]*img.shape[1])/2
    
    Outputs:
    + out_img - filetered image of size N x N
              - values between [0, 255] - 'uint8'
    
    Allowed external package:
    + You are allowed to use any cv2.dft or cv2.idft module or any other suitable module in numpy and cv2 
    + Just make sure you do not use any direct implementation.
    
    Hint:
    + use getFrequencyDomainSignal() and reconstruction()
    
    '''
    
    #############################
    # Start your code from here #
    #############################
    
    # Replace with your code...
    
    #############################
    # End your code here ########
    #############################    
    
    return out_img
def applyBandRejectFilter(img, fL, fH):
    '''
    Write a program to apply an ideal band reject filter (having cutoff frequency at 'fL' and 'fH')
    to the input image. You must use frquency domain analysis to do this.
    
    Inputs:
    + img - grayscaled image of size N x N
          - values between [0, 255] - 'uint8'
    + fL & fH - cutoff frequency 
              - 0 < fL < fH < sqrt(img.shape[0]*img.shape[1])/2
    
    Outputs:
    + out_img - filetered image of size N x N
              - values between [0, 255] - 'uint8'
    
    Allowed external package:
    + You are allowed to use any cv2.dft or cv2.idft module or any other suitable module in numpy and cv2 
    + Just make sure you do not use any direct implementation.
    
    Hint:
    + use getFrequencyDomainSignal() and reconstruction()
    
    '''
    
    #############################
    # Start your code from here #
    #############################
    
    # Replace with your code...
    
    #############################
    # End your code here ########
    #############################    
    
    return out_img
# Do not change codes inside this cell

lena_noisy = cv2.imread('data/lena_noisy.jpg', 0)

def plot_frame(gridx, gridy, subplot_id, img, name, axis=False):
    plt.subplot(gridx, gridy, 1 + int(subplot_id))
    plt.imshow(np.uint8(img), cmap="gray", vmin=0, vmax=255)
    
    if axis == False:
        plt.axis("off")
    plt.title(name)
# Test getFrequencyDomainSignal() and reconstruction()
# Make sure the reconstructed image looks similar to the original image

mag_spectrum, phase_spectrum = getFrequencyDomainSignal(lena_noisy)
lena_reconstructed = reconstruction(mag_spectrum, phase_spectrum)

plt.figure(figsize=(12, 6))
plot_frame(1, 2, 0, lena_noisy, "Noisy Lena")
plot_frame(1, 2, 1, lena_reconstructed, "Noisy Lena Reconstructed")
# Do not change codes inside this cell

mag_spectrum, phase_spectrum = getFrequencyDomainSignal(lena_noisy)

plt.figure(figsize=(18, 6))
plot_frame(1, 3, 0, lena_noisy, "Noisy Lena")

tmp = np.log(mag_spectrum)
tmp -= np.min(tmp)
if np.max(tmp) != 0:
    tmp /= np.max(tmp)
tmp *= 255
plot_frame(1, 3, 1, tmp, "Magnitude Spectrum", axis=True)

tmp = phase_spectrum
tmp -= np.min(tmp)
if np.max(tmp) != 0:
    tmp /= np.max(tmp)
tmp *= 255
plot_frame(1, 3, 2, phase_spectrum, "Phase Spectrum", axis=True)<jupyter_output><empty_output><jupyter_text>Find suitable fL and fH for band reject filter<jupyter_code># Have a look at the phase and magnitude spectrum of the image and find a suitable fL and fH for
# a Band reject filter to work

def plot_BRF_output(fL, fH):
    if fL == ".....":
        fL = 1
        fH = int(lena_noisy.shape[0]/2)
        
    lena_filtered = applyBandRejectFilter(lena_noisy, fL, fH)
    plt.figure(figsize=(12, 6))
    plot_frame(1, 2, 0, lena_noisy, "Noisy")
    plot_frame(1, 2, 1, lena_filtered, "Filtered")


#############################
# Your part #################
#############################

# Replace with your value for fL and fH
fL = "....."
fH = "....."

plot_BRF_output(fL, fH)<jupyter_output><empty_output><jupyter_text>Observations<jupyter_code># Do not change codes inside this cell
# Add your observations in next cell

plt.figure(figsize=(18, 12))
plot_frame(2, 3, 0, lena_noisy, "Noisy")

tmp = applyBandRejectFilter(lena_noisy, fL, fH)
plot_frame(2, 3, 1, tmp, "BRF")

tmp = applyLPF(lena_noisy, fL)
plot_frame(2, 3, 2, tmp, "LPF (fc = fL)")

tmp = applyLPF(lena_noisy, fH)
plot_frame(2, 3, 3, tmp, "LPF (fc = fH)")

tmp = applyHPF(lena_noisy, fL)
plot_frame(2, 3, 4, tmp, "HPF (fc = fL)")

tmp = applyHPF(lena_noisy, fH)
plot_frame(2, 3, 5, tmp, "HPF (fc = fH)")
# Add your observations; make sure you compare all the five filtering operations done in the previous cell

your_observation = """
Replace this with your observations.
"""

print(your_observation)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1659299964161988, 'fraction_numerical': 0.0181579261736949, 'mean_word_length': 2.9658929417337756, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '448081', 'license': 'MIT', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2584, 'n_words': 889, 'path': 'assignment_3/Task_2.ipynb', 'repo_name': 'antranx/ee604_assignments', 'size': '8371'}