2

I know that the easiest way to segment certain color is using the HSV color space, but I have a task to do it in HSV, RGB and Lab... I am really confused how to do it in Lab. I did it in HSV and it's really simple. Here's the function that segments human color using the HSV color space

import cv2
import matplotlib.pyplot as plt
import numpy as np

def HSV_Segmentation(image):
    lowerRange= np.array([0, 70, 0] , dtype="uint8")
    upperRange= np.array([25, 255, 255], dtype="uint8")
    mask = image[:].copy()

    imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    imageRange = cv2.inRange(imageHSV,upperRange, lowerRange)

    mask[:,:,0] = imageRange
    mask[:,:,1] = imageRange
    mask[:,:,2] = imageRange

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    faceHSV = cv2.bitwise_and(image,mask)
    
    return faceHSV

And now I'm stuck at Lab color space,and I am not really sure where to begin. I tried something similar with upper and lower range of human color but obviously that's not the right way.

gkvski
  • 43
  • 6
  • From Wikipedia "The CIELAB ... expresses color as three values: L* for the lightness from black (0) to white (100), a* from green (−) to red (+), and b* from blue (−) to yellow (+). CIELAB was designed so that the same amount of numerical change in these values corresponds to roughly the same amount of visually perceived change." So the L is intensity and you have to look at the appropriate + or - values of A, B to find the color your want. A and B are often biased so that they are all positive. So you have to look at above and below 50%. – fmw42 Nov 07 '20 at 21:50
  • Experiment with http://colormine.org/convert/rgb-to-lab – fmw42 Nov 07 '20 at 21:57
  • The human color is known to be easier to segment in the HSV color space. You could transform the LAB to HSV or find a range in LAB where you can obtain similar results. – JoOkuma Nov 07 '20 at 22:30
  • @JoOkuma Do you have a reference for that? – jtlz2 Feb 15 '21 at 07:05
  • You can find some here (https://scholar.google.com/scholar?hl=en&as_sdt=0%2C39&q=HSV+segmentation+skin&btnG=) – JoOkuma Feb 16 '21 at 11:17

1 Answers1

1

I tried different ranges but this seems to work for me. That wasn't so hard :)

def Lab_Segmentation(image):
    lowerRange= np.array([0, 135, 135] , dtype="uint8")
    upperRange= np.array([255, 160, 195], dtype="uint8")
    mask = image[:].copy()

    imageLab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
    imageRange = cv2.inRange(imageLab,lowerRange, upperRange)
    
    mask[:,:,0] = imageRange
    mask[:,:,1] = imageRange
    mask[:,:,2] = imageRange
    
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    faceLab = cv2.bitwise_and(image,mask)

    return faceLab
gkvski
  • 43
  • 6