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:
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
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
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])




