c# - 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)我需要稍后访问块的行和列,因为我稍后将对块执行数学运算......例如,与 block(n)[x,y] 和 block(n+1) 的 XOR 操作[x,y]。所以这个声明部分,这部分程序是非常非常重要的。
我将程序的这一部分卡住了 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;
}
}
解决方案
尝试以下:
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);
}
推荐阅读
- python - RobotFramework:获取“TypeError:下标泛型不能用于类和实例检查”
- ios - How to update previous view upon dismissing the sheet view in swiftui
- python - In Python, printing a newline when a word is in line
- javascript - Olney Font for Dygraphs on Mobile
- visual-studio-code - How to remove this annoying Push Changes button in vscode?
- excel - How to convert date format values of a Column into only numbers in same Column in excel with VBA
- flutter - 如何在 Dart 中实现 Null 检查器
- django - 如何根据窗口大小调整 django ckeditor 的大小?
- c# - 如何从 v4 打印机驱动程序打开 wpf 应用程序
- java - android RecycleView 如何更快地加载内容?