Complex image.

The class is used to keep image represented in complex numbers sutable for Fourier transformations.

Sample usage:

// create complex image ComplexImage complexImage = ComplexImage.FromBitmap( image ); // do forward Fourier transformation complexImage.ForwardFourierTransform( ); // get complex image as bitmat Bitmap fourierImage = complexImage.ToBitmap( );

Initial image:

Fourier image:

Наследование: ICloneable
Пример #1
0
        public static unsafe ComplexImage FromBitmap(BitmapData imageData)
        {
            if (imageData.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new UnsupportedImageFormatException("Source image can be graysclae (8bpp indexed) image only.");
            }
            int width  = imageData.Width;
            int height = imageData.Height;
            int num3   = imageData.Stride - width;

            if (!Tools.IsPowerOf2(width) || !Tools.IsPowerOf2(height))
            {
                throw new InvalidImagePropertiesException("Image width and height should be power of 2.");
            }
            ComplexImage image = new ComplexImage(width, height);

            Complex[,] data = image.data;
            byte *numPtr = (byte *)imageData.Scan0.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num5 = 0;
                while (num5 < width)
                {
                    data[i, num5].Re = ((float)numPtr[0]) / 255f;
                    num5++;
                    numPtr++;
                }
                numPtr += num3;
            }
            return(image);
        }
Пример #2
0
        public unsafe static ComplexImage FromBitmap(BitmapData imageData)
        {
            if (imageData.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new UnsupportedImageFormatException("Source image can be graysclae (8bpp indexed) image only.");
            }
            int num  = imageData.Width;
            int num2 = imageData.Height;
            int num3 = imageData.Stride - num;

            if (!Tools.IsPowerOf2(num) || !Tools.IsPowerOf2(num2))
            {
                throw new InvalidImagePropertiesException("Image width and height should be power of 2.");
            }
            ComplexImage complexImage = new ComplexImage(num, num2);

            Complex[,] array = complexImage.data;
            byte *ptr = (byte *)imageData.Scan0.ToPointer();

            for (int i = 0; i < num2; i++)
            {
                int num4 = 0;
                while (num4 < num)
                {
                    array[i, num4].Re = (float)(int)(*ptr) / 255f;
                    num4++;
                    ptr++;
                }
                ptr += num3;
            }
            return(complexImage);
        }
        public static Bitmap GetTransferImage(Bitmap srcImage, int imageSize)
        {
            if ((imageSize & (imageSize - 1)) != 0)
            {
                throw new ArgumentException("Wrong Image size!!!");
            }
            else
            {
                IMAGE_SIZE_X = imageSize;
                IMAGE_SIZE_Y = imageSize;
            }

            AForge.Imaging.ComplexImage fft2Image = GetFFT2(srcImage);
            double[,] logAmp = GetLAmp(fft2Image);
            double[,] phase  = GetPhase(fft2Image);
            Bitmap sr     = GetSR(logAmp);
            Bitmap salMap = GetSalMap(sr, phase);

            var filt2 = new Accord.Imaging.Filters.GaussianBlur(2.5, 9);

            filt2.ApplyInPlace(salMap);

            var filtNorm = new Accord.Imaging.Filters.ContrastStretch();

            filtNorm.ApplyInPlace(salMap);

            return(salMap);
        }
        private static double[,] GetPhase(AForge.Imaging.ComplexImage fft2Image)
        {
            AForge.Imaging.ComplexImage complexImage = fft2Image;

            double[,] phase = new double[IMAGE_SIZE_X, IMAGE_SIZE_Y];
            for (int x = 0; x < IMAGE_SIZE_X; x++)
            {
                for (int y = 0; y < IMAGE_SIZE_Y; y++)
                {
                    phase[x, y] = complexImage.Data[x, y].Phase;
                }
            }

            return(phase);
        }
Пример #5
0
        public object Clone()
        {
            ComplexImage complexImage = new ComplexImage(width, height);

            Complex[,] array = complexImage.data;
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    array[i, j] = data[i, j];
                }
            }
            complexImage.fourierTransformed = fourierTransformed;
            return(complexImage);
        }
