2016-06-12 2 views
0

Как конвертировать Растровое изображение на Complex Number?Как мы можем преобразовать объект Bitmap в комплексное число?

 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; 
     } 

Учитывая, что ...

2D массива Bitmap конвертер

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 комплексное число в Bitmap (Является ли это правильно?)

 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

В 'Array2DToBitmap' вы вводите' int' в 'byte', вы уверены, что все значения на' image' находятся в диапазоне от 0 до 255? – Shago

+0

Я работаю только с оттенками серого. – anonymous

ответ

0

Вместо того, чтобы учитывать величину каждого сложного числа я бы кодировал бы реальную часть и мнимую часть, разделенную на разные пиксели.

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; 
} 
Смежные вопросы