Exemple #1
0
        private void button9_Click(object sender, EventArgs e)
        {
            var tmp = TestImageGenerator.GetTestPair(size0, size1, 5, 15);

            image     = tmp.Item1.images[0];
            image_res = tmp.Item2;
            fft_image = Complex.CreateComplexArray(image);
            FourierTransform.FFT2(fft_image, FourierTransform.Direction.Forward);
            CreateHilbertFilter();
            Complex.ApplyFilter(fft_image, filter_image);
            FourierTransform.FFT2(fft_image, FourierTransform.Direction.Backward);
            image = Tools.CalculatePhaseImageByHilbert(fft_image);
            unwrapper.UpdateParamsIfNeed(image);
            unwrapper.UnwrapParallel(image, out var scr);
            ImageSource.subtract_min(image);
            ImageSource.subtract_min(image_res);
            var d = ImageSource.diff(image, image_res);

            label1.Text = Math.Round(ImageSource.max(d), 5).ToString();;
            label2.Text = Math.Round(ImageSource.min(d), 5).ToString();;
            label3.Text = Math.Round(ImageSource.std(image, image_res), 5).ToString();;
            label1.Update();
            label2.Update();
            label3.Update();
            plot(d);
        }
        internal unsafe static Image <Complex, TDepth> FFT <TDepth>(this Image <Complex, TDepth> image, FourierTransform.Direction direction, bool inPlace = false)
            where TDepth : struct
        {
            Image <Complex, TDepth> dest = null;

            if (inPlace)
            {
                dest = image;
            }
            else
            {
                dest = image.Clone();
            }

            if (typeof(TDepth).Equals(typeof(float)))
            {
                FourierTransform.FFT2((ComplexF *)dest.ImageData, dest.Width, dest.Height, dest.Stride, direction);
            }
            else if (typeof(TDepth).Equals(typeof(double)))
            {
                throw new NotImplementedException(); //TODO: implement for double
                //FourierTransform.FFT2((Complex*)dest.ImageData, dest.Width, dest.Height, dest.Stride, direction);
            }
            else
            {
                throw new NotSupportedException();
            }

            return(dest);
        }
Exemple #3
0
        // Moution Blur....................................
        public static ByteBitmap WienerFilterFromMoution(ByteBitmap aBitmap, float Length, int Angle, float Nsr)
        {
            //make fft from abitmap
            Point      lStart;
            ByteBitmap lLoc = FFTHelpers.FitByteBitmapForFFT2D(aBitmap, 10, out lStart, 255);

            Complex[,] lFx = FFTHelpers.ByteBitmapToComplex(lLoc);
            FourierTransform.FFT2(lFx, FourierTransform.Direction.Forward);

            //make moution FFT
            Complex[,] lHx = GetMotionComplexImage(new Size(lLoc.Width, lLoc.Height), Angle, Length);
            FourierTransform.FFT2(lHx, FourierTransform.Direction.Forward);

            //make wiener
            Complex[,] lWR = WienerFilter(lFx, lHx, Nsr);

            //backward result
            FourierTransform.FFT2(lWR, FourierTransform.Direction.Backward);

            //convert to bytebitmap
            ByteBitmap lRes = FFTHelpers.ComplexToByteBitmap(lWR);

            lRes = FFTHelpers.GetFittedByteBitmapFromFFT2D(lRes, lStart, new Size(aBitmap.Width, aBitmap.Height));
            return(lRes);
        }
Exemple #4
0
        public static WriteableBitmap MakeFFT(WriteableBitmap image)
        {
            Complex[,] grayImage = ToGrayScale(image);
            FourierTransform.FFT2(grayImage, FourierTransform.Direction.Forward);

            return(ComplexToMagnitude(grayImage, image.PixelHeight));
        }
