[英]Fastest wasy to copy a 3 dimensional byte array to replace PART of another 3 dimensional byte array
我有一個winform桌面C#應用程序。
我的圖像大小為720x576。
該圖像由3維字節數組表示:720x576x3。
x3代表紅色,綠色和藍色的3個顏色通道。
這個圖像在邏輯上(為了我的目的)划分為4個區域,如下所示:
假設我有一個用於Region 4的新字節數組,我希望更新這個'Base'數組。
我可以這樣做:
for (Int16 camIndex = 0; camIndex < 4; camIndex++)
{
if (camsChanged[camIndex])
{
Rectangle roi = Rectangle.Empty;
switch (camIndex)
{
case 0:
roi = new Rectangle(0, 0, 360, 288);
break;
case 1:
roi = new Rectangle(360, 0, 360, 288);
break;
case 2:
roi = new Rectangle(0, 288, 360, 288);
break;
case 3:
roi = new Rectangle(360, 288, 360, 288);
break;
}
for (int x = roi.X; x < roi.X + roi.Width; x++)
{
for (int y = roi.Y; y < roi.Y + roi.Height; y++)
{
BaseImage[x, y, 0] = NewBaseImage[x, y, 0]; //signifies Red Channel
BaseImage[x, y, 1] = NewBaseImage[x, y, 1]; //signifies Green Channel
BaseImage[x, y, 2] = NewBaseImage[x, y, 2]; //signifies Yellow Channel
}
}
}
}
但有更快的方法嗎? 我查看了Buffer.BlockCopy和Array.Copy,但我看不出它在這種情況下如何幫助我,因為它想要替換整個'Base'數組?
謝謝
Array.Copy消除了對每個索引調用進行邊界檢查的需要(因此運行得更快)。 這里最大的問題是你正在處理一個多維數組。 因為你關心速度,你應該扁平化為單維數組。 這個SO問題包括扁平化和索引( 如何在一維數組中“展平”或“索引”3D數組? )
我個人沒有太多使用BlockCopy的經驗,但Array.Copy比單獨復制每個項目產生更好的性能。
編輯
我想到你可能實際上能夠在多維數組上調用Array.Copy,但你必須傳遞計算的索引值。 不幸的是,你們四個區域並沒有排列成連續的區塊。 看看這個更簡單的(2-d)示例來說明問題:
int[,] a = new int[4,4];
int quadrent = 1;
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
if (x < 2)
{
if (y < 2)
{
quadrent = 1;
}
else
{
quadrent = 2;
}
}
else
{
if (y < 2)
{
quadrent = 3;
}
else
{
quadrent = 4;
}
}
a[x, y] = quadrent;
}
}
int i, j;
/* output each array element's value */
for (i = 0; i < 4; i++)
{
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}", a[i, 0], a[i, 1], a[i, 2], a[i, 3]));
}
//split the line;
Console.WriteLine();
Console.WriteLine();
Array.Copy(a, 0, a, 11, 4);
for (i = 0; i < 4; i++)
{
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}", a[i, 0], a[i, 1], a[i, 2], a[i, 3]));
}
Console.ReadKey();
輸出:
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 1
1, 2, 2, 4
因此,您將無法在整個區域上調用Array.Copy,但您可以在代碼的連續段上調用它。 你有第三維的事實會增加復雜性,但它應該是可能的。 這樣就不需要直接壓扁,但是你仍然需要循環切片。 話雖這么說,你仍然應該看到一個可衡量的速度提升。
感謝您更新問題。 現在我可以使用Buffer.BLockCopy()
來回答。 我已經停留了一段時間,但我得到了很好的工作。 這是概念代碼的證明:
public enum CameraPos
{
TopLeft = 0,
TopRight = 1,
BottomLeft = 2,
BottomRight = 3
}
class Program
{
static void Main(string[] args)
{
const int W=360, H=288;
var BaseImage=new short[2*W, 2*H, 3];
// Initialize sequential numbers so I can debug the results easier.
short num=0;
for (int x=0; x<2*W; x++)
{
for (int y=0; y<2*H; y++)
{
// num = color + 3*( y+576*x )
BaseImage[x, y, 0]=num++;
BaseImage[x, y, 1]=num++;
BaseImage[x, y, 2]=num++;
}
}
var NewImage=new short[2*W, 2*H, 3];
CopySubImage(BaseImage, NewImage, CameraPos.TopRight);
// this copied x=360..719 and y=0..287
}
static void CopySubImage(short[, ,] base_image, short[, ,] new_image, CameraPos cam_index)
{
int W=base_image.GetLength(0)/2;
int H=base_image.GetLength(1)/2;
int x_index=((int)cam_index%2)*W; // either 0 or 360
// int y_index=((int)cam_index/2)*H; // either 0 or 288 (not needed)
// Copy columns with Buffer.BlockCopy. Shown below are the flat array indeces.
//
// | 3*(0+576*0) 3*(0+576*1) .. 3*(0+576*359) | 3*(0+576*360) .. 3*(0+576*719) |
// | 3*(1+576*0) 3*(1+576*1) .. 3*(1+576*359) | 3*(1+576*360) .. 3*(1+576*719) |
// | .. .. | .. .. |
// | 3*(287+576*0) .. 3*(287+576*359) | 3*(287+576*360) .. 3*(287+576*719) |
// + ---------------------------------------- + ---------------------------------- +
// | 3*(288+576*0) .. 3*(288+576*359) | 3*(288+576*360) .. 3*(288+576*719) |
// | .. .. | .. .. |
// | 3*(575+576*0) .. 3*(575+576*359) | 3*(575+576*360) .. 3*(575+576*719) |
for (int x=x_index; x<x_index+W; x++)
{
int k_start=3*(y_index+2*H*x);
//int k_end=3*(x_index+W+720*y); (not needed)
Buffer.BlockCopy(base_image, k_start, new_image, k_start, 3*H*sizeof(short));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.