python - 使用 numpy 将棕褐色效果应用于 3D 数组

标签 python arrays numpy

在 numpy 中,我希望采用 3d 颜色值数组(2d 表示图像,其中每个像素都是 RGB 颜色)并为其应用棕褐色滤镜。假设一种颜色由 r, g, b 定义,应用棕褐色滤镜后应该返回的颜色是:

sepia_r = .393*r + .769*g + .189&b
sepia_g = .349*r + .686*g + .168*b
sepia_b = .272*r + .534*g + .131*b

使用大型数组(大概是 3 个长向量的 1080x864 数组)实时完成此操作的最快方法是什么?

最佳答案

假设 (height, width, channels) 的标准图像数组组织,您可以直接使用 numpy 矩阵乘法。

from skimage.data import lena  # Color image version of lena. If you don't have skimage, use any image
import matplotlib.pyplot as plt

img = lena().astype(float) / 256.
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(img)

sepia_filter = np.array([[.393, .769, .189],
                         [.349, .686, .168],
                         [.272, .534, .131]])

# here goes the filtering
sepia_img = img.dot(sepia_filter.T)

# Unfortunately your filter lines do not have unit sum, so we need to rescale
sepia_img /= sepia_img.max()

plt.subplot(1, 2, 2)
plt.imshow(sepia_img)
plt.show()

关于python - 使用 numpy 将棕褐色效果应用于 3D 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802725/

相关文章:

Python: "importing"输入参数

java.lang.ArrayIndexOutOfBoundsException ,无法创建对象数组

python - 为什么 numpy.dtype(numpy.float64) 计算结果为 False

具有多维 numpy 数组的 python 数据结构

python - 由另一个多维张量索引多维 torch 张量

python - 如何在 sublime text 2 中配置 python 解释器以使其充当 IDLE python shell

python - 在 TensorFlow 中从头开始训练 Inception

python - 如何使用pymysql将日志上传到mysql

ios - 为什么字典中的 "values"属性不是一个简单的数组,而是一些奇怪的东西?

Java将一个数组放入另一个数组中