Exemple #5
0
    public Form1()
    {
        InitializeComponent();
        Bitmap bmp = Bitmap.FromFile("lenagr.png") as Bitmap;

        pictureBox1.Image = bmp;
        Complex[,] cImage = ToComplex(bmp);
        for (int y = 0; y < cImage.GetLength(1); y++)
        {
            for (int x = 0; x < cImage.GetLength(0); x++)
            {
                if (((x + y) & 0x1) != 0)
                {
                    double real      = cImage[y, x].Real * (-1);
                    double imaginary = cImage[y, x].Imaginary * (-1);
                    cImage[y, x] = new Complex(real, imaginary);
                }
            }
        }
        FourierTransform.FFT2(cImage, FourierTransform.Direction.Forward);
        double[,] intImage = ToDouble(cImage);
        intImage           = Limit(intImage);
        Bitmap bmpMagImg = ToBitmap(intImage, PixelFormat.Format24bppRgb);

        pictureBox2.Image = bmpMagImg;
    }
        public void FFT2_test1()
        {
            var tuple = TestImageGenerator.GetTestPair(1024, 1024, 4);

            Complex[,] test_image = Complex.CreateComplexArray(tuple.Item1.images[0]);
            FourierTransform.FFT2(test_image, FourierTransform.Direction.Forward);
            Complex[,] reserv = (Complex[, ])test_image.Clone();
            FourierTransform.FFT2(test_image, FourierTransform.Direction.Backward);
            double[,] inverse_result = Complex.CreateDoubleArray(test_image);
            double std = ImageSource.std(tuple.Item1.images[0], inverse_result);

            Assert.IsTrue(std < ImageSource.mean(tuple.Item1.images[0]) / 1000);
        }
Exemple #7
0
        public override void Calc()
        {
            if (!calc)
            {
                return;
            }
            int size0 = images[0].GetUpperBound(0) + 1;
            int size1 = images[0].GetUpperBound(1) + 1;

            Complex[,] test_image = Complex.CreateComplexArray(images[0]);
            FourierTransform.FFT2(test_image, FourierTransform.Direction.Forward);
            filter = CreateFilterIfNeed(size0, size1);
            Complex.ApplyFilter(test_image, filter);
            FourierTransform.FFT2(test_image, FourierTransform.Direction.Backward);
            images[0] = Tools.CalculatePhaseImageByHilbert(test_image);
        }
        public void FFT2_test2()
        {
            var tuple = TestImageGenerator.GetTestPair(1024, 2048, 4);

            //DateTime dt1 = DateTime.UtcNow;
            Complex[,] test_image = Complex.CreateComplexArray(tuple.Item1.images[0]);
            DateTime dt1 = DateTime.UtcNow;

            FourierTransform.FFT2(test_image, FourierTransform.Direction.Forward);
            Complex[,] reserv = (Complex[, ])test_image.Clone();
            FourierTransform.FFT2(test_image, FourierTransform.Direction.Backward);
            double[,] inverse_result = Complex.CreateDoubleArray(test_image);
            double resss = dt1.Subtract(DateTime.UtcNow).TotalSeconds;

            double std = ImageSource.std(tuple.Item1.images[0], inverse_result);

            Assert.IsTrue(std < ImageSource.mean(tuple.Item1.images[0]) / 1000);
        }
        /// <summary>
        /// Calculates Fast Fourier transform.
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="direction">Forward or backward direction.</param>
        /// <param name="inPlace">Process in place or not.</param>
        /// <returns>Processed image. If <paramref name="inPlace"/> is used the result is the same as input image therefore may be omitted.</returns>
        public unsafe static ComplexF[,] FFT(this ComplexF[,] image, FourierTransform.Direction direction, bool inPlace = false)
        {
            ComplexF[,] dest = null;
            if (inPlace)
            {
                dest = image;
            }
            else
            {
                dest = (ComplexF[, ])image.Clone();
            }

            using (var uDest = dest.Lock())
            {
                FourierTransform.FFT2((ComplexF *)uDest.ImageData, uDest.Width, uDest.Height, uDest.Stride, direction);
            }

            return(dest);
        }
Exemple #10
0
        /// <summary>
        /// Applies forward fast Fourier transformation to the complex image.
        /// </summary>
        ///
        public void ForwardFourierTransform( )
        {
            if (!fourierTransformed)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (((x + y) & 0x1) != 0)
                        {
                            data[y, x] *= -1.0;
                        }
                    }
                }

                FourierTransform.FFT2(data, FourierTransform.Direction.Forward);
                fourierTransformed = true;
            }
        }
