Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Auto detect threshold for skin detection w/ HSV channel
Hello forum,
I have some code that gets the HSV color channels for a given image.
I need to be able to automatically get a lower and upper threshold value based on these values for skin detection.
Basically, I cannot hard-code the threshold values -they have to be extracted from the histogram.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy.signal import argrelextrema
import sys
from scipy.signal import savgol_filter


# Getting HSV values
img = cv2.imread("face_dark.bmp")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

# Getting YCrCb values
YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)  
Y,Cr,Cb = cv2.split(YCrCb)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
for x, c, z in zip([h,s,v], ['r', 'g', 'b'], [30, 20, 10]):
    xs = np.arange(256)
    ys = cv2.calcHist([x], [0], None, [256], [0,256])
    cs = [c] * len(xs)
    cs[0] = 'c', ys.ravel(), zs=z, zdir='y', color=cs, alpha=0.8)


array = np.asarray(img)
arr = (array.astype(float))/255.0
img_hsv = matplotlib.colors.rgb_to_hsv(arr[...,:3])

# hard-coded thresholds
lower = np.array([0, 10, 60], dtype = "uint8") 
upper = np.array([20, 150, 255], dtype = "uint8")

mask = cv2.inRange(hsv, lower, upper)
res  = cv2.bitwise_and(img, img, mask=mask)

Based on the following HSV histogram:

how would I extract upper and lower thresholds for the skin detection?


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  pika channel.start_consuming() is not running sundar 0 417 Mar-20-2019, 06:28 AM
Last Post: sundar
  How to change TV channel with Python arsenal58 7 704 Nov-19-2018, 01:11 AM
Last Post: Larz60+
  AttributeError: type object 'MyClass' has no attribute 'channel' chris0147 2 3,394 Sep-29-2017, 06:16 PM
Last Post: chris0147
  ORA-03113: end-of-file on communication channel Python Cx_Oracle akansha 3 2,914 Nov-17-2016, 08:50 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)