C#将矩阵划分为子块
我有一个矩阵来自图像1600x1600。现在我需要将这个矩阵分配到4x4块中。例如:C#将矩阵划分为子块,c#,matrix,split,block,divide,C#,Matrix,Split,Block,Divide,我有一个矩阵来自图像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
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
我不知道要走多少个街区
3)我需要稍后访问块的行和列,因为我稍后将对块执行数学运算。。。例如,具有块(n)[x,y]的异或运算具有块(n+1)[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;
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
位图bmp=新位图(“c:\\yavanna.jpg”);
pictureBox1.Image=Image.FromFile(“c:\\yavanna.jpg”);
int width=bmp.width;
int-height=bmp.height;
颜色p;
int[,]alpha_map_int=新int[宽度,高度];
int[,]红色地图\新的int[宽度,高度];
int[,]绿色\地图\新的int[宽度,高度];
int[,]蓝色地图\新的int[宽度,高度];
int[,]灰度\贴图\ int=新int[宽度,高度];
字符串[,]灰度图=新字符串[宽度,高度];
字符串温度值;
对于(int y=0;y
尝试以下操作:
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);
}
const string FILENAME=@“c:\temp\test.jpg”;
公共表格1()
{
初始化组件();
位图图像=新位图(文件名);
int height=(int)image.height;
int-width=(int)image.width;
列表字节=新列表();
List grayscale_map_int=new List();
对于(int行=0;行<高度;行+=4)
{
对于(int col=0;col
以下是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] }
});
}
然后用它得到一个像素:
var block100pixelrow1col3 = block100[1][3];
或
请注意,这些索引都是基于零的,因此块中没有元素[4]
现在我考虑一下,您可能在寻找一个二维块的二维数组。如果是这样,代码将如下所示:
var block100 = bytes4x4[100];
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] }
});
}
var block14by23 = bytes4x4[14][23];
你能告诉我们你得到了什么吗?我有从图像中获取灰度矩阵的所有部分作为字符串。我尝试了“列表块=新列表();”推荐但我没能做到。至少你应该告诉我们输入和输出数据结构是什么样子。字符串真的吗?建议尽快将它们转换为int或byte。-计算块数应该很简单(16k*16k/4),然后在输入阵列上进行双循环,并将数字填入适当的插槽。另外:尽管OP声明它可以被4x4块分割,但我建议不要测试此限制。次要点-这是2x2块,而不是4x4块-但原理很好。标准图像大小
var block14by23 = bytes4x4[14][23];