gpt4 book ai didi

c++ - 将 OpenCV BGR 8 位图像转换为 CIE L*a*b*

转载 作者:可可西里 更新时间:2023-11-01 15:04:23 25 4
gpt4 key购买 nike

我正在尝试使用文档中提供的函数将表示具有 8 位深度的 RGB 图像的给定 Mat 转换为 Lab :

cvtColor(source, destination, <conversion code>);

我尝试了以下转换代码:

CV_RGB2Lab
CV_BGR2Lab
CV_LBGR2Lab

我每次都收到奇怪的结果,某些样本的“L”值大于 100,字面意思是 <107、125、130>。

我也在使用 Photoshop 检查结果 - 但鉴于 107 超出了 0 ≤ L ≤ 100 的可接受范围,我无法理解我的错误是什么。

更新:我将在这里发布我的总体结果:给定一个用 8 位 BGR 表示的图像(Mat),图像可以通过以下方式转换:

cvtColor(source, destination, CV_BGR2Lab);

然后可以通过以下方式访问像素值:

int step = destination.step;
int channels = destination.channels();
for (int i = 0; i < destination.rows(); i++) {
for (int j = 0; j < destination.cols(); j++) {
Point3_<uchar> pixelData;
//L*: 0-255 (elsewhere is represented by 0 to 100)
pixelData.x = destination.data[step*i + channels*j + 0];
//a*: 0-255 (elsewhere is represented by -127 to 127)
pixelData.y = destination.data[step*i + channels*j + 1];
//b*: 0-255 (elsewhere is represented by -127 to 127)
pixelData.z = destination.data[step*i + channels*j + 2];
}
}

最佳答案

如果有人对其他变量 ab 的范围感兴趣,我制作了一个小程序来测试它们的范围。如果将所有用 RGB 表示的颜色转换为 OpenCV 中使用的 CieLab,则范围为:

0  <=L<= 255
42 <=a<= 226
20 <=b<= 223

如果您在浮点模式下使用 RGB 值而不是 uint8,则范围将是:

0.0      <=L<= 100.0
-86.1813 <=a<= 98.2352
-107.862 <=b<= 94.4758

附言如果您想了解一个 LAB 值与另一个 LAB 值的区别(关于人类感知),您应该使用 float 。用于将实验室值保持在 uint8 范围内的比例与它们的欧几里德距离相混淆。

这是我使用的代码(python):

L=[0]*256**3
a=[0]*256**3
b=[0]*256**3
i=0
for r in xrange(256):
for g in xrange(256):
for bb in xrange(256):
im = np.array((bb,g,r),np.uint8).reshape(1,1,3)
cv2.cvtColor(im,cv2.COLOR_BGR2LAB,im) #tranform it to LAB
L[i] = im[0,0,0]
a[i] = im[0,0,1]
b[i] = im[0,0,2]
i+=1

print min(L), '<=L<=', max(L)
print min(a), '<=a<=', max(a)
print min(b), '<=b<=', max(b)

关于c++ - 将 OpenCV BGR 8 位图像转换为 CIE L*a*b*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com