我们如何将Bitmap对象转换为复数?

时间:2016-06-12 02:32:57

标签: arrays c#-4.0 bitmap complex-numbers

如何将位图转换为复数

        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阵列到位图转换器

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复数到位图(是否正确?)

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

1 个答案:

答案 0 :(得分: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;
}