Exemple #11
0
        /// <summary>
        /// Applies backward fast Fourier transformation to the complex image.
        /// </summary>
        ///
        public void BackwardFourierTransform( )
        {
            if (fourierTransformed)
            {
                FourierTransform.FFT2(data, FourierTransform.Direction.Backward);
                fourierTransformed = false;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (((x + y) & 0x1) != 0)
                        {
                            data[y, x] *= -1.0;
                        }
                    }
                }
            }
        }
Exemple #12
0
 public void BackwardFourierTransform()
 {
     if (!fourierTransformed)
     {
         return;
     }
     FourierTransform.FFT2(data, FourierTransform.Direction.Backward);
     fourierTransformed = false;
     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width; j++)
         {
             if (((j + i) & 1) != 0)
             {
                 data[i, j].Re *= -1.0;
                 data[i, j].Im *= -1.0;
             }
         }
     }
 }
Exemple #13
0
        /// <summary>
        /// Applies backward fast Fourier transformation to the complex image.
        /// </summary>
        /// 
        public void BackwardFourierTransform( )
        {
            if ( this.fourierTransformed )
            {
                FourierTransform.FFT2(this.data , FourierTransform.Direction.Backward );
                this.fourierTransformed = false;

                for ( var y = 0; y < this.height; y++ )
                {
                    for ( var x = 0; x < this.width; x++ )
                    {
                        if ( ( ( x + y ) & 0x1 ) != 0 )
                        {
                            this.data[y, x].Re *= -1;
                            this.data[y, x].Im *= -1;
                        }
                    }
                }
            }
        }
        // Backward Fourier Transform
        public void BackwardFourierTransform( )
        {
            if (fmode)
            {
                FourierTransform.FFT2(data, FourierDirection.Backward);
                fmode = false;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (((x + y) & 0x1) != 0)
                        {
                            data[y, x].Re *= -1;
                            data[y, x].Im *= -1;
                        }
                    }
                }
            }
        }
        // Farward Fourier Transform
        public void ForwardFourierTransform( )
        {
            if (!fmode)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (((x + y) & 0x1) != 0)
                        {
                            data[y, x].Re *= -1;
                            data[y, x].Im *= -1;
                        }
                    }
                }

                FourierTransform.FFT2(data, FourierDirection.Forward);
                fmode = true;
            }
        }
Exemple #16
0
 public void ForwardFourierTransform()
 {
     if (!this.fmode)
     {
         for (int i = 0; i < this.height; i++)
         {
             for (int j = 0; j < this.width; j++)
             {
                 if (((j + i) & 1) != 0)
                 {
                     Complex complex1 = this.data[i, j];
                     complex1.Re *= -1f;
                     Complex complex2 = this.data[i, j];
                     complex2.Im *= -1f;
                 }
             }
         }
         FourierTransform.FFT2(this.data, FourierDirection.Forward);
         this.fmode = true;
     }
 }
Exemple #17
0
 public void ForwardFourierTransform()
 {
     if (!this.fourierTransformed)
     {
         for (int i = 0; i < this.height; i++)
         {
             for (int j = 0; j < this.width; j++)
             {
                 if (((j + i) & 1) != 0)
                 {
                     Complex complex1 = this.data[i, j];
                     complex1.Re *= -1.0;
                     Complex complex2 = this.data[i, j];
                     complex2.Im *= -1.0;
                 }
             }
         }
         FourierTransform.FFT2(this.data, 1);
         this.fourierTransformed = true;
     }
 }
