How to subtract one bitmap from another in C # /. NET?

I have two bitmaps created by different variations of the algorithm. I would like to create a third bitmap by subtracting one from the other to show the differences.

How can this be done in .NET? I looked at the Graphics class and all its parameters, including the ImageAttributes class, and I have a hunch that it includes the functionality of a color matrix or redirect table.

Does anyone have a link to some sample code or can point me in the right direction? A google search doesn't show much unless my google-fu can get me today.

+6
c # image subtraction
source share
5 answers

The real question is: what differences do you want to show? If you just need to use RGB color values, the best thing, in my opinion, is just to scan through both bitmaps and compare the color values ​​using GetPixel and using SetPixel to create a “difference” bitmap. Perhaps you just want to subtract the values ​​and use them as the new color value for the third bitmap. Or perhaps you want to calculate the luminosity and use it. Even better, if you have three indicators to compare, assign each of them to the RG and B color components. I used this approach for fractal coloring before.

There are other approaches, but with this you are limited only by your imagination. This may not be the fastest approach, but for this scenario, it doesn't look like performance.

+7
source share

Check out this project. This is a motion detector made by Andrew Kirillov. It implements a couple of filters to get the differences between the two pictures and those used to calculate the movements. It is really nice to do and easy to change and use in your application.

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

+3
source share

This can be done using the PInvoking API function BitBlt. Here is a sample code:

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

The example uses the raster code code SRCCOPY; to get the differences between the two bitmaps, instead you want to use SRCPAINT or something (GOOGLE should provide a list of codes).

GetPixel and SetPixel (by bitmap class) are incredibly slow. Using LockBits will be much faster, but you still have to write your own code.

Update: this is the best link:

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

and includes all possible three-dimensional raster operations (SRCPAINT or SRCAND - this is probably what you are looking for.).

+2
source share

Define subtract ;-p first. What do you want the answer to look like?

The most efficient way to do this is probably LockBits - it should be much faster than many GetPixel calls, but you will need to decode the bytes yourself. Easy if it's something like ARb 32bpp, but difficult for some more complex cases.

+1
source share

I read somewhere that the language used in Adobe Pixel Bender is inspired by something that Microsoft once did. I don’t remember where I read it. My thinking is that perhaps Microsoft is “something” wrapped in something that the .Net project can use. Overkill for simply subtracting two images, but anyway.

0
source share

All Articles