在C#中使用Emgu将图像的每个像素应用ArcCos的最快方法是什么

时间:2019-04-30 04:05:10

标签: c# image-processing emgucv operation cos

早上好

我对带有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]);
    }
}

1 个答案:

答案 0 :(得分:0)

对于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内核。

一种更简单/更紧凑的方法是使用内置的Convert方法。

Image<Bgr, float> imageAcos = image.Convert(p => (float)Math.Acos((double)p));

这不能像for循环那样并行化,但是应该与您当前的实现速度差不多。

顺便说一句,我很确定您在Data []中的x和y顺序错误。