为什么在 C# 中调用 AForge 库函数会导致 AccessViolationException?

发布于 2024-11-19 09:58:23 字数 677 浏览 7 评论 0原文

以下代码会导致访问冲突异常 - 这是为什么?我可以从 unsafe 块内部调用 AForge 过滤器吗?

unsafe
{
    BitmapData bmd = ThresholdedImage.LockBits(boundingR, ImageLockMode.ReadWrite, ThresholdedImage.PixelFormat);
    BitmapData bmda = intersectResult.LockBits(
        new Rectangle(0, 0, intersectResult.Width, intersectResult.Height),  
        ImageLockMode.ReadWrite,  
        intersectResult.PixelFormat);


    intersectResult = intersectFilter.Apply(bmd); //causes exception here
    ImageStatistics st = new ImageStatistics(intersectResult);
    area = st.PixelsCountWithoutBlack;

    intersectResult.UnlockBits(bmda);
    ThresholdedImage.UnlockBits(bmd);
}

The following code causes access violation exception - why is that? Can I call AForge filters from inside unsafe block?

unsafe
{
    BitmapData bmd = ThresholdedImage.LockBits(boundingR, ImageLockMode.ReadWrite, ThresholdedImage.PixelFormat);
    BitmapData bmda = intersectResult.LockBits(
        new Rectangle(0, 0, intersectResult.Width, intersectResult.Height),  
        ImageLockMode.ReadWrite,  
        intersectResult.PixelFormat);


    intersectResult = intersectFilter.Apply(bmd); //causes exception here
    ImageStatistics st = new ImageStatistics(intersectResult);
    area = st.PixelsCountWithoutBlack;

    intersectResult.UnlockBits(bmda);
    ThresholdedImage.UnlockBits(bmd);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2024-11-26 09:58:23

我试图找出这个问题。

你能确认你的boundingR区域确实不代表整个图像吗?

您会收到此异常,因为在 AForge 端的某个时刻,有一个非托管字节副本错误地使用了完整图像的Stride值。

我相信从 BaseInPlacePartialFilter 派生的过滤器可以处理图像区域,但基于 BaseInPlaceFilter 的过滤器很可能会出现问题。

如果可行,您可以在整个图像上应用滤镜,然后仅复制回您感兴趣的区域......

I tried to track down this issue.

Can you confirm that your boundingR region does not indeed represent the whole image ?

You will get this exception because at some point on AForge side there is an unmanaged copy of bytes that mistakenly uses the Stride value of the full image.

I believe filters that derive from BaseInPlacePartialFilter will work with image regions, but filters based on BaseInPlaceFilter will most likely have the issue.

If it's feasible you may apply the filter on the whole image and then copy back just the region you are interested in…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文