Пример #6
0
        public object Clone()
        {
            ComplexImage image = new ComplexImage(this.width, this.height);

            Complex[,] data = image.data;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    *(data[i, j]) = *(this.data[i, j]);
                }
            }
            image.fourierTransformed = this.fourierTransformed;
            return(image);
        }
        private static double[,] GetLAmp(AForge.Imaging.ComplexImage fft2Image)
        {
            AForge.Imaging.ComplexImage complexImage = fft2Image;

            double[,] logAmpl = new double[IMAGE_SIZE_X, IMAGE_SIZE_Y];
            for (int x = 0; x < IMAGE_SIZE_X; x++)
            {
                for (int y = 0; y < IMAGE_SIZE_Y; y++)
                {
                    logAmpl[x, y] = Math.Log(complexImage.Data[x, y].Magnitude);
                }
            }

            return(logAmpl);
        }
Пример #8
0
        /// <summary>
        /// Create complex image from grayscale bitmap.
        /// </summary>
        ///
        /// <param name="imageData">Source image data (8 bpp indexed).</param>
        ///
        /// <returns>Returns an instance of complex image.</returns>
        ///
        /// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
        /// <exception cref="InvalidImagePropertiesException">Image width and height should be power of 2.</exception>
        ///
        public static ComplexImage FromBitmap(BitmapData imageData)
        {
            // check image format
            if (imageData.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new UnsupportedImageFormatException("Source image can be graysclae (8bpp indexed) image only.");
            }

            // get source image size
            int width  = imageData.Width;
            int height = imageData.Height;
            int offset = imageData.Stride - width;

            // check image size
            if ((!Tools.IsPowerOf2(width)) || (!Tools.IsPowerOf2(height)))
            {
                throw new InvalidImagePropertiesException("Image width and height should be power of 2.");
            }

            // create new complex image
            ComplexImage complexImage = new ComplexImage(width, height);

            Complex[,] data = complexImage.data;

            // do the job
            unsafe
            {
                byte *src = (byte *)imageData.Scan0.ToPointer();

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, src++)
                    {
                        data[y, x] = new Complex((float)*src / 255, data[y, x].Imaginary);
                    }

                    src += offset;
                }
            }

            return(complexImage);
        }
Пример #9
0
        /// <summary>
        /// Clone the complex image.
        /// </summary>
        ///
        /// <returns>Returns copy of the complex image.</returns>
        ///
        public object Clone()
        {
            // create new complex image
            var dstImage = new ComplexImage(width, height);
            var data     = dstImage.data;

            for (var i = 0; i < height; i++)
            {
                for (var j = 0; j < width; j++)
                {
                    data[i, j] = this.data[i, j];
                }
            }

            // clone mode as well
            dstImage.fourierTransformed = fourierTransformed;

            return(dstImage);
        }
Пример #10
0
        /// <summary>
        /// Clone the complex image.
        /// </summary>
        /// 
        /// <returns>Returns copy of the complex image.</returns>
        /// 
        public object Clone( )
        {
            // create new complex image
            ComplexImage dstImage = new ComplexImage( width, height );
            Complex[,] data = dstImage.data;

            for ( int i = 0; i < height; i++ )
            {
                for ( int j = 0; j < width; j++ )
                {
                    data[i, j] = this.data[i, j];
                }
            }

            // clone mode as well
            dstImage.fourierTransformed = fourierTransformed;

            return dstImage;
        }
Пример #11
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();
        }
