# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .jl
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.3.3
#   kernelspec:
#     display_name: Julia 1.11.7
#     language: julia
#     name: julia-1.11
# ---

# Illustrate separable 2D low-pass filtering of an image
# using the convolution matrix generated in HW1.
# - 2017-09-11, Jeff Fessler, University of Michigan
# - 2021-08-20 Julia 1.6.2
# - 2023-07-24 Julia 1.9.2
# - 2025-09-22 Julia 1.11.7

using MIRTjim: jim # image display
using ImagePhantoms: shepp_logan, SheppLoganEmis # test image
using Random: seed!

# replace with the path to your own function, or paste your function code here
mydir = ENV["hw551test"] # change path
include(joinpath(mydir, "convolution.jl"))

# make a 2D image binary test image of a simple disk and display it
Xtrue = shepp_logan(128, SheppLoganEmis())[15:114,:]
@show (M,N) = size(Xtrue)
seed!(0)
X = Xtrue + 0.5 * randn(size(Xtrue)) # add some noise
jim(X, xlabel="x", ylabel="y", "original noisy image") # jiffy image display

# 1D moving-average filter represented as a matrix
hx = ones(9)/9 # 9-pt moving average filter for x (along cols)
hy = ones(5)/5 # 5-pt moving average filter for y (along rows)
Hx,_ = convolution(hx, zeros(M))
Hy,_ = convolution(hy, zeros(N));

# display one of the 1D convolution matrices
# transpose to show it like a matrix H[i,j], rather than as an image f[x,y]
jim(Hx', ylabel="i", xlabel="j")

Y = Hx * X * Hy'; # apply 1D filters via convolution to both cols and rows

# If the following line fails for you,
# or if the result is unrelated to your name on Canvas,
# then edit it to be `user =  "(your name)"`
user = Sys.username()

# Display filtered image - the noise is greatly reduced!
# It is "smeared out" (filtered or smoothed) more along x than y.
# Do you know why?
jim(Y, xlabel="x", ylabel="y", "filtered image by '$user'")

# +
#using Plots: savefig
#savefig("hs003jf-fig.pdf")
