Bitmap DegradedImage { get; set; }
ComplexImage GaussianFunction { get; set; }
double SNR { get; set; }
public Wiener(Bitmap DegradedImage, ComplexImage GaussianFunction, double SNR)
{
this.DegradedImage = DegradedImage;
this.GaussianFunction = GaussianFunction;
this.SNR = SNR;
}
public Bitmap ApplyFilter()
{
if (!GaussianFunction.FourierTransformed)
GaussianFunction.ForwardFourierTransform();
System.Numerics.Complex[,] Conj = Conjugate();
ComplexImage SqrFFTBlurredImage = new ComplexImage(GaussianFunction.Width, GaussianFunction.Height);
for (int x = 0; x < GaussianFunction.Width; x++)
for (int y = 0; y < GaussianFunction.Height; y++)
SqrFFTBlurredImage.Data[x, y] = System.Numerics.Complex.Multiply(GaussianFunction.Data[x, y], GaussianFunction.Data[x, y]);
for (int x = 0; x < SqrFFTBlurredImage.Width; x++)
for (int y = 0; y < SqrFFTBlurredImage.Height; y++)
SqrFFTBlurredImage.Data[x, y] = SqrFFTBlurredImage.Data[x, y] + (1 / (SNR * SNR));
ComplexImage Dividision = new ComplexImage(GaussianFunction.Width, GaussianFunction.Height);
for (int x = 0; x < Dividision.Width; x++)
for (int y = 0; y < Dividision.Height; y++)
Dividision.Data[x, y] = System.Numerics.Complex.Divide(Conj[x, y], GaussianFunction.Data[x, y]);
ComplexImage FFTOfDegradedImage = FFTDegradedImage();
ComplexImage Result = new ComplexImage(Dividision.Width, Dividision.Height);
for (int x = 0; x < Result.Width; x++)
for (int y = 0; y < Result.Height; y++)
Result.Data[x, y] = System.Numerics.Complex.Multiply(Dividision.Data[x, y], FFTOfDegradedImage.Data[x, y]);
Result.BackwardFourierTransform();
return Result.ToBitmap();
}
private ComplexImage FFTDegradedImage()
{
// create grayscale filter (BT709)
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
// apply the filter
Bitmap grayImage = filter.Apply(DegradedImage);
// create complex image
ComplexImage complexImage = ComplexImage.FromBitmap(grayImage);
// do forward Fourier transformation
complexImage.ForwardFourierTransform();
return complexImage;
}
private System.Numerics.Complex[,] Conjugate()
{
System.Numerics.Complex[,] Conj = new System.Numerics.Complex[GaussianFunction.Width, GaussianFunction.Height];
for (int x = 0; x < GaussianFunction.Width; x++)
for (int y = 0; y < GaussianFunction.Height; y++)
{
Conj[x, y] = System.Numerics.Complex.Conjugate(GaussianFunction.Data[x, y]);
}
return Conj;
}
ComplexImage GaussianFunction { get; set; }
double SNR { get; set; }
public Wiener(Bitmap DegradedImage, ComplexImage GaussianFunction, double SNR)
{
this.DegradedImage = DegradedImage;
this.GaussianFunction = GaussianFunction;
this.SNR = SNR;
}
public Bitmap ApplyFilter()
{
if (!GaussianFunction.FourierTransformed)
GaussianFunction.ForwardFourierTransform();
System.Numerics.Complex[,] Conj = Conjugate();
ComplexImage SqrFFTBlurredImage = new ComplexImage(GaussianFunction.Width, GaussianFunction.Height);
for (int x = 0; x < GaussianFunction.Width; x++)
for (int y = 0; y < GaussianFunction.Height; y++)
SqrFFTBlurredImage.Data[x, y] = System.Numerics.Complex.Multiply(GaussianFunction.Data[x, y], GaussianFunction.Data[x, y]);
for (int x = 0; x < SqrFFTBlurredImage.Width; x++)
for (int y = 0; y < SqrFFTBlurredImage.Height; y++)
SqrFFTBlurredImage.Data[x, y] = SqrFFTBlurredImage.Data[x, y] + (1 / (SNR * SNR));
ComplexImage Dividision = new ComplexImage(GaussianFunction.Width, GaussianFunction.Height);
for (int x = 0; x < Dividision.Width; x++)
for (int y = 0; y < Dividision.Height; y++)
Dividision.Data[x, y] = System.Numerics.Complex.Divide(Conj[x, y], GaussianFunction.Data[x, y]);
ComplexImage FFTOfDegradedImage = FFTDegradedImage();
ComplexImage Result = new ComplexImage(Dividision.Width, Dividision.Height);
for (int x = 0; x < Result.Width; x++)
for (int y = 0; y < Result.Height; y++)
Result.Data[x, y] = System.Numerics.Complex.Multiply(Dividision.Data[x, y], FFTOfDegradedImage.Data[x, y]);
Result.BackwardFourierTransform();
return Result.ToBitmap();
}
private ComplexImage FFTDegradedImage()
{
// create grayscale filter (BT709)
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
// apply the filter
Bitmap grayImage = filter.Apply(DegradedImage);
// create complex image
ComplexImage complexImage = ComplexImage.FromBitmap(grayImage);
// do forward Fourier transformation
complexImage.ForwardFourierTransform();
return complexImage;
}
private System.Numerics.Complex[,] Conjugate()
{
System.Numerics.Complex[,] Conj = new System.Numerics.Complex[GaussianFunction.Width, GaussianFunction.Height];
for (int x = 0; x < GaussianFunction.Width; x++)
for (int y = 0; y < GaussianFunction.Height; y++)
{
Conj[x, y] = System.Numerics.Complex.Conjugate(GaussianFunction.Data[x, y]);
}
return Conj;
}
No comments:
Post a Comment