# Compose one image over another

I'm a newbie in image processing using Mathematica.

I'd like to compose an image $A$ over an image $B$ into an image C such that

$\quad C[i,j] = B[i,j]$, if $A[i,j]$ is white

$\quad C[i,j] = A[i,j]$, otherwise

It would work if I could do Min on each pixel:

$\quad C[i,j] = \min(A[i,j],B[i,j])$

but Min does not seem to work component-wise.

Perhaps I could turn white in $A$ into transparency and then compose over $B$. How can I do this?

• Did you check the Image Composition docs? – Öskå Sep 5 '14 at 11:42
• @Öskå, I did but can't seem to achieve what I want. – lhf Sep 5 '14 at 11:44
• You can use ImageApply to apply a function like Min pixel-wise. Or turn the images to normal arrays using ImageData and use MapThread. – Niki Estner Sep 5 '14 at 11:58
• Your comment to @Öskå suggests that you have tried something, which means you can post it here, which will help us help you. – bobthechemist Sep 5 '14 at 12:11
• @m_goldberg You're right. I assumed the first one in my answer – Dr. belisarius Sep 5 '14 at 19:17

The naive method for component-wise Min is

img1 = Import["ExampleData/lena.tif"]
img2 = ColorNegate[img1]


ImageApply[Min /@ Transpose@{##} &, {img1, img2}] // AbsoluteTiming


But it is quite slow. However, one can note that $$\min(A,B) = \frac{A}{2}+\frac{B}{2}-\left|\frac{A}{2}-\frac{B}{2}\right|$$ Therefore, let's try the following

ImageSubtract[ImageAdd[##], ImageDifference[##]] &[
ImageMultiply[img1, 0.5], ImageMultiply[img2, 0.5]] // AbsoluteTiming


It is much faster!

P.S. It is also faster then ColorSeparate/ColorCombine:

ColorCombine[ImageApply[Min, #] & /@
Transpose[ColorSeparate /@ {img1, img2}]] // AbsoluteTiming


(* Two sample images *)
a = Image[RandomReal[1, {100, 100, 3}]];
b = Image[Array[If[#1 < #2, RandomReal[.5, 3], {1, 1, 1}] &, {100, 100}]];

(* Calculate*)

Timing[
mask = Binarize[b, {1., 1., 1.} == # &];