Oct-13-2019, 12:23 AM
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.
how would I extract upper and lower thresholds for the skin detection?
Thanks!
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:
how would I extract upper and lower thresholds for the skin detection?
Thanks!