如何在 C#/.NET 中从一个位图中减去另一个位图?

发布于 2024-07-11 02:37:29 字数 222 浏览 13 评论 0原文

我有两个位图,由算法的不同变体生成。 我想通过从另一个位图中减去一个位图来创建第三个位图以显示差异。

在 .NET 中如何做到这一点? 我查看了 Graphics 类及其所有选项,包括 ImageAttributes 类,我有一种预感它涉及颜色矩阵或重映射表功能。

有谁有一些示例代码的链接,或者可以为我指出正确的方向吗? 谷歌搜索不会透露太多信息,除非我今天的谷歌搜索失败了。

I have two bitmaps, produced by different variations of an algorithm. I'd like to create a third bitmap by subtracting one from the other to show the differences.

How can this be done in .NET? I've looked over the Graphics class and all its options, including the ImageAttributes class, and I have a hunch it involves the color matrix or remap tables functionality.

Does anyone have a link to some example code, or can point me in the right direction? A google search doesn't reveal much, unless my google-fu is failing me today.

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

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

发布评论

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

评论(5

蓝眼睛不忧郁 2024-07-18 02:37:29

真正的问题是,你想展示什么差异? 如果您只需要对 RGB 颜色值进行操作,我认为最好的选择是扫描两个位图并使用 GetPixel 比较颜色值,然后使用 SetPixel 生成“差异”位图。 也许您只是想减去这些值并将它们用作第三个位图的新颜色值。 或者您可能想计算并使用亮度。 更好的是,如果您有三个用于比较的指标,请将每个指标分配给颜色的 RG 和 B 分量。 我以前曾使用这种方法进行分形着色。

还有其他方法,但使用这种方法时,您的想象力将受到限制。 这可能不是最快的方法,但听起来对于这种情况来说性能并不是必需的。

The real question is, what differences do you want to show? If you just need to operate on RGB color values, the best bet in my opinion is to just scan through both bitmaps and compare the Color values using GetPixel, and use SetPixel to generate your 'difference' bitmap. Perhaps you simply want to subtract the values and use those as the new Color value for the third bitmap. Or perhaps you want to calculate out the luminosity and use that. Even better, if you have three metrics for comparison, assign each one to the R G and B components of the color. I've used this approach for fractal colorization before.

There are other approaches, but with this one you are limited only to your imagination. It may not be the fastest approach, but it does not sound like performance is necessary for this scenario.

成熟的代价 2024-07-18 02:37:29

看看这个项目。 它是安德鲁·基里洛夫(Andrew Kirillov)制造的运动探测器。 他实现了几个过滤器来获取两张图片之间的差异,并使用它来计算运动。 它做得非常好,并且很容易在您自己的应用程序中修改和使用。

http://www.codeproject.com/KB/audio-video/Motion_Detection。 ASPX

Check out this project. It is a motion detector made by Andrew Kirillov. He implements a couple of filters to get the differences between two pictures and uses that to calculate movements. It is really nice done and its easy to modify and use in your own application.

http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx

私野 2024-07-18 02:37:29

这可以通过调用 BitBlt API 函数来完成。 以下是一些示例代码:

http://www.codeproject.com/KB/ GDI-plus/Bitblt_wrapper_class.aspx

该示例使用 SRCCOPY 光栅操作代码; 要获得两个位图之间的差异,您需要使用 SRCPAINT 或其他东西(GOOGLE 应该给出代码列表)。

GetPixel 和 SetPixel(在 Bitmap 类上)慢得令人难以置信。 使用 LockBits 会快得多,但您仍然需要编写自己的代码。

更新:这是一个更好的链接:

http://www.pinvoke.net/default。 aspx/gdi32.BitBlt

并包含所有可能的三元光栅操作(SRCPAINT 或 SRCAND 可能就是您正在寻找的内容。)。

This can be done by PInvoking the BitBlt API function. Here is some sample code:

http://www.codeproject.com/KB/GDI-plus/Bitblt_wrapper_class.aspx

The sample uses the SRCCOPY raster op code; to get the differences between two bitmaps, you'd instead want to use SRCPAINT or something (GOOGLE should give the list of codes).

GetPixel and SetPixel (on the Bitmap class) are unbelievably slow. Using LockBits will be much faster, but you'll still have to write your own code.

Update: this is a better link:

http://www.pinvoke.net/default.aspx/gdi32.BitBlt

and includes all the possible ternary raster operations (SRCPAINT or SRCAND are probably what you're looking for.).

很快妥协 2024-07-18 02:37:29

首先,定义减法;-p 你希望答案是什么样的?

执行此操作的最佳性能方法可能是 LockBits - 它应该比许多 GetPixel 调用快得多,但您需要自己解码字节。 如果它只是 32bpp ARGB 之类的东西,那么很容易,但对于一些更复杂的情况来说就很棘手。

First, define subtract ;-p What do you want the answer to look like?

The most performance way to do this is probably LockBits - it should be much quicker than lots of GetPixel calls, but you'll need to decode the bytes yourself. Easy if it is just something like 32bpp ARGB, but tricky for some more complex cases.

冷情妓 2024-07-18 02:37:29

我在某处读到 Adobe Pixel Bender 中使用的语言受到 Microsoft 的启发曾经做过。 不记得我在哪里读过它。 我的想法是,也许微软的“东西”被包装到了.Net 项目可以使用的东西中。 仅仅减去两个图像就太过分了,但无论如何。

I've read somewhere that the language used in Adobe Pixel Bender is inspired by something that Microsoft once did. Don't remember where I read it. My thinking is that maybe that Microsoft "something" is wrapped into something that a .Net project can use. Overkill for just subtracting two images, but anyway.

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