Python Forum
Auto detect threshold for skin detection w/ HSV channel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto detect threshold for skin detection w/ HSV channel
#1
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

np.set_printoptions(threshold=sys.maxsize)

# 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'
    ax.bar(xs, ys.ravel(), zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

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)

cv2.imshow('frame',img)
cv2.imshow('mask',mask)
cv2.imshow('res',res)

plt.show()
Based on the following HSV histogram:
[Image: hsv-histogram.png]

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

Thanks!
Reply
#2
np.argmax(n)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to check if someone send a message in a discord channel? Zerolysimin 1 776 Nov-06-2022, 11:10 AM
Last Post: Larz60+
  Output from Paramiko exec_command from channel pemca 0 2,594 Dec-03-2021, 12:29 PM
Last Post: pemca
  Not able to add multiple channel IDs to python bot flaykez 3 85,193 Oct-18-2021, 01:26 AM
Last Post: CyKlop
  Using pyaudio to stop recording under certain sound threshold Twanski94 2 6,460 Jun-13-2020, 11:35 AM
Last Post: Twanski94
  finding probability of exceding certain threshold Staph 1 1,913 Dec-14-2019, 04:58 AM
Last Post: Larz60+
  pika channel.start_consuming() is not running sundar 0 2,416 Mar-20-2019, 06:28 AM
Last Post: sundar
  How to change TV channel with Python arsenal58 7 6,586 Nov-19-2018, 01:11 AM
Last Post: Larz60+
  AttributeError: type object 'MyClass' has no attribute 'channel' chris0147 2 8,748 Sep-29-2017, 06:16 PM
Last Post: chris0147
  ORA-03113: end-of-file on communication channel Python Cx_Oracle akansha 3 7,565 Nov-17-2016, 08:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020