C# 将矩阵划分为子块

C# Dividing a Matrix into Sub Blocks

我有一个矩阵来自 1600x1600 的图像。现在我需要将这个矩阵分配到 4x4 块中。例如:

              00 01 02 03
      IMAGE = 04 05 06 07        BLOCK(i) =   00 01       BLOCK(i+1) = 02 03
              08 09 0A 0B                     04 05                    06 07
              0C 0D 0E 0F
                                BLOCK(i+2) = 08 09        BLOCK(i+3) = 0A 0B
                                             0C 0D                   = 0E 0F

1 ) 首先我不知道图像尺寸,用户打开它。我稍后得到它。我的测试图像 1600x1600.But 块尺寸固定为 4x4。并且图像尺寸为 ,让我们同意现在可以用 4 分割...

2 ) 不知道要多少块。

3 ) 稍后我需要访问块的行和列,因为稍后我将对块执行数学运算...例如,块 (n)[x,y] 与块的异或运算(n+1) [x,y]。 所以这个减速部分,这部分程序非常非常重要。

我将这部分程序卡住了 2 周,我无法继续。请帮助我。 İt 看起来很简单的代码但是......

我的结构是这样的,开始部分

 private void Form1_Load(object sender, EventArgs e)
  {
    Bitmap bmp = new Bitmap("c:\yavanna.jpg");
    pictureBox1.Image = Image.FromFile("c:\yavanna.jpg");

    int width = bmp.Width;
    int height = bmp.Height;
    Color p;
    int[,] alpha_map_int = new int[width, height];
    int[,] red_map_int = new int[width, height];
    int[,] green_map_int = new int[width, height];
    int[,] blue_map_int = new int[width, height];
    int[,] grayscale_map_int = new int[width, height];

    string[,] gray_scale_map = new string[width, height];

    string temp_hexValue;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            //get pixel value
            p = bmp.GetPixel(x, y);

            //extract pixel component ARGB
            int a = p.A;
            alpha_map_int[x, y] = a;

            int r = p.R;
            red_map_int[x, y] = r;

            int g = p.G;
            green_map_int[x, y] = g;

            int b = p.B;
            blue_map_int[x, y] = b;

            //convert to gryscale
            double grayscale = 0.2126 * red_map_int[x,y] + 0.7152 * green_map_int[x, y] + 0.0722 * blue_map_int[x, y];
            grayscale_map_int[x, y] = Convert.ToInt32(grayscale);
            temp_hexValue = Convert.ToString(grayscale_map_int[x, y]);
            gray_scale_map[x, y] = temp_hexValue;
      }
    }

尝试以下操作:

       const string FILENAME = @"c:\temp\test.jpg";
        public Form1()
        {
            InitializeComponent();

            Bitmap image = new Bitmap(FILENAME);


            int height = (int)image.Height ;
            int width = (int)image.Width;


            List<List<List<Color>>> bytes = new List<List<List<Color>>>();
            List<List<List<Int32>>> grayscale_map_int = new List<List<List<Int32>>>();


            for (int row = 0; row < height; row += 4)
            {
                for (int col = 0; col < width; col += 4)
                {
                    bytes.Add(new List<List<Color>>()  { 
                         new List<Color>() { image.GetPixel(col, row), image.GetPixel(col + 1, row), image.GetPixel(col + 2, row), image.GetPixel(col + 3, row)} , 
                         new List<Color>() { image.GetPixel(col, row + 1), image.GetPixel(col + 1, row + 1), image.GetPixel(col + 2, row + 1), image.GetPixel(col + 3, row + 1)} , 
                         new List<Color>() { image.GetPixel(col, row + 2), image.GetPixel(col + 1, row + 2), image.GetPixel(col + 2, row + 2), image.GetPixel(col + 3, row + 2)} , 
                         new List<Color>() { image.GetPixel(col, row + 3), image.GetPixel(col + 1, row + 3), image.GetPixel(col + 2, row + 3), image.GetPixel(col + 3, row + 3)} , 
                    });

                    grayscale_map_int.Add(new List<List<Int32>>()  { 
                         new List<Int32>() { GetGrayScale(image.GetPixel(col, row)), GetGrayScale(image.GetPixel(col + 1, row)), GetGrayScale(image.GetPixel(col + 2, row)), GetGrayScale(image.GetPixel(col + 3, row))} , 
                         new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 1)), GetGrayScale(image.GetPixel(col + 1, row + 1)), GetGrayScale(image.GetPixel(col + 2, row + 1)), GetGrayScale(image.GetPixel(col + 3, row + 1))} , 
                         new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 2)), GetGrayScale(image.GetPixel(col + 1, row + 2)), GetGrayScale(image.GetPixel(col + 2, row + 2)), GetGrayScale(image.GetPixel(col + 3, row + 2))} , 
                         new List<Int32>() { GetGrayScale(image.GetPixel(col, row + 3)), GetGrayScale(image.GetPixel(col + 1, row + 3)), GetGrayScale(image.GetPixel(col + 2, row + 3)), GetGrayScale(image.GetPixel(col + 3, row + 3))} , 
                    });

                }
            } 


        }
        public Int32 GetGrayScale(Color color)
        {
            return Convert.ToInt32(0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B);
        }

这是 jdweng 答案的一个版本,它生成 4x4 数组并处理不除以 4 的源数组。您可以看到他发布简化示例的原因。任何更大的,都值得使用两个循环来填充 4x4 数组。

'image'是输入,'bytes4x4'是输出。

List<List<List<byte>>> bytes4x4 = new List<List<List<byte>>>();

for (int row = 0; row<length-3 ; row += 4)
{
    for (int col = 0; col<width-3; col += 4)
    {
        bytes4x4.Add(new List<List<byte>>()  { 
            new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]}, 
            new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] }, 
            new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },
            new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }
    });
}

这声明并填充了 'bytes4x4',这是一长串二维块。像这样访问一个块:

var block100 = bytes4x4[100];

并用它来获得一个像素:

var block100pixelrow1col3 = block100[1][3];

var block100pixelrow1col3 = bytes4x4[100][1][3];

请注意,这些索引都是从零开始的,因此块中没有元素 [4]。

现在想想,你可能追求的是二维块的二维数组。如果是这样,代码将如下所示:

var bytes4x4 = new List<List<List<List<byte>>>>();

for (int row = 0; row<length-3 ; row += 4)
{
    var row = new List<List<List<byte>>>();
    bytes4x4.Add(row);
    for (int col = 0; col<width-3; col += 4)
    {
        row.Add(new List<List<byte>>()  { 
            new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]}, 
            new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] }, 
            new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },
            new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }
    });
}

然后你可以像这样访问向下 14 行和 23 列的块:

var block14by23 = bytes4x4[14][23];