在python中混合两个图像以不降低强度

2024-06-25 06:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在第234页的^{}中,有一个示例将解决最小二乘问题,以找到导致所有625个像素的照明度为1的灯功率

当我试图复制下一页显示的绘图时,所有灯光的像素强度如下所示:

Combined pixel intensity of all the lamps

我无法达到如上所示的结果

然而,由于各个灯而产生的各个像素强度可以像这样容易地可视化

[Lamp13Lamp2

当我试图想象所有的灯时,问题来了

如果我尝试添加灯的单个像素值,那么我会得到这样的图片

All lamps combined

这与书中所示完全不同。我必须以某种方式混合单个图像,而不是降低因像素较暗而导致的强度。如果有人能给我一些如何将所有图像混合成一个图像的想法,我将不胜感激

如果有人对init感兴趣,可以在下面找到代码

代码

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

Tags: ofthetoin图像fornpones
1条回答
网友
1楼 · 发布于 2024-06-25 06:42:02

我想书中的结果有点夸张。但其实很简单

矩阵A只需与灯功率阵列相乘,假设所有灯功率都设置为最高,我们得到房间照明不最佳的第一张图片

然后,同样的矩阵需要与我们在解决最小二乘问题后得到的灯功率阵列相乘,以可视化最佳照明房间

代码

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

我们从最小二乘问题中得到的解决方案是最大照明面积的每个单独的灯功率

解决方案

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]

我们有两个不同的情节,比如

房间照明,所有电源设置为1

Room intensity plot when all the lamp powers set to one

房间被解决最小二乘问题后得到的能量照亮Room intensity plot when all the lamp powers set to least-squares result

您可以清楚地看到,边缘上的灯亮着,使整个房间的亮度达到最大

希望我的想法是正确的。如果我错了,请纠正我

相关问题 更多 >