在 c# 中使用 Emgu 为图像中的每个像素应用 ArcCos 的最快方法是什么
What is the fastest way to apply ArcCos for each pixel in an image using Emgu in c#
早上好,
我正在使用 Emgu 对图像进行大量操作,例如 Pow、Add、Sub、Mul。
这些操作在两个操作的图像之间逐个元素地工作。
但是,我看到Emgu库中没有包含cos、acos、sin和asin,我需要最快的方法来进行acos操作。
尽管如此,我已经有以下方法可以做到这一点,但我不知道它是否是最快的。
// The original image
Image<Bgr, float> image = new Image<Bgr, float>(@"C:\image.jpg");
// Get the image width and height
int imageWidth = image.Width;
int imageHeight = image.Height;
// The ArcCos operated image
Image<Bgr, float> imageAcos = new Image<Bgr, float>(imageWidth, imageHeight);
// Start operating the image
for (int y = 0; y < imageHeight; y++)
{
for(int x = 0; x < imageWidth; x++)
{
// The Blue frame
imageAcos.Data[y, x, 0] = (float) Math.Acos((double) image.Data[y, x, 0]);
// The Green frame
imageAcos.Data[y, x, 1] = (float) Math.Acos((double) image.Data[y, x, 1]);
// The Red frame
imageAcos.Data[y, x, 2] = (float) Math.Acos((double) image.Data[y, x, 2]);
}
}
使用 Image<,>
我认为这是在不使用不安全代码和指针的情况下所能达到的最快速度。快速加速是 运行 并行的外部循环,如下所示:
Parallel.For(0, imageHeight, y =>
{
for (int x = 0; x < imageWidth; x++)
{
// The Blue frame
imageAcos.Data[y, x, 0] = Method(image.Data[y, x, 0]);
// The Green frame
imageAcos.Data[y, x, 1] = Method(image.Data[y, x, 1]);
// The Red frame
imageAcos.Data[y, x, 2] = Method(image.Data[y, x, 2]);
}
});
这是否会导致加速取决于图像大小,因此请务必使用您的图像进行测试。它将利用您可能不需要的所有 CPU 个内核。
一种simpler/more紧凑的方法是使用内置的 Convert 方法。
Image<Bgr, float> imageAcos = image.Convert(p => (float)Math.Acos((double)p));
这不能像 for 循环那样并行化,但应该与您当前的实现一样快。
顺便说一下,我很确定你的数据 [] 中的 x 和 y 顺序错误。
早上好,
我正在使用 Emgu 对图像进行大量操作,例如 Pow、Add、Sub、Mul。 这些操作在两个操作的图像之间逐个元素地工作。 但是,我看到Emgu库中没有包含cos、acos、sin和asin,我需要最快的方法来进行acos操作。
尽管如此,我已经有以下方法可以做到这一点,但我不知道它是否是最快的。
// The original image
Image<Bgr, float> image = new Image<Bgr, float>(@"C:\image.jpg");
// Get the image width and height
int imageWidth = image.Width;
int imageHeight = image.Height;
// The ArcCos operated image
Image<Bgr, float> imageAcos = new Image<Bgr, float>(imageWidth, imageHeight);
// Start operating the image
for (int y = 0; y < imageHeight; y++)
{
for(int x = 0; x < imageWidth; x++)
{
// The Blue frame
imageAcos.Data[y, x, 0] = (float) Math.Acos((double) image.Data[y, x, 0]);
// The Green frame
imageAcos.Data[y, x, 1] = (float) Math.Acos((double) image.Data[y, x, 1]);
// The Red frame
imageAcos.Data[y, x, 2] = (float) Math.Acos((double) image.Data[y, x, 2]);
}
}
使用 Image<,>
我认为这是在不使用不安全代码和指针的情况下所能达到的最快速度。快速加速是 运行 并行的外部循环,如下所示:
Parallel.For(0, imageHeight, y =>
{
for (int x = 0; x < imageWidth; x++)
{
// The Blue frame
imageAcos.Data[y, x, 0] = Method(image.Data[y, x, 0]);
// The Green frame
imageAcos.Data[y, x, 1] = Method(image.Data[y, x, 1]);
// The Red frame
imageAcos.Data[y, x, 2] = Method(image.Data[y, x, 2]);
}
});
这是否会导致加速取决于图像大小,因此请务必使用您的图像进行测试。它将利用您可能不需要的所有 CPU 个内核。
一种simpler/more紧凑的方法是使用内置的 Convert 方法。
Image<Bgr, float> imageAcos = image.Convert(p => (float)Math.Acos((double)p));
这不能像 for 循环那样并行化,但应该与您当前的实现一样快。
顺便说一下,我很确定你的数据 [] 中的 x 和 y 顺序错误。