Пример #12
0
        // Clone the object
        public object Clone( )
        {
            // create new complex image
            ComplexImage dstImg = new ComplexImage( );

            Complex[,]              data = new Complex[height, width];

            dstImg.data   = data;
            dstImg.width  = width;
            dstImg.height = height;
            dstImg.fmode  = fmode;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    data[i, j] = this.data[i, j];
                }
            }

            return(dstImg);
        }
        private static Bitmap GetSalMap(Bitmap sr, double[,] phase)
        {
            AForge.Imaging.ComplexImage totalSpec = AForge.Imaging.ComplexImage.FromBitmap(sr);

            // exp(SR + 1i * Phase)
            for (int x = 0; x < IMAGE_SIZE_X; x++)
            {
                for (int y = 0; y < IMAGE_SIZE_Y; y++)
                {
                    totalSpec.Data[x, y].Im = phase[x, y];
                    totalSpec.Data[x, y]    = Complex.Exp(totalSpec.Data[x, y]);
                }
            }

            totalSpec.ForwardFourierTransform();

            // .^2
            for (int x = 0; x < IMAGE_SIZE_X; x++)
            {
                for (int y = 0; y < IMAGE_SIZE_Y; y++)
                {
                    totalSpec.Data[x, y] =
                        Complex.Multiply(totalSpec.Data[x, y], totalSpec.Data[x, y]);
                }
            }

            // shift
            Complex[,] shifted = Shift(totalSpec.Data, IMAGE_SIZE_X, IMAGE_SIZE_Y);
            for (int x = 0; x < IMAGE_SIZE_X; x++)
            {
                for (int y = 0; y < IMAGE_SIZE_Y; y++)
                {
                    totalSpec.Data[x, y] = shifted[x, y];
                }
            }

            return(totalSpec.ToBitmap());
        }
Пример #14
0
        // Constructors
        public FourierDoc( ComplexImage image, IDocumentsHost host )
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent( );

            //
            this.host = host;
            this.image = image;

            width = image.Width;
            height = image.Height;

            UpdateNewImage( );

            // form style
            SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true );

            // init scroll bars
            this.AutoScroll = true;
            // scroll bar size
            this.AutoScrollMinSize = new Size( width, height );
        }
Пример #15
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;
        }
Пример #16
0
        // Create ComplexImage from Bitmap
        public static ComplexImage FromBitmap(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            // check image size
            if (
                (!Tools.IsPowerOf2(width)) ||
                (!Tools.IsPowerOf2(height))
                )
            {
                throw new ArgumentException( );
            }

            // create new complex image
            ComplexImage dstImg = new ComplexImage( );

            Complex[,]              data = new Complex[height, width];

            dstImg.data   = data;
            dstImg.width  = width;
            dstImg.height = height;

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, srcImg.PixelFormat);

            int offset = srcData.Stride - ((srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ? width : width * 3);

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer( );

                if (srcImg.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    // grayscale image

                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, src++)
                        {
                            data[y, x].Re = (float)*src / 255;
                        }
                        src += offset;
                    }
                }
                else
                {
                    // RGB image

                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, src += 3)
                        {
                            data[y, x].Re = (float)(0.2125f * src[RGB.R] + 0.7154f * src[RGB.G] + 0.0721f * src[RGB.B]) / 255;
                        }
                        src += offset;
                    }
                }
            }
            // unlock source images
            srcImg.UnlockBits(srcData);

            return(dstImg);
        }
Пример #17
0
 // Undo filter
 private void undoFourierItem_Click( object sender, System.EventArgs e )
 {
     if ( backup != null )
     {
         image = backup;
         backup = null;
         UpdateNewImage( );
     }
 }
