【问题标题】:Apply transformation matrix to pixels in OpenCV image将变换矩阵应用于 OpenCV 图像中的像素
【发布时间】:2014-03-31 15:27:45
【问题描述】:

我想将图像的颜色基础从 RGB 更改为其他颜色。我有一个矩阵 M,我想将其应用于每个像素的 RGB,我们可以将其定义为 xij.

我目前正在迭代 NumPy 图像的每个像素并手动计算 Mxij。我什至无法在行上对其进行矢量化,因为 RGB 是 1x3 而不是 3x1 数组。

有没有更好的方法来做到这一点?也许是 OpenCV 或 NumPy 中的一个函数?

【问题讨论】:

标签: python opencv numpy


【解决方案1】:

不记得执行此操作的规范方法(可能避免转置),但这应该可行:

import numpy as np

M = np.random.random_sample((3, 3))

rgb = np.random.random_sample((5, 4, 3))

slow_result = np.zeros_like(rgb)
for i in range(rgb.shape[0]):
    for j in range(rgb.shape[1]):
        slow_result[i, j, :] = np.dot(M, rgb[i, j, :])

# faster method
rgb_reshaped = rgb.reshape((rgb.shape[0] * rgb.shape[1], rgb.shape[2]))
result = np.dot(M, rgb_reshaped.T).T.reshape(rgb.shape)

print np.allclose(slow_result, result)

如果是标准色彩空间之间的转换,那么您应该使用 Scikit Image:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多