/// <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 Exception("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 Exception("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); }
static void Main(string[] args) { bool Sequence = true; if (args.Length > 1 && !Sequence) { Console.WriteLine(DateTime.Now.TimeOfDay); foreach (var picture in args) { var processInfo = new ProcessStartInfo { UseShellExecute = false, // change value to false FileName = "FFTc.exe", Arguments = picture }; Console.WriteLine("Starting process for " + "\\img\\" + picture + ".bmp"); Process.Start(processInfo); } } else if (Sequence) { Stopwatch stopwatch = Stopwatch.StartNew(); foreach (var picture in args) { Bitmap image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + picture + ".bmp"); ComplexImage complexImage = ComplexImage.FromBitmap(image); complexImage.ForwardFourierTransform(); Bitmap fourierImage = complexImage.ToBitmap(); // Console.WriteLine(System.IO.Directory.GetCurrentDirectory() + "\\img\\"+ args[0] + "fourier.bmp"); fourierImage.Save(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + picture + "fourier.bmp"); // ComplexImage backward = ComplexImage.FromBitmap(fourierImage); complexImage.BackwardFourierTransform(); Bitmap backwardFourier = complexImage.ToBitmap(); backwardFourier.Save(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + picture + "backwardFourier.bmp"); } stopwatch.Stop(); Console.WriteLine("Sekwencyjne wykonywanie programu trwało: " + stopwatch.ElapsedMilliseconds + " milisekund.\n "); } else if (args.Length == 1) { Bitmap image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + args[0] + ".bmp"); ComplexImage complexImage = ComplexImage.FromBitmap(image); // Stopwatch stopwatch = Stopwatch.StartNew(); complexImage.ForwardFourierTransform(8); // stopwatch.Stop(); // Console.WriteLine(stopwatch.ElapsedMilliseconds); Bitmap fourierImage = complexImage.ToBitmap(); // Console.WriteLine(System.IO.Directory.GetCurrentDirectory() + "\\img\\"+ args[0] + "fourier.bmp"); fourierImage.Save(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + args[0] + "fourier.bmp"); // Stopwatch stopwatch2 = Stopwatch.StartNew(); // ComplexImage backward = ComplexImage.FromBitmap(fourierImage); complexImage.BackwardFourierTransform(8); // stopwatch2.Stop(); // Console.WriteLine(stopwatch2.ElapsedMilliseconds); Bitmap backwardFourier = complexImage.ToBitmap(); backwardFourier.Save(System.IO.Directory.GetCurrentDirectory() + "\\img\\" + args[0] + "backwardFourier.bmp"); Console.WriteLine("Proces dla obrazka " + args[0] + " zakończył się o: " + DateTime.Now.TimeOfDay); } Console.ReadLine(); }