Page 1 of 1

XOR image differences for webcam 'movie'

Posted: 2019-05-29T02:22:15-07:00
by zander
Hi,

I'm curious to create a 'difference' between 2 images that can be applied to turn one into the other. I believe XOR should do this (?) and so "composite -compose difference" should do the trick (?)

In particular I'm curious whether such differences can be JPEG compressed and used to update a webcam server a bit more efficiently than just sending regular JPEGs - kind of like a rudimentary MPEG perhaps.

It seems to get almost there, but leaves a 'halo' around image elements which spoils the effect. Also some 'ghost' effects. I tried creating these simple example PNGs

Image Image1

Image Image2 - just moved down-right a little

Image difference (XOR)

Image Image1 XOR difference -> should be Image 2 ? But get artifacts.

Commands used -

Code: Select all

composite 1.png 2.png -compose difference 3.png
composite 1.png 3.png -compose difference 4.png
I've tried using -quality 100 in case that helps too. Any suggestions appreciated :)

If I've got the wrong end of the stick somewhere, just let me know too :) Cheers!

Re: XOR image differences for webcam 'movie'

Posted: 2019-05-29T04:54:43-07:00
by snibgo
"-compose" is a setting used by the "-composite" operation. The setting must come first. If there is no operation, the setting has no effect. So your commands are wrong.

"-compose Xor -composite" is different to "-compose Difference -composite".

Despite the name, "-compose Xor" does not simply apply the mathematical XOR operator to the channels, so the operation is not reversible in the way that the mathematical XOR is. See http://www.imagemagick.org/Usage/compose/#xor

Re: XOR image differences for webcam 'movie'

Posted: 2019-05-29T21:28:20-07:00
by zander
Hi snibgo,

Thanks for your reply, though I am using '-difference' which I gather is the mathematical XOR rather than the other one you mention.

https://www.imagemagick.org/Usage/compose/#set_theory

I'm using the 'composite' command line on Raspberry Pi, which presumably fires off -composite as you mention.

I tried changing the order of commands as you suggested, but came out with the exact same results

I've just found this old post, which I'll read through about XOR - https://www.imagemagick.org/discourse-s ... hp?t=19995

Cheers

Alex

Re: XOR image differences for webcam 'movie'

Posted: 2019-05-30T00:00:09-07:00
by snibgo
If the two images are black and white only, then "-compose Difference -composite" gives the same as a mathematical XOR. But "Difference" is actually the absolute value of one image minus the other, so for pixel values that are neither 0 nor 100%, the "Difference" is different to XOR. Your inputs have almost all pixel values 0 or 100%, but the anti-aliased pixels are neither.

If you really want XOR, then use "-evaluate-sequence Xor". Using your inputs:

Code: Select all

magick x0wac0.png 17s58o.png -evaluate-sequence Xor xor_x.png
Image

Code: Select all

magick x0wac0.png xor_x.png -evaluate-sequence Xor xor_y.png
Image
As expected, xor_y.png is identical to your second input.

Re: XOR image differences for webcam 'movie'

Posted: 2019-05-30T18:24:16-07:00
by zander
Thanks very much snibgo - that did indeed work!

Unfortunately trying it on a couple of similar photos from a series, generated a surprisingly complex and psychedelic 'difference' which took up more space than the original image to compress as a JPEG. Also the JPEG artifacts at quite high compression turned the final image into a bit of a surrealist painting :) Not quite what I was hoping, but a fun experiment thanks.

Image Photo 1 - compressed with mozjpeg -quality 40 -tune-ms-ssim

Image High Quality XOR of photo 1 and 2

Image XOR JPEG compressed with mozjpeg ms-ssim -quality 40

Image Re-created image for photo 2 using JPEG XOR :)

In the end, it will be simpler and more efficient just to send highly compressed JPEGs from my webcam

Thanks again!