pythonopencvlab-color-space

What is the example reference threshold for each component of a LAB image?


I'm doing a project of face skin detection. I need to replace each pixel in a face image with a black pixel if the image intensity is less than some fixed constant T, or a white pixel if the image intensity is greater than that constant.

I know in opencv, cv2.threshold takes two arguments, First argument is the source image, which should be a grayscale image. Second argument is the threshold value which is used to classify the pixel values.

Can anybody tell me how to threshold color images by designating a separate threshold for each of the LAB components of the image and then combine them with an AND operation?

Example threshold ranges would be great!


Solution

  • here is an sample code i wrote:

    import cv2
    
    color_image = cv2.imread("lena.png")
    lab_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2LAB)
    L,A,B=cv2.split(lab_image)  
    
    th, th_image = cv2.threshold(L,100,255,cv2.THRESH_BINARY)
    
    #cv2.imshow("original",color_image)
    #cv2.imshow("l space",L)
    cv2.imshow("th imaged",th_image)
    
    
    # wait until escape is pressed
    while True:
        keyboard = cv2.waitKey()
        if keyboard == 27:
            break
    
    cv2.destroyAllWindows()
    

    here is the official doc:

    cv.Threshold(src, dst, threshold, maxValue, thresholdType) → None

    Parameters:

    • src – input array (single-channel, 8-bit or 32-bit floating point).
      • dst – output array of the same size and type as src.
      • thresh – threshold value.
      • maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
      • type – thresholding type (see the details below).

    the code will generate you these three images:

    Original:

    enter image description here

    L-Space of the LAB Convertion:

    enter image description here

    and finally a simple threshold example:

    enter image description here

    a rly good tutorial can be found here for tresholding: OpenCV