Esempio n. 1
0
        // Backward Fourier transformation
        private void backwardFourierItem_Click(object sender, System.EventArgs e)
        {
            ComplexImage cimg = (ComplexImage)image.Clone( );

            cimg.BackwardFourierTransform( );
            host.NewDocument(cimg.ToBitmap( ));
        }
Esempio n. 2
0
        public void Apply(ComplexImage complexImage)
        {
            if (!complexImage.FourierTransformed)
            {
                throw new ArgumentException("The source complex image should be Fourier transformed.");
            }
            int width  = complexImage.Width;
            int height = complexImage.Height;
            int num    = width >> 1;
            int num2   = height >> 1;
            int min    = frequencyRange.Min;
            int max    = frequencyRange.Max;

            Complex[,] data = complexImage.Data;
            for (int i = 0; i < height; i++)
            {
                int num3 = i - num2;
                for (int j = 0; j < width; j++)
                {
                    int num4 = j - num;
                    int num5 = (int)System.Math.Sqrt(num4 * num4 + num3 * num3);
                    if (num5 > max || num5 < min)
                    {
                        data[i, j].Re = 0.0;
                        data[i, j].Im = 0.0;
                    }
                }
            }
        }
Esempio n. 3
0
        public static BitmapImage FromFourierToPhase(this ComplexImage complexImage, string fileName)
        {
            Bitmap bitmap = new Bitmap(fileName).ScaleToPowerBy2();

            for (int i = 0; i < complexImage.Height; i++)
            {
                for (int j = 0; j < complexImage.Width; j++)
                {
                    double phase = complexImage.Data[i, j].Phase; //Bierzemy faze w radianach czyli od -PI(-3.14) do +PI(3.14)
                    phase = phase + Math.PI;                      //Dodajemy PI żeby zakres był od PI do 2Pi
                    bitmap.SetPixel(j, i, ColorLerp(Color.White, Color.Black, (float)(phase / (2 * Math.PI))));
                }
            }

            using (var memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();

                return(bitmapImage);
            }
        }
Esempio n. 4
0
 public Bitmap ImagenCompleja()
 {
     cimage = ComplexImage.FromBitmap(imagen);
     cimage.ForwardFourierTransform();
     imagen = cimage.ToBitmap();
     return(imagen);
 }
        public void Apply(ComplexImage complexImage)
        {
            if (!complexImage.FourierTransformed)
            {
                throw new ArgumentException("The source complex image should be Fourier transformed.");
            }
            int width  = complexImage.Width;
            int height = complexImage.Height;
            int num3   = width >> 1;
            int num4   = height >> 1;
            int min    = this.frequencyRange.Min;
            int max    = this.frequencyRange.Max;

            Complex[,] data = complexImage.Data;
            for (int i = 0; i < height; i++)
            {
                int num8 = i - num4;
                for (int j = 0; j < width; j++)
                {
                    int num10 = j - num3;
                    int num11 = (int)Math.Sqrt((double)((num10 * num10) + (num8 * num8)));
                    if ((num11 > max) || (num11 < min))
                    {
                        data[i, j].Re = 0.0;
                        data[i, j].Im = 0.0;
                    }
                }
            }
        }
