【问题标题】:How to use pointer to process image with c# and emgucv?c#和emgucv如何使用指针处理图像?
【发布时间】:2014-05-06 20:42:59
【问题描述】:

我想用指针处理图像以使其更快。起初,我这样做

unsafe
    {
        double a = 288 / 55;
        double b = -215 * 288 / 55;
        MIplImage ss = diff.MIplImage;
        for (int i = 0; i < ss.height; i++)
        {
            IntPtr ptr = ss.imageData + i * ss.widthStep;
            for (int j = 0; j < ss.width; j++)
            {
                if (a * j - i + b > 0)
                    ((byte*)(ptr))[j] = 0;
            }
        }
    }

它表现良好(0.5ms,4ms 没有指针)。

然后我发现托管对象应该是固定的,以防止重定位。所以我想应该是这样的

Image<Gray, byte> diff = new Image<Gray, byte>(frame.Width,frame.Height);
fixed (void* p_temp=diff.Ptr.ToPointer()){}

fixed (byte* p_temp=(byte*)temp.Ptr){}

但这是错误的。那么如何修复图像?以及如何在 emgucv 中使用指针?我真的很困惑。谢谢!

unsafe
        {
            var data = diff.Data;
            int stride = diff.MIplImage.widthStep;
            byte* p;
            fixed (byte* pData = data)
            {
                for (int i = 0; i < diff.Height; i++)
                {
                    p = pData + i * stride;
                    for (int j = 0; j < diff.Width; j++)
                    {
                        if (a * j - i + b > 0)
                            *(p + j) = (byte)0;
                    }

                }
            }
        }

【问题讨论】:

    标签: c# emgucv


    【解决方案1】:

    我刚刚做了一个快速演示(不是生产质量代码),为您提供一个关于不安全 emgu 使用的开始工作示例。

            Image<Gray, Byte> img = new Image<Gray, byte>(510, 510);            
            // Fill image with random values
            img.SetRandUniform(new MCvScalar(), new MCvScalar(255));
            // Show Image
            ImageViewer.Show(img);
            var data = img.Data;
            int stride = img.MIplImage.widthStep;
            fixed (byte* pData = data)
            {
                for (int i = 0; i < 255 * stride; i++)
                    *(pData + i) = (byte)(i % stride);
            }
            // Show Image Again
            ImageViewer.Show(img);
    

    首先我用随机噪声填充图像,然后用渐变填充一些行:-)

    【讨论】:

    • 如果您尝试我的代码并且它有效,您可以将其标记为答案:-)
    • 谢谢!但是还有一个问题。我修改了代码并添加到描述中。请看一下。它花费了大约45ms,而之前是0.5ms。有什么办法解决吗?跨度>
    • 您在 for 循环中使用了 Height 和 Width 属性,这在幕后是一个 GDI 调用。看看我在一个与你的症状相似的问题上给出的答案。 stackoverflow.com/questions/5101986/…
    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 2014-06-05
    • 2021-08-16
    • 1970-01-01
    • 2021-10-17
    • 2016-03-14
    • 2015-02-05
    • 2012-01-02
    相关资源
    最近更新 更多