JT *_*Cho 11 c++ rgb opencv colors bgr
我正在尝试使用文档中提供的功能将Mat表示具有8位深度的RGB图像的给定转换为Lab:
cvtColor(source, destination, <conversion code>);
Run Code Online (Sandbox Code Playgroud)
我尝试了以下转换代码:
CV_RGB2Lab
CV_BGR2Lab
CV_LBGR2Lab
Run Code Online (Sandbox Code Playgroud)
我每次都收到奇怪的结果,某些样本的"L"值大于100,字面上<107,125,130>.
我也使用Photoshop检查结果 - 但鉴于107超出0≤L≤100的可接受范围,我无法理解我的错误是什么.
更新: 我将在此处发布我的整体结果:给定由8位BGR表示的图像(Mat),可以通过以下方式转换图像:
cvtColor(source, destination, CV_BGR2Lab);
Run Code Online (Sandbox Code Playgroud)
然后可以通过以下方式访问像素值:
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];
}
}
Run Code Online (Sandbox Code Playgroud)
Joã*_*tes 10
如果有人有兴趣在其他变量的取值范围a和b我做了一个小程序来测试他们的范围.如果将使用RGB表示的所有颜色转换为OpenCV中使用的CieLab,则范围为:
0 <=L<= 255
42 <=a<= 226
20 <=b<= 223
Run Code Online (Sandbox Code Playgroud)
如果您在浮点模式而不是uint8中使用RGB值,则范围将为:
0.0 <=L<= 100.0
-86.1813 <=a<= 98.2352
-107.862 <=b<= 94.4758
Run Code Online (Sandbox Code Playgroud)
PS如果要查看与另一个LAB值的LAB值是多么可区分(关于人类感知),则应使用浮点.用于保持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)
Run Code Online (Sandbox Code Playgroud)