Exemple #18
0
        private void Form1_Load(object sender, EventArgs e)
        {
            String address = "D:/workshop/holography/WindowsFormsApplication1/WindowsFormsApplication1";

            String file_name = "/Tang25.txt";

            file_manager file = new file_manager(file_name, address);

            file.read_coordinate();
            // file.write_coordinate_file(file.x, file.y, file.z, file.r, file.g, file.b);

            List <double> obj_x = new List <double>();
            List <double> obj_y = new List <double>();
            List <double> obj_z = new List <double>();
            List <double> obj_r = new List <double>();
            List <double> obj_g = new List <double>();
            List <double> obj_b = new List <double>();

            List <double> O = new List <double>();

            for (int i = 0; i < file.x.Count; i++)
            {
                //System.Console.WriteLine(i + " " + file.x[i] + " " + file.y[i] + " " + file.z[i]);

                obj_x.Add(Math.Round((Convert.ToDouble(file.x[i]) + 100) * 5));
                obj_y.Add(Math.Round((Convert.ToDouble(file.y[i]) + 50) * 6));
                obj_z.Add(-Math.Round(Convert.ToDouble(file.z[i]) - 230));

                obj_r.Add(Convert.ToDouble(file.r[i]));
                obj_g.Add(Convert.ToDouble(file.g[i]));
                obj_b.Add(Convert.ToDouble(file.b[i]));
            }


            int    o      = 8;
            int    t      = 1;
            int    s      = 1024;
            double d      = 0.25;
            double lambda = 532e-9;
            double k      = 2 * Math.PI / lambda;

            double Hologram_sampling_interval = 7.4e-6;
            double dx = Hologram_sampling_interval;
            double dy = Hologram_sampling_interval;

            double[,] image = new double[s, s];



            for (int i = 0; i < obj_x.Count; i++)
            {
                obj_x[i] = obj_x[i] * t;
                obj_y[i] = obj_y[i] * t;
                obj_z[i] = obj_z[i] * 1;
            }


            List <double> Cut = obj_z.Distinct().ToList();

            Cut.Sort();
            double Ny = s;
            double Nx = s;
            double fx = 1 / (dx * Nx);
            double fy = 1 / (dy * Ny);

            double[,] x = new double[s, s];
            double[,] y = new double[s, s];



            Complex complex1 = new Complex(1, 1);



            Complex[,] Hologram = new Complex[s, s];


            int counter_y = 0;

            for (int i = 0; i < s; i++)
            {
                int counter_x = 0;
                if (counter_y == s / 2)
                {
                    counter_y *= -1;
                }

                for (int j = 0; j < s; j++)
                {
                    if (counter_x == s / 2)
                    {
                        counter_x *= -1;
                    }

                    x[i, j] = counter_x * fx;
                    y[i, j] = counter_y * fy;

                    counter_x++;
                }

                counter_y++;
            }


            double d1;

            Complex[,] O_image;
            Complex[,] H = new Complex[s, s];
            Complex[,] film;
            double[,] phase_h = new double[s, s];
            double max = -1.0;


            for (int i = 0; i < Cut.Count; i++)
            {
                film    = new Complex[s, s];
                H       = new Complex[s, s];
                O_image = new Complex[s, s];
                d1      = d - Cut[i] * Hologram_sampling_interval / 2;


                for (int j = 0; j < obj_z.Count; j++)
                {
                    if (Cut[i] == obj_z[j])
                    {
                        O_image[Convert.ToInt32(obj_x[j]), Convert.ToInt32(obj_y[j])] = (Complex)obj_r[j];
                    }
                }

                FourierTransform.FFT2(O_image, FourierTransform.Direction.Backward); // fft2    O_image = fft2(O_image);

                // Stopwatch stopwatch = Stopwatch.StartNew(); //creates and start the instance of Stopwatch



                Complex com2 = Complex.Exp(new Complex(0, (1 * k * d1)));
                for (var p = 0; p < s; p++)
                {    //  H = exp(1i*k*d1).*exp(-1i*pi*lambda*d1*(x.^2+y.^2));
                    Complex com1;
                    for (var j = 0; j < s; j++)
                    {
                        com1 = Complex.Exp(new Complex(0, (-1 * Math.PI * lambda * d1 * (Math.Pow(x[p, j], 2) + Math.Pow(y[p, j], 2)))));

                        H[p, j]    = Complex.Multiply(com1, com2);             // H = exp(1i*k*d1).*exp(-1i*pi*lambda*d1*(x.^2+y.^2));
                        film[p, j] = Complex.Multiply(O_image[p, j], H[p, j]); // lol =O_image.*H;
                    }
                }

                //////Stop();
                //Console.WriteLine(stopwatch.ElapsedMilliseconds);
                FourierTransform.FFT2(film, FourierTransform.Direction.Forward);    //ifft2    film3 =ifft2(lol);


                for (int p = 0; p < s; p++)
                {
                    for (int j = 0; j < s; j++)
                    {
                        Hologram[p, j] = Complex.Add(Hologram[p, j], film[p, j]);  //  Hologram = Hologram+film3;

                        if (i == Cut.Count - 1)
                        {
                            phase_h[p, j] = Hologram[p, j].Phase + Math.PI;    //phase_H = angle(Hologram) + pi;
                            if (phase_h[p, j] > max)
                            {
                                max = phase_h[p, j];
                            }
                        } // end of finding max and adding phase
                    }
                }
            } //end of Cut


            // Console.WriteLine(max);
            byte[,] phase_h_image = new byte[s, s];
            double d2 = d - o * 0.0001;

            Complex com3 = Complex.Exp(new Complex(0, (1 * k * -d2)));

            for (int p = 0; p < s; p++)
            {
                Complex com1;

                for (int j = 0; j < s; j++)
                {
                    double temp = 255 * phase_h[p, j] / max;   ///phase_H_image = uint8(255*phase_H/max(max(phase_H)));
                    phase_h_image[p, j] = System.Convert.ToByte(temp);

                    com1    = Complex.Exp(new Complex(0, (-1 * Math.PI * lambda * -d2 * (Math.Pow(x[p, j], 2) + Math.Pow(y[p, j], 2)))));
                    H[p, j] = Complex.Multiply(com1, com3);         //H = exp(1i*k*z).*exp(-1i*pi*lambda*z*(x.^2+y.^2));
                }
            }


            FourierTransform.FFT2(Hologram, FourierTransform.Direction.Backward);  //  O = fft2(object);

            Complex[,] originalR = new Complex[s, s];
            for (int p = 0; p < s; p++)
            {
                for (int j = 0; j < s; j++)
                {
                    originalR[p, j] = Complex.Multiply(Hologram[p, j], H[p, j]); //hologram =ifft2(O.*H); = >    O_F =  O.*H
                }
            }


            FourierTransform.FFT2(originalR, FourierTransform.Direction.Forward);  // hologram =ifft2(O_F)

            double[,] O_F = new double[s, s];
            for (int p = 0; p < s; p++)
            {
                for (int j = 0; j < s; j++)
                {
                    O_F[p, j] = originalR[p, j].Magnitude;   //abs(originalR)
                }
            }


            Bitmap     bmp = ToBitmap(O_F);
            PictureBox P   = new PictureBox();


            P.Image = bmp;
            P.Dock  = DockStyle.Fill;
            this.Controls.Add(P);
            this.Show();



            System.Console.WriteLine("END");
        }// end of form
