使用 OpenCV 将 RGB 图像转换为 LMS,反之亦然
Converting a RGB image to LMS, and vice versa, using OpenCV
我正在尝试在 Python 中使用 OpenCV 将图像从 RGB 转换为 LMS,反之亦然。据我了解,我应该使用给定的 3x3 变换矩阵并将其乘以 3x1 RGB/LMS 矩阵。可以找到使用的变换矩阵here.
我在这个网站上探索过 questions,但不幸的是他们使用的是 C++,我还没有精通这种语言,我很难理解他们是如何解决他们的问题的。
到目前为止,这是我的代码:[截至 2019-05-19 已解决]
import numpy as np
import cv2
#Transformation Matrix#
MsRGB = np.zeros((3,3), dtype='float')
MHPE = np.zeros((3,3), dtype='float')
MsRGB = np.array([[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]])
MHPE = np.array([[ 0.4002, 0.7076, -0.0808],
[-0.2263, 1.1653, 0.0457],
[ 0, 0, 0.9182]])
Trgb2lms = MHPE @ MsRGB
Tlms2rgb = np.linalg.inv(Trgb2lms)
imgpath = "(insert file directory here)"
imgIN = cv2.imread(imgpath,cv2.IMREAD_UNCHANGED)
imgINrgb = cv2.cvtColor(imgIN, cv2.COLOR_BGR2RGB)
x,y,z = imgINrgb.shape
imgLMS = np.zeros((x,y,z), dtype='float')
imgReshaped = imgINrgb.transpose(2, 0, 1).reshape(3,-1)
imgLMS = Trgb2lms @ imgReshaped #Convert to LMS
imgOUT = Tlms2rgb @ imgLMS #Convert back to RGB
imgLMS = imgLMS.reshape(z, x, y).transpose(1, 2, 0).astype(np.uint8)
imgOUT = imgOUT.reshape(z, x, y).transpose(1, 2, 0).astype(np.uint8)
imgOUT = cv2.cvtColor(imgOUT, cv2.COLOR_RGB2BGR)
cv2.imshow('Input', imgIN)
cv2.imshow('LMS', imgLMS)
cv2.imshow('Output', imgOUT)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码现在能够使用给定的变换矩阵对给定的 RGB 图像执行线性变换。结果可以找到here.
根据您问题的上下文,存在一些错误:
T
未定义。从您的代码的上下文来看,这应该是 Trgb2lms
,所以我们需要更改它们。
根据我从问题中收集到的信息,您正在对图像中的所有像素应用线性变换。为此,您需要重塑矩阵,使我们有三行,其中每行对应一个像素,然后展开沿列的所有像素。在那种情况下,reshape
方法是不正确的。您不仅需要打乱维度,以便最后一个维度排在第一位,而且您还需要设置 reshape
的最后一个维度,使其为 -1。这意味着我们将自动填充列,使其包含图像中的像素总数。
最后,一旦你做了线性变换,你需要将矩阵重塑回原始图像大小。您可以使用最终的 reshape
调用并使用原始调用中的 x, y
和 z
来推断图像尺寸。请记住,当我们重塑时,通道首先出现,因此我们必须再次排列尺寸。在我们进行转换后,您还需要返回无符号 8 位精度。
也是为了比较,我们运行把这个通过逆变换来确保我们是原版的。
因此:
import numpy as np
import cv2
#Transformation Matrix#
MsRGB = np.zeros((3,3), dtype='float')
MHPE = np.zeros((3,3), dtype='float')
MsRGB = np.array([[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]])
MHPE = np.array([[ 0.4002, 0.7076, -0.0808],
[-0.2263, 1.1653, 0.0457],
[ 0, 0, 0.9182]])
Trgb2lms = MHPE @ MsRGB
# Change
Tlms2rgb = np.linalg.inv(Trgb2lms)
imgpath = "(insert filename here)"
imgIN = cv2.imread(imgpath,cv2.IMREAD_UNCHANGED)
imgINrgb = cv2.cvtColor(imgIN, cv2.COLOR_BGR2RGB)
x,y,z = imgINrgb.shape
imgLMS = np.zeros((x,y,z), dtype='float')
#imgFlatten = imgINrgb.flatten()
# Change
imgReshaped = imgINrgb.transpose(2, 0, 1).reshape(3,-1)
# Change
imgLMS = Trgb2lms @ imgReshaped
imgOUT = Tlms2rgb @ imgLMS
# New
imgLMS = imgLMS.transpose(z, x, y).permute(1, 2, 0).astype(np.uint8)
imgOUT = imgOUT.transpose(z, x, y).permute(1, 2, 0).astype(np.uint8)
我正在尝试在 Python 中使用 OpenCV 将图像从 RGB 转换为 LMS,反之亦然。据我了解,我应该使用给定的 3x3 变换矩阵并将其乘以 3x1 RGB/LMS 矩阵。可以找到使用的变换矩阵here.
我在这个网站上探索过
到目前为止,这是我的代码:[截至 2019-05-19 已解决]
import numpy as np
import cv2
#Transformation Matrix#
MsRGB = np.zeros((3,3), dtype='float')
MHPE = np.zeros((3,3), dtype='float')
MsRGB = np.array([[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]])
MHPE = np.array([[ 0.4002, 0.7076, -0.0808],
[-0.2263, 1.1653, 0.0457],
[ 0, 0, 0.9182]])
Trgb2lms = MHPE @ MsRGB
Tlms2rgb = np.linalg.inv(Trgb2lms)
imgpath = "(insert file directory here)"
imgIN = cv2.imread(imgpath,cv2.IMREAD_UNCHANGED)
imgINrgb = cv2.cvtColor(imgIN, cv2.COLOR_BGR2RGB)
x,y,z = imgINrgb.shape
imgLMS = np.zeros((x,y,z), dtype='float')
imgReshaped = imgINrgb.transpose(2, 0, 1).reshape(3,-1)
imgLMS = Trgb2lms @ imgReshaped #Convert to LMS
imgOUT = Tlms2rgb @ imgLMS #Convert back to RGB
imgLMS = imgLMS.reshape(z, x, y).transpose(1, 2, 0).astype(np.uint8)
imgOUT = imgOUT.reshape(z, x, y).transpose(1, 2, 0).astype(np.uint8)
imgOUT = cv2.cvtColor(imgOUT, cv2.COLOR_RGB2BGR)
cv2.imshow('Input', imgIN)
cv2.imshow('LMS', imgLMS)
cv2.imshow('Output', imgOUT)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码现在能够使用给定的变换矩阵对给定的 RGB 图像执行线性变换。结果可以找到here.
根据您问题的上下文,存在一些错误:
T
未定义。从您的代码的上下文来看,这应该是Trgb2lms
,所以我们需要更改它们。根据我从问题中收集到的信息,您正在对图像中的所有像素应用线性变换。为此,您需要重塑矩阵,使我们有三行,其中每行对应一个像素,然后展开沿列的所有像素。在那种情况下,
reshape
方法是不正确的。您不仅需要打乱维度,以便最后一个维度排在第一位,而且您还需要设置reshape
的最后一个维度,使其为 -1。这意味着我们将自动填充列,使其包含图像中的像素总数。最后,一旦你做了线性变换,你需要将矩阵重塑回原始图像大小。您可以使用最终的
reshape
调用并使用原始调用中的x, y
和z
来推断图像尺寸。请记住,当我们重塑时,通道首先出现,因此我们必须再次排列尺寸。在我们进行转换后,您还需要返回无符号 8 位精度。也是为了比较,我们运行把这个通过逆变换来确保我们是原版的。
因此:
import numpy as np
import cv2
#Transformation Matrix#
MsRGB = np.zeros((3,3), dtype='float')
MHPE = np.zeros((3,3), dtype='float')
MsRGB = np.array([[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]])
MHPE = np.array([[ 0.4002, 0.7076, -0.0808],
[-0.2263, 1.1653, 0.0457],
[ 0, 0, 0.9182]])
Trgb2lms = MHPE @ MsRGB
# Change
Tlms2rgb = np.linalg.inv(Trgb2lms)
imgpath = "(insert filename here)"
imgIN = cv2.imread(imgpath,cv2.IMREAD_UNCHANGED)
imgINrgb = cv2.cvtColor(imgIN, cv2.COLOR_BGR2RGB)
x,y,z = imgINrgb.shape
imgLMS = np.zeros((x,y,z), dtype='float')
#imgFlatten = imgINrgb.flatten()
# Change
imgReshaped = imgINrgb.transpose(2, 0, 1).reshape(3,-1)
# Change
imgLMS = Trgb2lms @ imgReshaped
imgOUT = Tlms2rgb @ imgLMS
# New
imgLMS = imgLMS.transpose(z, x, y).permute(1, 2, 0).astype(np.uint8)
imgOUT = imgOUT.transpose(z, x, y).permute(1, 2, 0).astype(np.uint8)