Converting an OpenCV BGR 8-bit Image to CIE L*a*b*

后端 未结 4 749
既然无缘
既然无缘 2020-12-30 05:29

I am trying to convert a given Mat representing an RGB image with 8-bit depth to Lab using the function provided in the documentation:

相关标签:
4条回答
  • 2020-12-30 06:07

    That's because L value is in range [0..255] in OpenCV. You can simply scale this value to needed interval ([0..100] in your case).

    0 讨论(0)
  • 2020-12-30 06:15

    In case anyone runs into the same issue:

    Please note that in OpenCV (2.4.13), you can not convert CV_32FC3 BGR images into the Lab color space. That is to say:

    //this->xImage is CV_8UC3
    this->xImage.convertTo(FloatPrecisionImage, CV_32FC3);
    Mat result;
    cvtColor(FloatPrecisionImage, result, COLOR_BGR2Lab);
    this->xImage = result;
    

    will not work while

    Mat result;
    cvtColor(this->xImage, result, COLOR_BGR2Lab);
    result.convertTo(this->xImage, CV_32FC3);
    

    works like a charm. I did not track down the reason for said behavior; however it seems off to me, because this in effect puts limits on the image's quality.

    0 讨论(0)
  • 2020-12-30 06:21

    I am not sure about João Abrantes's range on A and B.

    The opencv documentation has clearly mentioned the CIE L*a*b*range.

    • 8 bit images

    Thus leading to a range of

    0 <= L <= 255
    0 <= a <= 255
    0 <= b <= 255
    
    0 讨论(0)
  • 2020-12-30 06:27

    If anyone is interested in the range of the other variables a and b I made a small program to test their range. If you convert all the colors that are represented with RGB to the CieLab used in OpenCV the ranges are:

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

    And if you're using RGB values in the float mode instead of uint8 the ranges will be:

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

    P.S. If you want to see how distinguishable (regarding human perception) is a LAB value from another LAB value, you should use the floating point. The scale used to keep the lab values in the uint8 ranges messes up with their euclidean distance.

    This is the code I used (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)
    
    0 讨论(0)
提交回复
热议问题