Blend two images in python to not lose intensity

Viewed 172

In Introduction to Applied Linear Algebra – Vectors, Matrices, and Least Squares in page 234 there is an example that will solve the least squares problem to find the lamp powers that result in the illumination of 1 for all the 625 pixels.

When I was trying to replicate the plot that was showed in the next pages where the pixel intensity of all the lights are shown like:

Combined pixel intensity of all the lamps

I was not able to achieve the result like shown above.

However, the individual pixel intensities due to the individual lamps can be easily visualized like

[Lamp13 Lamp2

The problem comes when I try to visualize all the lamps.

If i try to add in the individual pixel values of the lamps, then I get a picture like this

All lamps combined

which is nothing like shown in the book. I have to somehow blend individual images not lowering the intensity caused due to the dark pixels. If any one can give me some ideas how to blend all the images into one, i would be grateful.

Code can be found below if any one is interested init

Code

import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid
# number of lamps
n = 10
# x, y positions of lamps and height above floor
lamps = np.array([[4.1 ,20.4, 4],[14.1, 21.3, 3.5],[22.6, 17.1,6], 
                  [5.5 ,12.3, 4.0], [12.2, 9.7, 4.0], [15.3, 13.8, 6],
                  [21.3, 10.5, 5.5], [3.9 ,3.3, 5.0], [13.1, 4.3, 5.0], 
                  [20.3,4.2, 4.5]])

N = 25 # grid size
m = N*N # number of pixels

# construct m x 2 matrix with coordinates of pixel centers
pixels = np.hstack([np.outer(np.arange(0.5,N,1),
np.ones(N)).reshape(m,1), np.outer(np.ones(N),
np.arange(0.5,N,1)).reshape(m,1)])

# The m x n matrix A maps lamp powers to pixel intensities.
# A[i,j] is inversely proportional to the squared distance of
# lamp j to pixel i.
A = np.zeros((m,n))
for i in range(m):
    for j in range(n):
        A[i,j] = 1.0 / (np.linalg.norm(np.hstack([pixels[i,:], 0])- lamps[j,:])**2)

A = (m/np.sum(A)) * A # scale elements of A

# Plot the intensities of each pixel as a grid
# TODO Figure out a way to blend these images.
allLamps = A[:, 0].reshape(N, N)
for i in range(1, n):
    lampI = A[:, i].reshape(N,N)
    allLamps += lampI
    fig = plt.figure(figsize=(4., 4.))
    plt.imshow(lampI)
    plt.scatter(lamps[i, 1], lamps[i, 0])

# Visualize the intensity of all the lamps in single image
fig = plt.figure(figsize=(4., 4.))
plt.imshow(allLamps)
plt.scatter(lamps[:, 1], lamps[:, 0])
# Solve the least squares problem
lampPowers = np.linalg.lstsq(A, np.ones(m))
print("Optimal lamp powers to have 1 value for every pixel is = \n", lampPowers[0])
1 Answers

I guess the result in the book is a bit exaggerated. But actually it is very simple.

The Matrix A just needs to be multiplied with the lamp power array assuming that all the lamp powers are set to the highest, we get the first picture where the room is not optimally lit.

Then the same matrix needs to be multiplied with the lamp power array that we get after solving the least-squares problem to visualize the optimally lit room.

Code

lResultIn1 = np.matmul(A, np.ones(n))
fig = plt.figure(figsize=(4., 4.))
plt.imshow(lResultIn1.reshape(25, 25))
plt.scatter(lamps[:, 1], lamps[:, 0])

# Solve the least squares problem
lampPowers = np.linalg.lstsq(A, np.ones(m))

# Plot the optimally lit room
lResultOptimal = A @ np.asarray(lampPowers[0])
fig = plt.figure(figsize=(4., 4.))
plt.imshow(lResultOptimal.reshape(25, 25))
plt.scatter(lamps[:, 1], lamps[:, 0])

The solution that we get out of least-squares problem is the each individual lamp power for the maximal lit area.

Solution

Optimal lamp powers to have 1 value for every pixel is = 
 [ 1.46211018  0.78797433  2.96641047  0.74358042  0.08317333  0.21263945
  0.21218408  2.05114815  0.90760315  1.47222464]

We get two different plots like

Room lit with all powers set to one

Room intensity plot when all the lamp powers set to one

Room lit with powers we got after solving the least-squares problem Room intensity plot when all the lamp powers set to least-squares result

You can clearly see that the lamps that are on the edges are brightly lit for maximum intensity spread across the room.

Hope I am thinking in the right way. Please correct me if I am wrong.

Related