Exemple #19
0
    public Quaternion[] QuaternionFourierTransform(int dim1, int dim2, double[] real, double[] i, double[] j, double[] k, bool inverse)
    {
        //Create Arrays of Complex data
        Complex[,] f1 = new Complex[dim1, dim2];
        Complex[,] f2 = new Complex[dim1, dim2];

        //For loop for filling in the data to two complex arrays one consisting of r and i.
        //The other j and k.

        for (int x = 0; x < dim1 - 4; x++)
        {
            for (int y = 0; y < dim2; y++)
            {
                f1[x, y] = new Complex(real[y * dim2 + x], i[y * dim2 + x]);
                f2[x, y] = new Complex(j[y * dim2 + x], k[y * dim2 + x]);
            }
        }

        //If statement to check whether to do forward or backward FFT.
        if (inverse)
        {
            FourierTransform.FFT2(f1, FourierTransform.Direction.Backward);
            FourierTransform.FFT2(f2, FourierTransform.Direction.Backward);
        }
        else
        {
            FourierTransform.FFT2(f1, FourierTransform.Direction.Forward);
            FourierTransform.FFT2(f2, FourierTransform.Direction.Forward);
        }

        //Concat the two FFTS to one single quaternion array.
        Quaternion[] Concat = new Quaternion[dim1 * dim2];

        for (int x = 0; x < dim1; x++)
        {
            for (int y = 0; y < dim2; y++)
            {
                Concat[x + y * dim2] = new Quaternion((float)f1[x, y].Im, (float)f2[x, y].Re, (float)f2[x, y].Im, (float)f1[x, y].Re);
            }
        }
        return(Concat);


        //Concatenate

        //int align = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Complex));// sizeof(Complex);
        // Do something on frequency domain

        //backward_1.fftNormalized(f1);
        //backward_2.fftNormalized(f2);
        //
        //for (int j = 0; j < dim1; j++)
        //{
        //    for (int i = 0; i < dim2; i++)
        //    {
        //        double p1 = real(f1(i, j));
        //        double p2 = imag(f1(i, j));
        //        double p3 = real(f2(i, j));
        //        double p4 = imag(f2(i, j));
        //
        //        // Do something after inverse transform
        //    }
        //}
    }
