Blending images

### 28.1.10 Blending images

The interp command interpolates two images linearly in the XYZ color space.

- interp takes three arguments:
  - img1, an image object with dimensions w1× h1 and color depth d1.
  - img2, an image object with dimensions w2× h2 and color depth d2.
  - t, a real number in [0,1].
- interp(img1,img2,t) returns the image with dimensions w× h and color depth d, obtained by linear interpolation between img1 and img2. The width an height of the resulting image are computed by solving the system of equations

  w h=(1−t)w1 h1+t w2 h2, ln |
  w
  h
  | =(1−t)ln |
  w1
  h1
  | +tln |
  w2
  h2
  | .

  The resulting color depth d is the smallest one that is able to fit data from each image. For example, if img1 is a grayscale image with alpha channel (d1=2) and img2 is a RGB image (d2=3) with three channels and no alpha channel, then the resulting depth will be RGBA, i.e. d=4 (three color channels with alpha channel).

  To interpolate pixel data, interp first resizes both images to w× h. The RGB values of individual pixels are then computed by a perceptually uniform linear interpolation between the corresponding values in img1 and img2 (see Section 19.1.3).

#### Example

To begin, load two images:

img1:=image("/home/luka/Pictures/xcas-logo.png")

an image of size 377×353 (RGB) |  |  |  |  |  |  |  |  |  |

img2:=image("/usr/local/share/giac/examples/Exemples/demo/terre.jpg")

an image of size 512×256 (RGB) |  |  |  |  |  |  |  |  |  |

display(img1); display(img2,400)

Now blend the two images together with t=0.6:

img:=interp(img1,img2,0.6)

an image of size 474×277 (RGB) |  |  |  |  |  |  |  |  |  |

display(img)