#! /bin/sh
#
# SparseLab Pandore toolbox
#
# GREYC IMAGE
# 6 Boulevard Marechal Juin
# F-14050 Caen Cedex
#
# Please to email any bugs, comments, and/or additions to this file see:
# http://www.greyc.ensicaen.fr/EquipeImage/Pandore
#
#(C) Jalal Fadili - 2007-05-31
#
# Denoising example.

if [ $# -ne 1 ]
then
echo >&2 "USAGE : $0 noise_std"
exit 1
fi

echo ---------------
echo "Seismic image"
echo ---------------
imin=../../data/seismic.pan
std=$1
realmax=1.7977e+308

echo Add AWGN noise of sigma=$std
paddnoise 1 0 $std $imin imnoisy.pan
pvisu imnoisy.pan

echo "Compute threshold (3*sigma/sqrt(2))"
echo "sqrt(2) because the wave atoms are not normalized to a unit l_2 norm."
pnewimage 1 1 0 $std thd.pan
pmult thd.pan thd.pan thd.pan
pmultcst  9. thd.pan thd.pan
pdivcst   2. thd.pan thd.pan
psqrt thd.pan thd.pan
pmaximumvalue thd.pan
thd=`pstatus`
echo thd=$thd

# Forward transform.
echo "Forward transform"
../../bin/fatomfm2sym p $imin c.pan

# Hard thresholding.
echo "Hard thresholding"
pabs c.pan cabs.pan
pbinarization $thd $realmax cabs.pan mask.pan
pmask c.pan mask.pan cmasked.pan

# Inverse transform.
echo 'Inverse transform'
../../bin/iatomfm2sym p cmasked.pan rec.pan
pvisu rec.pan

# Display PSNR.
pmaximumvalue $imin
max=`pstatus`
ppsnr $max $imin imnoisy.pan
psnrin=`pstatus`
ppsnr $max $imin rec.pan
psnr=`pstatus`
echo PSNRin=$psnrin PSNRden=$psnr dB


echo ---------------
echo "Fingerprint image"
echo ---------------
imin=../../data/fingerprint.pan
std=$1
realmax=1.7977e+308

echo Add AWGN noise of sigma=$std
paddnoise 1 0 $std $imin imnoisy.pan
pvisu imnoisy.pan

echo "Compute threshold (3*sigma/sqrt(2))"
echo "sqrt(2) because the wave atoms are not normalized to a unit l_2 norm."
pnewimage 1 1 0 $std thd.pan
pmult thd.pan thd.pan thd.pan
pmultcst  9. thd.pan thd.pan
pdivcst   2. thd.pan thd.pan
psqrt thd.pan thd.pan
pmaximumvalue thd.pan
thd=`pstatus`
echo thd=$thd

# Forward transform.
echo "Forward transform"
../../bin/fatomfm2sym p $imin c.pan

# Hard thresholding.
echo "Hard thresholding"
pabs c.pan cabs.pan
pbinarization $thd $realmax cabs.pan mask.pan
pmask c.pan mask.pan cmasked.pan

# Inverse transform.
echo 'Inverse transform'
../../bin/iatomfm2sym p cmasked.pan rec.pan
pvisu rec.pan

# Display PSNR.
pmaximumvalue $imin
max=`pstatus`
ppsnr $max $imin imnoisy.pan
psnrin=`pstatus`
ppsnr $max $imin rec.pan
psnr=`pstatus`
echo PSNRin=$psnrin PSNRden=$psnr dB

rm {imnoisy,c,thd,cabs,cmasked,mask,rec}.pan


