C#将矩阵划分为子块

问题描述 投票:-2回答:2

我有一个矩阵来自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)我需要稍后访问块的行和coloumb,因为我将在稍后对块执行数学运算...例如,块(n)[x,y]与块(n + 1)的XOR运算[X,Y]。所以这个取消部分,这部分程序非常重要。

我把这部分程序坚持了两个星期我不能再继续了。请帮助我。 İ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;
      }
    }
c# matrix split block divide
2个回答
0
投票

这是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];

0
投票

试试以下:

       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);
        }
© www.soinside.com 2019 - 2024. All rights reserved.