在 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/