Exemple #20
0
        //public static double[,] ConvolutionFFT(int[,] image, int[,] kernel)
        //{
        //    int maxSize = Math.Max(Math.Max(Math.Max(image.GetLength(0), image.GetLength(1)), kernel.GetLength(0)), kernel.GetLength(1));
        //    int size = 1;
        //    while (size < maxSize)
        //    {
        //        size *= 2;
        //    }
        //    size *= 2;
        //    Console.WriteLine(size);

        //    //Create complext image
        //    Complex[,] image_complex = new Complex[size, size];
        //    for (int i = 0; i < image.GetLength(0); i++)
        //    {
        //        for (int j = 0; j < image.GetLength(1); j++)
        //        {
        //            image_complex[i,j] = new Complex(image[i,j], 0);
        //        }
        //    }
        //    //Create complext kernel
        //    Complex[,] kernel_complexl = new Complex[size, size];
        //    for (int i = 0; i < kernel.GetLength(0); i++)
        //    {
        //        for (int j = 0; j < kernel.GetLength(1); j++)
        //        {
        //            kernel_complexl[i, j] = new Complex(kernel[i, j], 0);
        //        }
        //    }
        //    kernel_complexl[0, 0] = new Complex(1, 0);
        //    //Do work
        //    FourierTransform.FFT2(image_complex, FourierTransform.Direction.Forward);
        //    FourierTransform.FFT2(kernel_complexl, FourierTransform.Direction.Forward);
        //    for (int i = 0; i < size; i++)
        //    {
        //        for (int j = 0; j < size; j++)
        //        {
        //            image_complex[i, j] = image_complex[i, j] * new Complex(kernel_complexl[i, j].Re, -kernel_complexl[i, j].Im);
        //        }
        //    }
        //    FourierTransform.FFT2(image_complex, FourierTransform.Direction.Backward);


        //    //Do get result
        //    double normalization = size * size;
        //    double[,] result = new double[image.GetLength(0), image.GetLength(1)];
        //    for (int i = 0; i < result.GetLength(0); i++)
        //    {
        //        for (int j = 0; j < result.GetLength(0); j++)
        //        {
        //            result[i, j] = image_complex[i, j].Re * normalization;
        //        }
        //    }
        //    return result;
        //}


        public static double[,] CorrelationFFT(double[,] image, double[,] kernel)
        {
            double image_min = double.MaxValue;
            double image_max = double.MinValue;

            double kernel_min = double.MaxValue;
            double kernel_max = double.MinValue;

            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    image_min = Math.Min(image_min, image[i, j]);
                    image_max = Math.Max(image_max, image[i, j]);
                }
            }

            for (int i = 0; i < kernel.GetLength(0); i++)
            {
                for (int j = 0; j < kernel.GetLength(1); j++)
                {
                    kernel_min = Math.Min(kernel_min, kernel[i, j]);
                    kernel_max = Math.Max(kernel_max, kernel[i, j]);
                }
            }

            int maxSize = Math.Max(Math.Max(Math.Max(image.GetLength(0), image.GetLength(1)), kernel.GetLength(0)), kernel.GetLength(1));
            int size    = 1;

            while (size < maxSize)
            {
                size *= 2;
            }
            //size *= 2;
            Console.WriteLine(size);

            //Create complext image
            Complex[,] image_complex = new Complex[size, size];
            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    image_complex[i, j] = new Complex((image[i, j] - image_min) / (image_max - image_min), 0);
                }
            }
            //Create complext kernel
            double kernel_sum = 0;

            Complex[,] kernel_complexl = new Complex[size, size];
            for (int i = 0; i < kernel.GetLength(0); i++)
            {
                for (int j = 0; j < kernel.GetLength(1); j++)
                {
                    kernel_sum           += (kernel[i, j] - kernel_min) / (kernel_max - kernel_min);
                    kernel_complexl[i, j] = new Complex((kernel[i, j] - kernel_min) / (kernel_max - kernel_min), 0);
                }
            }
            kernel_complexl[0, 0] = new Complex(1, 0);
            //Do work
            FourierTransform.FFT2(image_complex, FourierTransform.Direction.Forward);
            FourierTransform.FFT2(kernel_complexl, FourierTransform.Direction.Forward);
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    image_complex[i, j] = image_complex[i, j] * new Complex(kernel_complexl[i, j].Re, -kernel_complexl[i, j].Im);
                }
            }
            FourierTransform.FFT2(image_complex, FourierTransform.Direction.Backward);

            double normalization = size * size / kernel_sum;

            double[,] result = new double[image.GetLength(0), image.GetLength(1)];
            for (int i = 0; i < result.GetLength(0); i++)
            {
                for (int j = 0; j < result.GetLength(1); j++)
                {
                    result[i, j] = image_complex[i, j].Re * normalization;
                }
            }
            return(result);
        }
