Attempt to extract single-colored semi-transparent overlay from multi-colored background

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?".
Post Reply
coolperez8
Posts: 27
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Attempt to extract single-colored semi-transparent overlay from multi-colored background

Post by coolperez8 »

So, I have 2 nearly identical images, except one has a semi-transparent overlay with a known color. Let's say Image A has the overlay, Image B is Image A without the overlay, and Image C is the overlay itself, which is the result I am trying to achieve.
Image A:
Image
Image B:
Image
Image C:
Image

What I would personally do if Image B were a single color background is this:

Code: Select all

convert 'IMGA.png' -level-colors "$col,#00FF00" \
        -alpha copy +level-colors '#00FF00,#00FF00' \
		'IMGC.png'
The only thing is that image A is a multi color background, so what I need to do is operate this command on image A for every color in image B, substituting "$col" with the corresponding color in image B. I know I could also use -compose difference, but with the image I'm trying to operate on, due to the differences in colors being different, it didn't seem to work right, since the overlay was pink and was placed on top of red and white.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Attempt to extract single-colored semi-transparent overlay from multi-colored background

Post by fmw42 »

try this:

Code: Select all

convert dtMQHtn.png OBN2242.png +swap -compose divide -composite -fuzz 30% -channel rgba -fill none +opaque "#74FF73" tmp.png
What is your IM version and platform? Please always provide that information when asking questions, since syntax may vary.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Attempt to extract single-colored semi-transparent overlay from multi-colored background

Post by snibgo »

You have images A, B and C.

You know that some process has made A from B and C. You want the inverse process, to make C from A and B.

To find the inverse process, we need to know the forwards process. It seems to be "-compose Over -composite". (Not, as you said, "overlay".)

We assume that A and B are opaque. We assume that C contains transparency, but we don't know what it is. Nor do we know the colours.

There is no unique solution. There is virtually an infinite number of possible images C that would give that result. One of these is simply A itself.

Let's add another constraint. Let's suppose that C has a constant colour sRGB(0,100%,0), #0f0, but varying alpha. Then the task is to find alpha at each pixel. Windows BAT script, IM v6:

Code: Select all

%IM%convert ^
  exA.png ^
  -fill White -colorize 100 ^
  wht.png

%IM%convert ^
  ( exA.png exB.png -compose MinusSrc -composite ) ^
  ( wht.png exB.png -compose MinusSrc -composite ) ^
  -compose DivideSrc -composite ^
  -channel G -separate +channel ^
  alph.png

%IM%convert ^
  exA.png ^
  -fill #0f0 -colorize 100 ^
  alph.png ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  fndC.png
fndC.png is the desired result:
Image
The three convert commands could be combined into one, of course.

EDIT: We can (and should) check the result.

Code: Select all

%IM%convert exB.png fndC.png -compose Over -composite calcA.png

%IM%compare -metric RMSE calcA.png exA.png NULL:
The RMSE is 0.00149872, in a range 0.0 to 1.0. It is virtually perfect.
snibgo's IM pages: im.snibgo.com
Post Reply