2016-06-12 20 views
0

Wie zu konvertieren Bitmap zu Komplexe Nummer?Wie können wir ein Bitmap-Objekt in eine komplexe Zahl konvertieren?

 public static COMPLEX [,] BitmapToComplex2D(Bitmap bmp) 
     { 
      COMPLEX[,] comp = new COMPLEX[bmp.Width, bmp.Height]; 

      int [,] array2D = BitmapToArray2D(bmp); 

      for (int i = 0; i <= bmp.Width - 1; i++) 
      { 
       for (int j = 0; j <= bmp.Height - 1; j++) 
       { 
        comp[i,j].RealPart = ???; 
        comp[i,j].ImaginaryPart = ???; 
       } 
      } 

      return comp; 
     } 

Da ...

2D-Array-Wandler

public static Bitmap Array2DToBitmap(int[,] image) 
     { 
      int i, j; 
      Bitmap output = new Bitmap(image.GetLength(0), image.GetLength(1)); 
      BitmapData bitmapData1 = output.LockBits(new Rectangle(0, 0, image.GetLength(0), image.GetLength(1)), 
            ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 
      unsafe 
      { 
       byte* imagePointer1 = (byte*)bitmapData1.Scan0; 
       for (i = 0; i < bitmapData1.Height; i++) 
       { 
        for (j = 0; j < bitmapData1.Width; j++) 
        { 
         imagePointer1[0] = (byte)image[j, i]; 
         imagePointer1[1] = (byte)image[j, i]; 
         imagePointer1[2] = (byte)image[j, i]; 
         imagePointer1[3] = 255; 
         //4 bytes per pixel 
         imagePointer1 += 4; 
        }//end for j 
        //4 bytes per pixel 
        imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4)); 
       }//end for i 
      }//end unsafe 
      output.UnlockBits(bitmapData1); 

      return output;// col; 
     }   

2D komplexe Zahl Bitmap in Bitmap (Ist es richtig?)

 public static Bitmap Complex2DToBitmap(COMPLEX[,] comp) 
     { 
      Bitmap output = new Bitmap(comp.GetLength(0), comp.GetLength(1)); 

      int[,] GreyscaleImage2DArray = new int[output.Width, output.Height]; 

      for (int i = 0; i <= output.Width - 1; i++) 
      { 
       for (int j = 0; j <= output.Height - 1; j++) 
       { 
        GreyscaleImage2DArray[i, j] = (int)comp[i, j].Magnitude(); 
       } 
      } 

      output = ImageConverter.Array2DToBitmap(GreyscaleImage2DArray); 

      return output; 
     } 
+0

In 'Array2DToBitmap' werfen Sie' int' nach 'byte', sind Sie sicher, dass alle Werte auf' image' zwischen 0 und 255 liegen? – Shago

+0

Ich arbeite nur mit Graustufen. – anonymous

Antwort

0

Anstatt die Magnitu zu vermitteln de jeder komplexen Zahl würde ich den reellen Teil und den imaginären Teil getrennt auf verschiedenen Pixeln kodieren.

public static Bitmap Complex2DToBitmap(COMPLEX[,] comp) 
{ 
    Bitmap output = new Bitmap(comp.GetLength(0), comp.GetLength(1)); 

    // Double the width for storing real and imaginary parts on different pixels. 
    int[,] GreyscaleImage2DArray = new int[output.Width * 2, output.Height]; 

    // Increase the step to 2, and decrease the limit by 1. 
    for (int i = 0; i <= output.Width - 2; i+=2) 
    { 
     for (int j = 0; j <= output.Height - 1; j++) 
     { 
      // Store RealPart and ImaginaryPart on different pixels. 
      GreyscaleImage2DArray[i, j] = (int)comp[i, j].RealPart; 
      GreyscaleImage2DArray[i+1, j] = (int)comp[i, j].ImaginaryPart; 
     } 
    } 

    output = ImageConverter.Array2DToBitmap(GreyscaleImage2DArray); 

    return output; 
} 

public static COMPLEX [,] BitmapToComplex2D(Bitmap bmp) 
{ 
    // Halve the width, each complex is stored in 2 pixels. 
    COMPLEX[,] comp = new COMPLEX[(int)(bmp.Width/2), bmp.Height]; 

    int [,] array2D = BitmapToArray2D(bmp); 

    for (int i = 0; i <= bmp.Width - 1; i++) 
    { 
     for (int j = 0; j <= bmp.Height - 1; j++) 
     { 
      comp[i, j].RealPart = array2D[i * 2, j]; 
      comp[i, j].ImaginaryPart = array2D[i * 2 + 1, j]; 
     } 
    } 

    return comp; 
}