对比使用C# unsafe代码和OpenCV进行图像处理的效率(下)

本文对比了使用C# unsafe代码和OpenCV进行图像处理的效率,并对C# Image类进行了优化,包括4字节对齐、减少浮点运算、查表法加速AddWeighted等。测试结果显示,优化后的C#代码在不同CPU和样本尺寸下与OpenCV的性能差距减小,尤其在Intel CPU上OpenCV表现更优,可能是因为其使用了IPP优化。作者邀请读者提供优化建议并关注后续更新。
摘要由CSDN通过智能技术生成

        经过前面的讨论,我对Image类进行了优化,代码如下:

    //C#灰度图像处理类,作者:wmesci
    //http://http://blog.csdn.net/wmesci
    unsafe class Image :CriticalHandle,  IDisposable
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr LocalAlloc(int flags, int size);

        [DllImport("kernel32.dll")]
        static extern IntPtr LocalFree(IntPtr memBlock);

        [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
        static extern unsafe void CopyMemory(void* dst, void* src, int count);

        [DllImport("ntdll.dll")]
        static extern unsafe void* memset(void* src, byte value, uint size);

        const byte Max = 255;
        const byte Min = 0;

        public Image(int width, int height) 
            : base(IntPtr.Zero)
        {
            if (width <= 0 || height <= 0)
                throw new ArgumentOutOfRangeException();

            Width = width;
            Height = height;
            Stride = (width + 3) & ~3;
            Length = Stride * Height;
            base.SetHandle(LocalAlloc(0x40, Length));

            Pointer = (byte*)handle.ToPointer();
        }

        public Image(int width, int height, byte* data)
            : this(width, height)
        {
            SetData(data);
        }

        public void GetData(void* dst) 
        {
            CopyMemory(dst, Pointer, Length);
        }

        public void SetData(void* src)
        {
            CopyMemory(Pointer, src, Length);
        }

        public readonly int Width;

        public readonly int Height;

        public readonly int Length;

        public readonly int Stride;

        public readonly byte* Pointer;

        public byte this[int x, int y] 
        {
            get
            {
                return *(Pointer + y * Stride + x);
            }
            set
            {
                *(Pointer + y * Stride + x) = value;
            }
        }

        public Image Clone()
        {
            return new Image(Width, Height, Pointer);
        }

        public void Add(Image img)
        {
            Action<int> act = y =>
            {
                byte* p1 = Pointer + y * Stride, p2 = (byte*)img.Pointer + y * img.Stride;
                for (int x = 0; x < Stride; x += 4, p1 += 4, p2 += 4)
                {
                    int d = (int)p1[0] + (int)p2[0];
                    if (d < 0)
                        p1[0] = 0;
                    else if (d > 255)
                        p1[0] = 255;
                    else
                        p1[0] = (byte)d;

                    d = (int)p1[1] + (int)p2[1];
                    if (d < 0)
                        p1[1] = 0;
                    else if (d > 255)
                        p1[1] = 255;
                    else
                        p1[1] = (byte)d;

                    d = (int)p1[2] + (int)p2[2];
                    if (d < 0)
                        p1[2] = 0;
                    else if (d > 255)
                        p1[2] = 255;
                    else
                        p1[2] &#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值