【问题标题】:Speed of interpolation algorithms, C# and C++ working together插值算法、C# 和 C++ 协同工作的速度
【发布时间】:2010-04-30 13:00:28
【问题描述】:

我需要快速实现流行的插值算法。我发现 C# 在如此简单的算法中会比 C++ 慢得多,所以我想编写一些本机代码并在我的 C# GUI 中使用它。

首先,我在1024x1024x3 矩阵上运行了一些测试和少量操作,在 C# 中花费了 32 毫秒,在 C++ 中花费了 4 毫秒,这就是我基本需要的。

然而,插值并不是一个好词,因为我只需要它们来进行缩小。但问题是:会不会比Drawing2D中的C#方法快

Image outputImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
Graphics grPhoto = Graphics.FromImage(outputImage);

grPhoto.InterpolationMode = InterpolationMode.*; //all of them

grPhoto.DrawImage(bmp, new Rectangle(0, 0, destWidth, destHeight),
Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

grPhoto.Dispose();

这些方法中的一些在 20 毫秒内运行,一些在 80 毫秒内运行。有没有办法更快?


编辑 1:

首先,我在此应用程序中使用 XNA,但似乎无法选择不同的插值方法。当然,它的工作速度非常快。

理想的方法是在显卡上实现这些方法。


编辑 2:

这是我的整个方法:

private unsafe Texture2D Scale(GraphicsDevice gd, Texture2D texture, float scale)
    {

        int sourceWidth = texture.Width;
        int sourceHeight = texture.Height;

        int destWidth = (int)(sourceWidth * scale);
        int destHeight = (int)(sourceHeight * scale);

        StopwatchEx sw = new StopwatchEx();
        sw.IntervalStart();
        //convert texture into bitmap
        byte[] textureData = new byte[4 * sourceWidth * sourceHeight];
        texture.GetData<byte>(textureData);

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sourceWidth, sourceHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, sourceWidth, sourceHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly,
                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        IntPtr safePtr = bmpData.Scan0;
        System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
        bmp.UnlockBits(bmpData);


        //output bitmap
        System.Drawing.Image outputImage = new System.Drawing.Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(outputImage);

        grPhoto.InterpolationMode = (System.Drawing.Drawing2D.InterpolationMode)(interpolationMode);
        grPhoto.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode)smoothingMode;
        grPhoto.PixelOffsetMode = (System.Drawing.Drawing2D.PixelOffsetMode)pixelOffsetMode;

        grPhoto.DrawImage((System.Drawing.Image)bmp, new System.Drawing.Rectangle(0, 0, destWidth, destHeight),
            new System.Drawing.Rectangle(0, 0, sourceWidth, sourceHeight), System.Drawing.GraphicsUnit.Pixel);

        grPhoto.Dispose();

        textureData = new byte[4 * sourceWidth * sourceHeight];

        MemoryStream ms = new MemoryStream();
        ((System.Drawing.Bitmap)outputImage).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        ms.Seek(0, SeekOrigin.Begin);
        Texture2D result = Texture2D.FromFile(gd, ms);
        ms.Dispose();
        sw.IntervalStop();
        sw.AppendResults("MEGS.txt");    

        return result;
    }

有趣的是,HighQualityBicubic 比 Bicubic 快得多。 (40 毫秒与 100 毫秒)

【问题讨论】:

  • 您是如何计算这些操作所花费的时间的? C# 中的 GDI 函数只是调用非托管 API;在做同样的事情时,C# 和 C++ 之间不应该存在(通常可察觉的)速度差异。
  • @Adam Robinson Stopwatch 类。自己编写这些方法并调用非托管 dlls 方法给内存中的图像指针返回另一个指针不是更快吗?
  • 不太可能,因为实际工作并未在托管代码中完成。
  • @Adam Robinson 如何使用显卡的电源 - 检查第 1 篇帖子中的更新

标签: c# c++ image scaling interpolation


【解决方案1】:

您是如何在 C# 中实现矩阵的?

在您的情况下,由于 .Net 默认情况下必须在矩阵数组中进行边界检查,因此可能会损失很多速度。在这种情况下,您可以使用不安全的 C# 使您的代码更快(从而消除任何边界检查)。

但如果它已经足够快地使用外部方法,为什么不使用它们呢?

【讨论】:

  • 运行速度不够快。而且我还没有任何外部方法 - 另一个问题。
  • 那么试试不安全的 C# 方法。我已经通过编写自定义图像过滤器(灰度、边缘检测和模糊)来做到这一点,并且性能非常好。
  • 你可以发布一些例子吗?我在这种方法中尝试过不安全,但性能是一样的。
【解决方案2】:

你用GetThumbnailImage方法试过了吗?

进一步扩展Adam Robinson's comment:我希望您使用Stopwatch class 来计时?

【讨论】:

  • 我当然用过秒表。并告诉我在GetThumbnailImage中设置插值方法@
  • GetThumbnailImage 非常有限,根据 MSDN 文档,它不适用于较大的缩略图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多