A common use of color replacement technology is “green scren” background replacement. An actor performs in front of a green wall or a Zoom meeting participant sits in front of a green sheet. A computer algorithm then looks for green pixels and replaces them with pixels from some other image. The result is that the person looks like they are in some other location. (Unless of course, they have a green shirt on, in which case it will look like their disembodied head is floating in the background.)
First up, we need to have an
isGreen
function to identify which pixels are part of the green background. It will be very similar to the
isRed
from the last page, only we want to verify that the green is greater than the red or blue values.
Once you get that working, you can copy and paste it into the program below. It will load the two pictures, with the woman stored as
img1
and the beach as
img2
. It then goes through each pixel in
img1
and asks
isGreen
about it. If the answer is
true
, that pixel gets replaced with the pixel from
img2
that is at the same x, y.