2

我需要快速实现流行的插值算法。我发现在这种简单算法中的 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 毫秒)

4

2 回答 2

1

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

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

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

于 2010-04-30T13:16:32.790 回答
0

你试过这个GetThumbnailImage方法吗?

进一步扩展Adam Robinson 的评论:我希望您使用Stopwatch 类来计时?

于 2010-04-30T13:05:44.847 回答