首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插值算法的速度,C#和C++协同工作

插值算法的速度,C#和C++协同工作
EN

Stack Overflow用户
提问于 2010-04-30 13:00:28
回答 2查看 2K关注 0票数 2

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

首先,我运行了一些测试,很少对1024x1024x3矩阵进行操作,在C#中使用32‘s,在C++中使用4ms,这是我基本需要的。

不过,插值并不是一个好词,因为我只需要它们来缩小比例。但问题是:在Drawing2D中,它会比Drawing2D方法更快吗?

代码语言:javascript
复制
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内运行,另一些在80 80中运行。有更快的方法吗?

编辑1:

首先,我在这个应用程序中使用了XNA,但是似乎没有办法选择不同的插值方法。当然它运行得很快。

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

编辑2:

以下是我的整个方法:

代码语言:javascript
复制
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毫秒)

EN

回答 2

Stack Overflow用户

发布于 2010-04-30 13:16:32

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2010-04-30 13:05:44

您用GetThumbnailImage方法尝试过吗?

为了进一步扩展亚当·罗宾逊的评论:我希望您使用秒表班对此进行计时

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2744628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档