将二维数组分成 8x8 块 c#
Divide 2d array into 8x8 blocks c#
我一直在网上冲浪,但没有找到任何好的答案。
所以我有这段代码,它应该将我的图像(它总是 512x512)裁剪成 4096 8x8 blocks.So 非常好。
private List<int[,]> PictureDivide(Bitmap Image0)
{
int[,] PartPic;
List<int[,]> MacroBlocks = new List<int[,]>();
HeightDivisions = Image0.Height / 8;
WidthDivisions = Image0.Width / 8;
for (int a = 0; a < Image0.Height; a = a + 8)
{
for (int b = 0; b < Image0.Width; b = b + 8)
{
PartPic = new int[8, 8];
for (int x = b, sx = 0; x < (b + 8); x++, sx++)
{
for (int y = a, sy = 0; y < (a + 8); y++, sy++)
{
PartPic[sx, sy] = Image0.GetPixel(x, y).R;
}
}
MacroBlocks.Add(PartPic);
}
}
return MacroBlocks;
}
但是当我重新绘制这张没有任何变化的图像时,它就坏了,就像我错过了一些像素一样。我的代码有问题吗,或者如果你们有更好的解决方案,我将不胜感激。
编辑:添加示例:前后对比。也许在我重建图像的方式上做错了什么?
看起来像反转的 x 和 y 试试这个:PartPic[sy, sx] = Image0.GetPixel(x, y).R;
问题结束的答案
我一直在网上冲浪,但没有找到任何好的答案。 所以我有这段代码,它应该将我的图像(它总是 512x512)裁剪成 4096 8x8 blocks.So 非常好。
private List<int[,]> PictureDivide(Bitmap Image0)
{
int[,] PartPic;
List<int[,]> MacroBlocks = new List<int[,]>();
HeightDivisions = Image0.Height / 8;
WidthDivisions = Image0.Width / 8;
for (int a = 0; a < Image0.Height; a = a + 8)
{
for (int b = 0; b < Image0.Width; b = b + 8)
{
PartPic = new int[8, 8];
for (int x = b, sx = 0; x < (b + 8); x++, sx++)
{
for (int y = a, sy = 0; y < (a + 8); y++, sy++)
{
PartPic[sx, sy] = Image0.GetPixel(x, y).R;
}
}
MacroBlocks.Add(PartPic);
}
}
return MacroBlocks;
}
但是当我重新绘制这张没有任何变化的图像时,它就坏了,就像我错过了一些像素一样。我的代码有问题吗,或者如果你们有更好的解决方案,我将不胜感激。
编辑:添加示例:前后对比。也许在我重建图像的方式上做错了什么?
看起来像反转的 x 和 y 试试这个:PartPic[sy, sx] = Image0.GetPixel(x, y).R;
问题结束的答案