以下使用RGB颜色255,0,0创建红色图像
import numpy as np
import matplotlib.pyplot as plt
import cv2
width = 5
height = 2
array = np.zeros([height, width, 3], dtype=np.uint8)
array[:,:] = [255, 0, 0] # make it red
print(array)
plt.imshow(array)
plt.show()
输出:[[[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]
[255 0 0]]]
如果将数组转换为LAB空间:
array = cv2.cvtColor(array, cv2.COLOR_BGR2LAB)
print(array)
结果如下:[[[ 82 207 20]
[ 82 207 20]
[ 82 207 20]
[ 82 207 20]
[ 82 207 20]]
[[ 82 207 20]
[ 82 207 20]
[ 82 207 20]
[ 82 207 20]
[ 82 207 20]]]
根据http://colorizer.org/,红色的值应为:lab(53.23, 80.11, 67.22)
为什么opencv产生不同的值?我想念什么吗?有没有可以查找的站点,例如opebcv的实验室色号为红色?谢谢。PS:
一个问题是我使用了COLOR_BGR2LAB而不是COLOR_RGB2LAB(感谢Mark Setchell),但是它仍然无法产生预期的 vector 53.23、80.11、67.22,它产生的结果是:54.4 83.2 78.距离不近但不相同...
import numpy as np
import matplotlib.pyplot as plt
import cv2
width = 5
height = 2
array = np.zeros([height, width, 3], dtype=np.uint8)
array[:,:] = [255, 0, 0] # make it red
print(array)
array = cv2.cvtColor(array, cv2.COLOR_RGB2LAB)
array = array / 2.5
print(array)
最佳答案
您以RGB顺序创建了图像,因此
array = cv2.cvtColor(array, cv2.COLOR_BGR2LAB)
应该:array = cv2.cvtColor(array, cv2.COLOR_RGB2LAB)
如果使用 OpenCV cv2.imshow(array)
和cv2.waitKey()
显示它,您将看到 OpenCV 将其视为蓝色。关于在线转换器和 OpenCV 之间的差异,我只能假设这与对RGB值使用
uint8
引起的舍入误差有关。如果您转换为float
类型,则问题将消失:Lab = cv2.cvtColor(array.astype(np.float32), cv2.COLOR_RGB2LAB)
# Result
array([[[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125]],
[[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125],
[53.240967, 80.09375 , 67.203125]]], dtype=float32)
顺便说一句,我注意到
scikit-image
会在您将其传递给float
时选择自动返回一个uint8
:from skimage import color
import numpy as np
# Make a rather small red image
array = np.full((1, 1, 3), [255,0,0], dtype=np.uint8)
# Convert to Lab with scikit-image
Lab = color.rgb2lab(array)
# Result
array([[[53.24058794, 80.09230823, 67.20275104]]])
只是为了好玩,我们一边检查 ImageMagick 是什么:
magick xc:red -colorspace lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (34891.4,53351.7,17270.9) #884BD068C376 cielab(53.2408,80.0943,67.202)
关于python - opencv中的色彩空间转换(RGB-> LAB)-红色不会产生期望值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63269882/