subtract only the luminance of two images?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
karlKrach
Posts: 5
Joined: 2020-02-19T15:20:20-07:00
Authentication code: 1152

subtract only the luminance of two images?

Post by karlKrach »

hi,
i'm currently experimenting with subtraction of two images (via evaluate and compose), but that also often inverts the colors, as it processes each rgb channel individually. what i would like to do instead, is to subtract luminance but just copy (or average) the color information – how would i do this?

to my understanding i would first need to convert these images to an hsl colorspace and then do separate calculations per hsl-channel: eg.
lightness: subtract
hue: mean
saturation: mean
only, how do i set this up - ideally in one line?

any help is appreciated a lot - thanks in advance!
k

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: subtract only the luminance of two images?

Post by fmw42 »

Please always provide your Imagemagick version and platform, since syntax varies. Also if you can provide your input images to some free hosting service and put the URLs here.

Please outline in more detail.

You probably want to use -compose difference rather than subtract or minus so as to get both polarities of the different.

Which image should provide the hue and saturation mean values?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: subtract only the luminance of two images?

Post by snibgo »

Subtracting one luminance from another will result in about half the values being negative. Is that what you really want? How about taking the difference, which is the absolute value of one minus the other.

The hue channels can be averaged, but does that mean anything? I think averaging the a and b channels of Lab would be more meaningful.

What colorspace are your inputs? I'll assume sRGB. Windows BAT syntax:

Code: Select all

magick ^
  in1.png in2.png ^
  -colorspace Lab ^
  -separate ^
  ( -clone 0,3 ^
    -compose Difference -composite ) ^
  ( -clone 1,4 ^
    -evaluate-sequence Mean ) ^
  ( -clone 2,5 ^
    -evaluate-sequence Mean ) ^
  -delete 0-5 ^
  -combine ^
  -set colorspace Lab ^
  -colorspace sRGB ^
  out.png
snibgo's IM pages: im.snibgo.com

karlKrach
Posts: 5
Joined: 2020-02-19T15:20:20-07:00
Authentication code: 1152

Re: subtract only the luminance of two images?

Post by karlKrach »

Hi again,
thanks for getting back to me! … wow, already a second post… so:

ImageMagick version is 7.0.9-7, running on macOS (bash / zsh) – unfortunately i have no idea of windows BAT…
My input images are sRGB.

Example images can be found here: https://imgur.com/a/dvFRPWe
Top is the base image (the one to subtract from), second the image to subtract, third my current result with inverted colors – note the green becoming purple.

The line I currently use to do this is:
convert image1.png image2.png -evaluate-sequence subtract -alpha off result.png

As for difference: In my case that actually does not matter as my first image is always brighter than my second one (see example images). I double checked this right now and the visual outputs of difference and subtract are indeed exactly the same in my case.

Regarding LAB color or HSL: After reading up on what LAB colour is, yeah, I probably want LAB colour. ; )

Again, thank you so much for helping me here!
k

karlKrach
Posts: 5
Joined: 2020-02-19T15:20:20-07:00
Authentication code: 1152

Re: subtract only the luminance of two images?

Post by karlKrach »

update: thanks to snibgo's BAT i think i got this to work!
i still need to test some more as the results are somewhat different of what i expected (no surprise), but i think it does what i want and i can tinker with this…

just for reference snibgo's BAT above in bash would be (i think):

Code: Select all

magick image1.png image2.png -colorspace Lab -separate \
  \( -clone 0,3 -compose Difference -composite \) \
  \( -clone 1,4 -evaluate-sequence Mean \) \
  \( -clone 2,5 -evaluate-sequence Mean \) \
  -delete 0-5 -combine -set colorspace Lab -colorspace sRGB result.png
again, thank you so much for your help!
k

Locked