Esempio n. 6
0
        public static Bitmap ToBitmapPhase(ComplexImage complexImage, int width)
        {
            // create new image
            Bitmap dstImage = AForge.Imaging.Image.CreateGrayscaleImage(width, width);

            // lock destination bitmap data
            BitmapData dstData = dstImage.LockBits(
                new Rectangle(0, 0, width, width),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int    offset = dstData.Stride - width;
            double scale  = Math.Sqrt(width * width);

            // do the job
            unsafe
            {
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                for (int y = 0; y < width; y++)
                {
                    for (int x = 0; x < width; x++, dst++)
                    {
                        *dst = (byte)Math.Max(0, Math.Min(255, complexImage.Data[y, x].Phase * scale * 255));
                    }
                    dst += offset;
                }
            }
            // unlock destination images
            dstImage.UnlockBits(dstData);

            return(dstImage);
        }
Esempio n. 7
0
 public void Apply(ComplexImage inFreqz, ComplexImage outFreqz)
 {
     Debug.Assert(inFreqz.Width == outFreqz.Width);
     Debug.Assert(inFreqz.Height == outFreqz.Height);
     outFreqz.C0 = inFreqz.C0 * C0;
     outFreqz.C1 = inFreqz.C1 * C1;
     outFreqz.C2 = inFreqz.C2 * C2;
 }
Esempio n. 8
0
        public static BitmapImage FastFourierTransform(BitmapImage image, out ComplexImage complexImage)
        {
            Bitmap bitmap = image.ToBitmap().ScaleToPowerBy2().ToGrayScale();

            complexImage = ComplexImage.FromBitmap(bitmap);
            complexImage.ForwardFourierTransform();
            return(complexImage.ToBitmapImage());
        }
Esempio n. 9
0
        public Bitmap GetBackwardFourierTransform()
        {
            ComplexImage copy = _complexImage;

            copy.BackwardFourierTransform();
            Bitmap fourierImage = copy.ToBitmap();

            return(fourierImage);
        }
Esempio n. 10
0
 // Undo filter
 private void undoFourierItem_Click(object sender, System.EventArgs e)
 {
     if (backup != null)
     {
         image  = backup;
         backup = null;
         UpdateNewImage( );
     }
 }
Esempio n. 11
0
        public Bitmap GetBackFTT()
        {
            ComplexImage copy = _image;

            copy.BackwardFourierTransform();
            Bitmap bitmap = copy.ToBitmap();

            return(bitmap);
        }
Esempio n. 12
0
        public Bitmap GetForwardFFT()
        {
            ComplexImage copy = _image;

            copy.ForwardFourierTransform();
            Bitmap bitmap = copy.ToBitmap();

            return(bitmap);
        }
Esempio n. 13
0
        public void DecodeTest()
        {
            string image = "123456789012";
            var    data  = UtilsDay8.StringToIntArray(image);
            var    img   = new ComplexImage(3, 2, data);

            Assert.Equal(new int[] { 1, 2, 3 }, img.Data[0][0]);
            Assert.Equal(new int[] { 4, 5, 6 }, img.Data[0][1]);
            Assert.Equal(new int[] { 7, 8, 9 }, img.Data[1][0]);
            Assert.Equal(new int[] { 0, 1, 2 }, img.Data[1][1]);
        }
Esempio n. 14
0
        public static BitmapImage MakeFFTMagnitude(Bitmap image)
        {
            Grayscale    filter       = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap       grayImage    = filter.Apply(image);
            ComplexImage compleximage = ComplexImage.FromBitmap(grayImage);

            compleximage.ForwardFourierTransform();
            Bitmap fi = compleximage.ToBitmap();

            return(ToBitmapImage(fi));
        }
Esempio n. 15
0
        public static Bitmap BFT(Bitmap inputImage)
        {
            System.Drawing.Image sizeImage = new Bitmap(inputImage, 512, 512);
            //inputImage.Dispose();
            inputImage = (Bitmap)(sizeImage);
            inputImage = ColorToGrayscale(inputImage);
            ComplexImage finalImg = ComplexImage.FromBitmap(inputImage);

            finalImg.BackwardFourierTransform();
            //var data = finalImg.Data;
            return(finalImg.ToBitmap());
        }
Esempio n. 16
0
        // Create new document for ComplexImage
        public bool NewDocument(ComplexImage image)
        {
            unNamedNumber++;

            FourierDoc imgDoc = new FourierDoc(image, (IDocumentsHost)this);

            imgDoc.Text = "Image " + unNamedNumber.ToString();
            imgDoc.Show(dockManager);
            imgDoc.Focus();

            return(true);
        }
Esempio n. 17
0
        private void Button4_Click(object sender, EventArgs e)
        {
            //var bit8 = new Bitmap(curBitmap.Width, curBitmap.Height, PixelFormat.Format8bppIndexed);
            var bit8 = Tool.RgbToGrayScale(curBitmap);

            orignal8 = bit8;
            ComplexImage complexImage = ComplexImage.FromBitmap(bit8);

            // 进行正向傅立叶变换,即将图像从空间域向频率域转换
            complexImage.ForwardFourierTransform();

            curBitmap = complexImage.ToBitmap();
            Invalidate();
        }
Esempio n. 18
0
        private void FFTfilter(object sender, EventArgs e)
        {
            ComplexImage complexImage = ComplexImage.FromBitmap(orignal8);

            complexImage.ForwardFourierTransform();
            //通过频率范围创建过滤器
            AForge.Imaging.ComplexFilters.FrequencyFilter filter = new AForge.Imaging.ComplexFilters.FrequencyFilter(new IntRange(10, 100));
            // 频率过滤
            filter.Apply(complexImage);
            //逆向频率域操作
            complexImage.BackwardFourierTransform();
            curBitmap = complexImage.ToBitmap();
            Invalidate();
        }
Esempio n. 19
0
        public override void Load()
        {
            if (Fetched)
            {
                return;
            }

            var images = DoFetchImages(FileNames);

            _fft        = Fourier.FFT2(Images[0], false);
            ViewColumns = 9;
            ViewRows    = 9;
            Images      = images;
            Rearrange(0, 0, 300, 150, 9);
        }
Esempio n. 20
0
        // Frequency filter
        private void frequencyFilterFourierItem_Click(object sender, System.EventArgs e)
        {
            FrequencyFilter form = new FrequencyFilter( );

            form.InputRange  = new IntRange(0, width >> 1);
            form.OutputRange = new IntRange(0, width >> 1);

            if (form.ShowDialog( ) == DialogResult.OK)
            {
                backup = (ComplexImage)image.Clone( );

                image.FrequencyFilter(form.OutputRange);
                UpdateNewImage( );
            }
        }
Esempio n. 21
0
        public static Bitmap GetCoreImage(Bitmap initialImage, Filter filter)
        {
            var g    = ComplexImage.FromBitmap(initialImage);
            var core = filter != Filter.Predict ? WienerFilter.GetCore(g, filter) : WienerPredictFilter.Core;
            var h    = GetComplexImageFromMatrix(core);

            for (int i = 0; i < h.Height; i++)
            {
                for (int j = 0; j < h.Width; j++)
                {
                    h.Data[i, j] *= 255;
                }
            }

            return(h.ToBitmap());
        }
Esempio n. 22
0
        internal static void Rotate(ComplexImage f)
        {
            for (int i = 0; i < f.Height / 2; i++)
            {
                for (int j = 0; j < f.Width / 2; j++)
                {
                    var t = f.Data[i, j];
                    f.Data[i, j] = f.Data[i + f.Height / 2, j + f.Width / 2];
                    f.Data[i + f.Height / 2, j + f.Width / 2] = t;

                    t = f.Data[i + f.Height / 2, j];
                    f.Data[i + f.Height / 2, j] = f.Data[i, j + f.Width / 2];
                    f.Data[i, j + f.Width / 2]  = t;
                }
            }
        }
Esempio n. 23
0
        public DeblurFilter(ComplexImage refFreqz, ComplexImage blurFreqz)
        {
            if (refFreqz == null ||
                blurFreqz == null)
            {
                throw new ArgumentNullException(blurFreqz == null ? "blurred" : "sharp");
            }

            if (!Equals(refFreqz.Width, refFreqz.Height, blurFreqz.Width, blurFreqz.Height))
            {
                throw new ArgumentException("Bitmaps not in the same size.");
            }

            C0 = refFreqz.C0.DivideBy(blurFreqz.C0, Preferences.DIVIDE_BY_ZERO_EPSILON);
            C1 = refFreqz.C1.DivideBy(blurFreqz.C1, Preferences.DIVIDE_BY_ZERO_EPSILON);
            C2 = refFreqz.C2.DivideBy(blurFreqz.C2, Preferences.DIVIDE_BY_ZERO_EPSILON);
        }
Esempio n. 24
0
        public static Bitmap Filter(Bitmap initialImage, ImageHelper.Filter filter)
        {
            var g = ComplexImage.FromBitmap(initialImage);
            var h = ImageHelper.GetComplexImageFromMatrix(GetCore(g, filter));

            var snr = ImageHelper.GetSNR(g.Data);

            var G = ImageHelper.GetComplexImageFromMatrix(ImageHelper.FFT2(ImageHelper.ToVector(g.Data)));
            var H = ImageHelper.GetComplexImageFromMatrix(ImageHelper.FFT2(ImageHelper.ToVector(h.Data)));

            var F = GetF(H, G, 0.035);

            var f = ImageHelper.GetComplexImageFromMatrix(ImageHelper.BFT2(ImageHelper.ToVector(F.Data)));

            ImageHelper.Rotate(f);

            return(f.ToBitmap());
        }
Esempio n. 25
0
        public static ComplexImage GetF(ComplexImage H, ComplexImage G, Complex snr)
        {
            var bitmap     = new Bitmap(G.Width, G.Height);
            var bitmap8bpp = bitmap.ConvertTo8bpp();

            bitmap8bpp.ConvertColor8bppToGrayscale8bpp();

            var complexImage = ComplexImage.FromBitmap(bitmap8bpp);

            for (int i = 0; i < G.Height; i++)
            {
                for (int j = 0; j < G.Width; j++)
                {
                    complexImage.Data[i, j] = ((1 / H.Data[i, j]) * (Math.Pow(Complex.Abs(H.Data[i, j]), 2)
                                                                     / (Math.Pow(Complex.Abs(H.Data[i, j]), 2) + snr))) * G.Data[i, j];
                }
            }
            return(complexImage);
        }
Esempio n. 26
0
        private static BitmapImage ToBitmapImage(this ComplexImage complexImage)
        {
            Bitmap bitmap = complexImage.ToBitmap();

            using (var memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();

                return(bitmapImage);
            }
        }
Esempio n. 27
0
        public static ComplexImage GetComplexImageFromMatrix(double[,] core)
        {
            var bitmap = new Bitmap(core.GetLength(1), core.GetLength(0));

            var bitmap8bpp = bitmap.ConvertTo8bpp();

            bitmap8bpp.ConvertColor8bppToGrayscale8bpp();

            var complexImage = ComplexImage.FromBitmap(bitmap8bpp);

            for (int i = 0; i < complexImage.Height; i++)
            {
                for (int j = 0; j < complexImage.Width; j++)
                {
                    complexImage.Data[i, j] = core[i, j];
                }
            }

            return(complexImage);
        }
Esempio n. 28
0
        /// <summary>
        /// Apply filter to complex image.
        /// </summary>
        ///
        /// <param name="complexImage">Complex image to apply filter to.</param>
        ///
        /// <exception cref="ArgumentException">The source complex image should be Fourier transformed.</exception>
        ///
        public void Apply(ComplexImage complexImage)
        {
            if (!complexImage.FourierTransformed)
            {
                throw new ArgumentException("The source complex image should be Fourier transformed.");
            }

            // get image dimenstion
            var width  = complexImage.Width;
            var height = complexImage.Height;

            // half of dimensions
            var hw = width >> 1;
            var hh = height >> 1;

            // min and max frequencies
            var min = frequencyRange.Min;
            var max = frequencyRange.Max;

            // complex data to process
            var data = complexImage.Data;

            // process all data
            for (var i = 0; i < height; i++)
            {
                var y = i - hh;

                for (var j = 0; j < width; j++)
                {
                    var x = j - hw;
                    var d = (int)System.Math.Sqrt(x * x + y * y);

                    // filter values outside the range
                    if ((d > max) || (d < min))
                    {
                        data[i, j].Re = 0;
                        data[i, j].Im = 0;
                    }
                }
            }
        }
Esempio n. 29
0
        public static void TestFFT()
        {
            var bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(FFT_sampleImgName);

            var          image = bmp.ToImage <Gray, byte>().Convert <Complex, float>();
            ComplexImage cuIm  = ComplexImage.FromBitmap(bmp);

            measure(() =>
            {
                image.FFT(FourierTransform.Direction.Forward);
                image.FFT(FourierTransform.Direction.Backward);
            },
                    () =>
            {
                cuIm.ForwardFourierTransform();
                cuIm.BackwardFourierTransform();
            },
                    100,
                    "Image<,> FFT",
                    "AForge FFT");
        }
Esempio n. 30
0
        private void SpectraWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (input.Width != input.Height)
            {
                throw new System.ApplicationException(
                          "The image height and width must be the same.");
            }

            SpectraWorker.ReportProgress(0, "Computing spectra");

            // The AForge FFT requires an 8 bits per pixel input format.
            Bitmap gray_8bpp
                = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.Y.Apply(input);

            AForge.Imaging.ComplexImage fft
                = ComplexImage.FromBitmap(gray_8bpp);
            fft.ForwardFourierTransform();

            SpectraWorker.ReportProgress(100, "Fourier Spectra Calculated.");
            fourier = fft.ToBitmap();
        }
Esempio n. 31
0
        /// <summary>
        /// Apply filter to complex image.
        /// </summary>
        /// 
        /// <param name="complexImage">Complex image to apply filter to.</param>
        /// 
        /// <exception cref="ArgumentException">The source complex image should be Fourier transformed.</exception>
        /// 
        public void Apply(ComplexImage complexImage)
        {
            if (!complexImage.FourierTransformed)
            {
                throw new ArgumentException("The source complex image should be Fourier transformed.");
            }

            // get image dimenstion
            int width = complexImage.Width;
            int height = complexImage.Height;

            // half of dimensions
            int hw = width >> 1;
            int hh = height >> 1;

            // min and max frequencies
            int min = frequencyRange.Min;
            int max = frequencyRange.Max;

            // complex data to process
            Complex[,] data = complexImage.Data;

            // process all data
            for (int i = 0; i < height; i++)
            {
                int y = i - hh;

                for (int j = 0; j < width; j++)
                {
                    int x = j - hw;
                    int d = (int)Math.Sqrt(x * x + y * y);

                    // filter values outside the range
                    if ((d > max) || (d < min))
                    {
                        data[i, j] = Complex.Zero;
                    }
                }
            }
        }