Пример #18
0
        // Frequency filter
        private void frequencyFilterFourierItem_Click( object sender, System.EventArgs e )
        {
            FrequencyFilterForm form = new FrequencyFilterForm( );

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

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

                // create and apply filter
                FrequencyFilter filter = new FrequencyFilter( form.OutputRange );
                filter.Apply( image );

                UpdateNewImage( );
            }
        }
        /// <summary>
        /// Create complex image from grayscale bitmap.
        /// </summary>
        /// 
        /// <param name="imageData">Source image data (8 bpp indexed).</param>
        /// 
        /// <returns>Returns an instance of complex image.</returns>
        /// 
        /// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
        /// <exception cref="InvalidImagePropertiesException">Image width and height should be power of 2.</exception>
        /// 
        public static ComplexImage FromBitmap( BitmapData imageData )
        {
            // check image format
            if ( imageData.PixelFormat != PixelFormat.Format8bppIndexed )
            {
                throw new UnsupportedImageFormatException( "Source image can be graysclae (8bpp indexed) image only." );
            }

            // get source image size
            int width  = imageData.Width;
            int height = imageData.Height;
            int offset = imageData.Stride - width;

            // check image size
            if ( ( !Tools.IsPowerOf2( width ) ) || ( !Tools.IsPowerOf2( height ) ) )
            {
                throw new InvalidImagePropertiesException( "Image width and height should be power of 2." );
            }

            // create new complex image
            ComplexImage complexImage = new ComplexImage( width, height );
            Complex[,] data = complexImage.data;

            // do the job
            unsafe
            {
                byte* src = (byte*) imageData.Scan0.ToPointer( );

                // for each line
                for ( int y = 0; y < height; y++ )
                {
                    // for each pixel
                    for ( int x = 0; x < width; x++, src++ )
                    {
                        data[y, x].Re = (float) *src / 255;
                    }
                    src += offset;
                }
            }

            return complexImage;
        }
        /// <summary>
        /// Clone the complex image.
        /// </summary>
        /// 
        /// <returns>Returns copy of the complex image.</returns>
        /// 
        public object Clone( )
        {
            // create new complex image
            ComplexImage dstImage = new ComplexImage( width, height );
            Complex[,] data = dstImage.data;

            for ( int i = 0; i < height; i++ )
            {
                for ( int j = 0; j < width; j++ )
                {
                    data[i, j] = this.data[i, j];
                }
            }

            // clone mode as well
            dstImage.fourierTransformed = fourierTransformed;

            return dstImage;
        }
Пример #21
0
 public Bitmap ImagenCompleja()
 {
     cimage = ComplexImage.FromBitmap(imagen);
     cimage.ForwardFourierTransform();
     imagen = cimage.ToBitmap();
     return imagen;
 }
Пример #22
0
        // Clone the object
        public object Clone( )
        {
            // create new complex image
            ComplexImage	dstImg = new ComplexImage( );
            Complex[,]		data = new Complex[height, width];

            dstImg.data		= data;
            dstImg.width	= width;
            dstImg.height	= height;
            dstImg.fmode	= fmode;

            for ( int i = 0; i < height; i++ )
            {
                for ( int j = 0; j < width; j++ )
                {
                    data[i, j] = this.data[i, j];
                }
            }

            return dstImg;
        }
Пример #23
0
        // Create ComplexImage from Bitmap
        public static ComplexImage FromBitmap( Bitmap srcImg )
        {
            // get source image size
            int width = srcImg.Width;
            int height = srcImg.Height;

            // check image size
            if (
                ( !Tools.IsPowerOf2( width ) ) ||
                ( !Tools.IsPowerOf2( height ) )
                )
            {
                throw new ArgumentException( );
            }

            // create new complex image
            ComplexImage	dstImg = new ComplexImage( );
            Complex[,]		data = new Complex[height, width];

            dstImg.data		= data;
            dstImg.width	= width;
            dstImg.height	= height;

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle( 0, 0, width, height ),
                ImageLockMode.ReadOnly, srcImg.PixelFormat );

            int offset = srcData.Stride - ( ( srcImg.PixelFormat == PixelFormat.Format8bppIndexed ) ? width : width * 3 );

            // do the job
            unsafe
            {
                byte * src = (byte *) srcData.Scan0.ToPointer( );

                if ( srcImg.PixelFormat == PixelFormat.Format8bppIndexed )
                {
                    // grayscale image

                    // for each line
                    for ( int y = 0; y < height; y++ )
                    {
                        // for each pixel
                        for ( int x = 0; x < width; x++, src ++ )
                        {
                            data[y, x].Re = (float) *src / 255;
                        }
                        src += offset;
                    }
                }
                else
                {
                    // RGB image

                    // for each line
                    for ( int y = 0; y < height; y++ )
                    {
                        // for each pixel
                        for ( int x = 0; x < width; x++, src += 3 )
                        {
                            data[y, x].Re = (float) ( 0.2125f * src[RGB.R] + 0.7154f * src[RGB.G] + 0.0721f * src[RGB.B] ) / 255;
                        }
                        src += offset;
                    }
                }
            }
            // unlock source images
            srcImg.UnlockBits( srcData );

            return dstImg;
        }