Exemple #21
0
        private void Form1_Load(object sender, EventArgs e)
        {
            String address = "D:/workshop/holography/WindowsFormsApplication1/WindowsFormsApplication1";

            String file_name = "/Tang25.txt";

            file_manager file = new file_manager(file_name, address);

            file.read_coordinate();
            // file.write_coordinate_file(file.x, file.y, file.z, file.r, file.g, file.b);


            List <double> obj_x = new List <double>();
            List <double> obj_y = new List <double>();
            List <double> obj_z = new List <double>();
            List <double> obj_r = new List <double>();
            List <double> obj_g = new List <double>();
            List <double> obj_b = new List <double>();

            List <double> O = new List <double>();

            for (int i = 0; i < file.x.Count; i++)
            {
                //System.Console.WriteLine(i + " " + file.x[i] + " " + file.y[i] + " " + file.z[i]);

                obj_x.Add(Math.Round((Convert.ToDouble(file.x[i]) + 100) * 5));
                obj_y.Add(Math.Round((Convert.ToDouble(file.y[i]) + 50) * 6));
                obj_z.Add(-Math.Round(Convert.ToDouble(file.z[i]) - 230));

                obj_r.Add(Convert.ToDouble(file.r[i]));
                obj_g.Add(Convert.ToDouble(file.g[i]));
                obj_b.Add(Convert.ToDouble(file.b[i]));
            }

            int    o      = 8;
            int    t      = 1;
            int    s      = 1024;
            double d      = 0.25;
            double lambda = 532e-9;
            double k      = 2 * Math.PI / lambda;

            double Hologram_sampling_interval = 7.4e-6;
            double dx = Hologram_sampling_interval;
            double dy = Hologram_sampling_interval;

            double[,] image = new double[s, s];



            for (int i = 0; i < obj_x.Count; i++)
            {
                obj_x[i] = obj_x[i] * t;
                obj_y[i] = obj_y[i] * t;
                obj_z[i] = obj_z[i] * 1;
            }


            List <double> Cut = obj_z.Distinct().ToList();

            Cut.Sort();
            double Ny = s;
            double Nx = s;
            double fx = 1 / (dx * Nx);
            double fy = 1 / (dy * Ny);

            double[,] x = new double[s, s];
            double[,] y = new double[s, s];



            Complex[,] O_image = new Complex[s, s];

            Complex complex1 = new Complex(1, 1);

            Complex[,] H    = new Complex[s, s];
            Complex[,] film = new Complex[s, s];
            Complex[][] Hologram = new Complex[s][];
            Complex[][] O_F;

            int counter_y = 0;

            for (int i = 0; i < s; i++)
            {
                // O_image[i]=new Complex[s];
                // film[i] = new Complex[s];
                //H[i] = new Complex[s];
                Hologram[i] = new Complex[s];



                int counter_x = 0;
                if (counter_y == s / 2)
                {
                    counter_y *= -1;
                }

                for (int j = 0; j < s; j++)
                {
                    if (counter_x == s / 2)
                    {
                        counter_x *= -1;
                    }

                    x[i, j] = counter_x * fx;
                    y[i, j] = counter_y * fy;

                    counter_x++;
                }

                counter_y++;
            }


            double d1;


            for (int i = 0; i < Cut.Count; i++)
            {
                //Complex[][] O_image2 = new Complex[s][];
                // temp[0] = new List<double>();  //for every unique value of z ( Cut) make an object/ layer which should have R,G,B
                for (int j = 0; j < obj_z.Count; j++)
                {
                    if (Cut[i] == obj_z[j])
                    {
                        /* double[] temp = new double[6];
                         * temp[0] = obj_x[j];
                         * temp[1] = obj_y[j];
                         * temp[2] = obj_z[j];
                         *
                         * temp[3] = obj_r[j];
                         * temp[4] = obj_g[j];
                         * temp[5] = obj_b[j];
                         * O.AddRange(temp);
                         */
                        O_image[Convert.ToInt32(obj_x[j]), Convert.ToInt32(obj_y[j])] = (Complex)obj_r[j];
                    }
                }

                d1 = d - Cut[i] * Hologram_sampling_interval / 2;


                FourierTransform.FFT2(O_image, FourierTransform.Direction.Forward);


                for (var p = 0; p < s; p++)
                {    // fourier transform all values of x and y
                    Complex com1;
                    Complex com2;
                    for (var j = 0; j < s; j++)
                    {
                        com1    = Complex.Exp(new Complex(0, (-complex1.Im * Math.PI * lambda * d1 * (Math.Pow(x[p, j], 2) + Math.Pow(y[p, j], 2)))));
                        com2    = Complex.Exp(new Complex(0, (complex1.Im * k * d1)));
                        H[i, j] = Complex.Multiply(com1, com2);
                    }
                }

                for (int p = 0; p < s; p++)
                {
                    for (int j = 0; j < s; j++)
                    {
                        film[i, j] = Complex.Multiply(O_image[i, j], H[i, j]);
                    }
                }

                FourierTransform.FFT2(film, FourierTransform.Direction.Backward);


                for (int p = 0; p < Hologram.Length; p++)
                {
                    for (int j = 0; j < Hologram[0].Length; j++)
                    {
                        Hologram[i][j] = Complex.Add(Hologram[p][j], film[p, j]);
                    }
                }
            } //end of Cut


            double[,] phase_h = new double[s, s];
            double max = -1.0;

            for (int p = 0; p < Hologram.Length; p++)
            {
                for (int j = 0; j < Hologram[0].Length; j++)
                {
                    phase_h[p, j] = Hologram[p][j].Phase + Math.PI;
                    if (Hologram[p][j].Phase > max)
                    {
                        max = Hologram[p][j].Phase;
                    }
                }
            }


            double[][] phase_h_image = new double[s][];
            for (int p = 0; p < Hologram.Length; p++)
            {
                phase_h_image[p] = new double[s];

                for (int j = 0; j < Hologram[0].Length; j++)
                {
                    double temp = 255 * phase_h[p, j] / max;
                    //phase_h_image[p][j] = System.Convert.ToByte(temp);
                }
            }

            double d2 = d - o * 0.0001;


            /*FresnelPropagation2
             *
             *
             *
             *
             * */



            for (var p = 0; p < s; p++)
            {    // fourier transform all values of x and y
                Complex com1;
                Complex com2;
                for (var j = 0; j < s; j++)
                {
                    com1    = Complex.Exp(new Complex(0, (-complex1.Im * Math.PI * lambda * -d2 * (Math.Pow(x[p, j], 2) + Math.Pow(y[p, j], 2)))));
                    com2    = Complex.Exp(new Complex(0, (complex1.Im * k * -d2)));
                    H[p, j] = Complex.Multiply(com1, com2);
                }
            }


            // O_F = FourierTransform2.FFT2(Hologram, "Forward");

            /* for (int p = 0; p < O_F.Length; p++)
             * {
             *
             *   for (int j = 0; j < O_F[0].Length; j++)
             *   {
             *       Hologram[p][j] = Complex.Multiply(O_F[p][j], H[p][j]);
             *
             *   }
             *
             * }
             * /
             * //Hologram = FourierTransform2.FFT2(film, "Backward");
             *
             *
             *
             *
             *
             * this.BackColor = Color.FromArgb(255, 102, 178);
             */

            System.Console.WriteLine("END");
        }// end of form
Exemple #22
0
 private void button3_Click(object sender, EventArgs e)
 {
     fft_image = Complex.CreateComplexArray(hpi.images[0]);
     FourierTransform.FFT2(fft_image, FourierTransform.Direction.Forward);
     plot(Tools.GetABS(fft_image));
 }