C# 将矩阵划分为子块

我有一个来自 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.但块尺寸固定为 4x4。并且图像尺寸是 ,让我们同意现在可以用 4 分...


2 ) 我不知道会有多少块。


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


我把这部分程序坚持了 2 周,我无法继续。请帮助我。它看起来很简单的代码但是......


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

 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;

      }

    }


慕码人2483693
浏览 238回答 2
2回答

牧羊人nacy

这是 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){&nbsp; &nbsp; for (int col = 0; col<width-3; col += 4)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; bytes4x4.Add(new List<List<byte>>()&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }&nbsp; &nbsp; });}这将声明并填充“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){&nbsp; &nbsp; var row = new List<List<List<byte>>>();&nbsp; &nbsp; bytes4x4.Add(row);&nbsp; &nbsp; for (int col = 0; col<width-3; col += 4)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; row.Add(new List<List<byte>>()&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row, col], image[row, col + 1], image[row, col + 2], image[row, col + 3]},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 1, col], image[row + 1, col + 1], image[row + 1, col + 2], image[row + 1, col + 3] },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 2, col], image[row + 2, col + 1], image[row + 2, col + 2], image[row + 2, col + 3] },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new List<byte>() { image[row + 3, col], image[row + 3, col + 1], image[row + 3, col + 2], image[row + 3, col + 3] }&nbsp; &nbsp; });}然后你可以像这样访问一个向下 14 行和 23 列的块:var block14by23 = bytes4x4[14][23];

MMTTMM

尝试以下:&nbsp; &nbsp; &nbsp; &nbsp;const string FILENAME = @"c:\temp\test.jpg";&nbsp; &nbsp; &nbsp; &nbsp; public Form1()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap image = new Bitmap(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = (int)image.Height ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = (int)image.Width;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<List<List<Color>>> bytes = new List<List<List<Color>>>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<List<List<Int32>>> grayscale_map_int = new List<List<List<Int32>>>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int row = 0; row < height; row += 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < width; col += 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytes.Add(new List<List<Color>>()&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new List<Color>() { image.GetPixel(col, row), image.GetPixel(col + 1, row), image.GetPixel(col + 2, row), image.GetPixel(col + 3, row)} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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)} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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)} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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)} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grayscale_map_int.Add(new List<List<Int32>>()&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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))} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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))} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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))} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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))} ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Int32 GetGrayScale(Color color)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Convert.ToInt32(0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP