Convex Combinations

Learn about convex combinations by implementing an interesting example of morphing.

A convex combination is simply a special case of linear combinations. In particular, for convex linear combinations:

  • Each scalar is greater than 00.
  • The sum must be equal to 11.

For example, for vectors v1\mathbf {v_1} and v2\mathbf {v_2}, a possible convex combination is 0.3v1+0.7v20.3\mathbf {v_1}+0.7\mathbf {v_2}, whereas 1.3v10.3v21.3\mathbf {v_1}-0.3\mathbf {v_2} isn’t a valid convex combination.

Formal definition

Let S={α1,α2,...}S=\{\alpha_1,\alpha_2,...\} be a set of scalars and O={x1,x2,...}O=\{\bold{x_1},\bold{x_2},...\} be a set of objects with addition and scalar multiplication (over SS) defined. The linear combination α1x1+α2x2+...+αnxn\alpha_1\bold{x_1}+\alpha_2\bold{x_2}+...+\alpha_n\bold{x_n} is a convex combination of the objects x1,x2,...,xn\bold{x_1},\bold{x_2},...,\bold{x_n} if αi>=0,  1in\alpha_i >=0, \; 1\le i \le n and α1+α2+...+αn=1\alpha_1+\alpha_2+...+\alpha_n=1.

An interesting example

Using a series of convex combinations, we can transform an imageFlickr-Faces-HQ Dataset (https://github.com/NVlabs/ffhq-dataset) available under Attribution License (https://creativecommons.org/licenses/by/2.0/) into anotherFlickr-Faces-HQ Dataset (https://github.com/NVlabs/ffhq-dataset) available under Attribution License (https://creativecommons.org/licenses/by/2.0/) as shown below:

Explanation

Let’s look at how we’re able to make the transformation of images above:

Step 1: We take two equal-sized images with similar backgrounds to seamlessly transform one image into the other. These images are shown in the figure above.

Step 2: We represent those two images in matrices, say A and B.

Step 3: We create a convex combination IαI_{\alpha} of these matrices, such as Iα=(1α)A+αBI_{\alpha} = (1-\alpha)A + \alpha B. We know that because it’s a convex combination, the α0\alpha \geq 0 and the sum of α+(1α)=1\alpha+(1-\alpha)=1.

Step 4: Initially, α\alpha is set to zero. So, the above equation yields (10)A+(0)B(1-0)A + (0)B, which is equal to A. This can also be seen in the figure below. For α=0\alpha=0, we get the first image corresponding to A.

Step 5: We slowly start to increase α\alpha. Therefore, the components of the first image (which corresponds to matrix A) gradually decrease, whereas the second image’s components (matrix B) increase in equal proportions.

Step 6: We continue to display the result of our transformation until the first image is completely transformed into the second image. That is, α\alpha becomes equal to 1. The figure below shows these transformations of images from α=0\alpha=0 to α=1\alpha=1.

Do it yourself

This section provides the complete code of the example above. We invite you to change the code to your liking. For instance, you can increase or decrease the speed or number of the convex transformation steps. You can even use pictures of your friends and see them transform.

The code below generates convex combinations of AA and BB.

Press + to interact
import numpy as np
import matplotlib.pyplot as plt
# Reading the two images
I = plt.imread('young.png')
J = plt.imread('aged.png')
# Selecting number of intermediate combinations and creating subplots
n = 5
fig, ax = plt.subplots(1, n, figsize=(50, 25))
# Generating convex combinations and plotting them
for i, alpha in zip(np.arange(n), np.linspace(0, 1, n)):
im = (1-alpha)*I + alpha*J
ax[i].imshow(im, cmap='bone');ax[i].axis('off')
ax[i].set_title(str(np.round(alpha, 2)))

💡 Launch the app and then hit the Run button or press shift+enter inside the Jupyter notebook to execute the given code. Please wait till the output Out[1]: appears.

The application below regenerates the animation available at the start of this lesson. You can edit the code if you want to.

Please login to launch live app!