def detect_skin(im, debug=False): hsv = image.rgb2hsv(im) if debug: image.show(hsv, 'hsv') h,s,v = image.split(hsv) if cv.CountNonZero(h) == cv.CountNonZero(s) == 0: white = image.new_from(im) cv.Set(white, 255) return white if debug: image.show(h, "Hue") image.show(s,"sat1") h_rng = 0, 46 s_rng = 48, 178 h = image.threshold(image.gaussian(h, 5), threshold=h_rng[1], type=cv.CV_THRESH_TOZERO_INV) h = image.threshold(h, threshold=h_rng[0], type=cv.CV_THRESH_TOZERO) h = image.threshold(h, threshold=1) s = image.threshold(image.gaussian(s, 5), threshold=s_rng[1], type=cv.CV_THRESH_TOZERO_INV) s = image.threshold(s, threshold=s_rng[0], type=cv.CV_THRESH_TOZERO) if debug: image.show(s,"sat2") s = image.threshold(s, threshold=1) v = image.dilate(image.erode(image.And(s, h))) #im = image.hsv2rgb(image.merge(h,s,v)) if debug: image.show(v, "Human") return image.threshold(v, threshold=1)
def measure(im, debug=False): gray = image.rgb2gray(im) size = cv.GetSize(im) total = float(size[0] * size[1]) l = image.sub(gray, image.gaussian(gray, 5)) l2 = image.sub(gray, image.gaussian(gray, 9)) edges = image.dilate(image.auto_edges(im, percentage=0.2)) if debug: image.show(image.threshold(l, threshold=1), "Before Edge Removal (kernel=5)") image.show(image.threshold(l2, threshold=1), "Before Edge Removal (kernel=9)") cv.Set(l, 0, image.threshold(edges, threshold=1)) cv.Set(l2, 0, image.threshold(edges, threshold=1)) l = image.threshold(l, threshold=1) l2 = image.threshold(l2, threshold=1) if debug: image.show(image.threshold(edges, threshold=1), "Edges") image.show(l, "After Edge Removal (kernel=5)") image.show(l2, "After Edge Removal (kernel=9)") noise2 = image.new_from(gray) cv.EqualizeHist(gray, noise2) cv.AbsDiff(noise2, gray, noise2) cv.Set(noise2, 0, image.threshold(image.sobel(im, xorder=2, yorder=2), threshold=4)) diff = image.cv2array(noise2) if debug: image.show(noise2, "DIFF") print "M", diff.mean(), "S", diff.std() diff_stat = (diff.mean(), diff.std()) percent_noise = cv.CountNonZero(noise2) / total if debug: image.show(noise2, "NOISE2") # magical, I don't understand how this works _, sat, _ = image.split(image.rgb2hsv(im)) edges = image.auto_edges(im) l, u, v = tuple(map(image.equalize_hist, image.split(image.rgb2luv(im)))) u, v = tuple(map(image.gaussian, (u, v))) if debug: image.show(l, "1. L") image.show(u, "1. U") image.show(v, "1. V") la, ua, va, uva = tuple(map(image.cv2array, (l, u, v, image.And(l, u, v)))) test = image.new_from(gray) test2 = image.new_from(gray) cv.Xor(u, v, test) if debug: image.show(test, "2. U Xor V") cv.Set(test, 0, image.dilate(edges)) #cv.Set(test, 0, image.invert(image.threshold(sat, threshold=8))) uv_score = cv.CountNonZero(test) / total if debug: image.show( test, "3. U Xor V - dilate(Edges) - invert(threshold(Saturation))") g = Grid(size) images = map(image.cv2array, g.split_into(test, 6)) arr = image.cv2array(test) avg_mean, avg_std = arr.mean(), arr.std() #ms = [(a.mean(), a.std()) for a in images] #min_mean = min_std = 255 #max_mean = max_std = 0 #for m,s in ms: # min_mean = min(min_mean, m) # min_std = min(min_std, s) # max_mean = max(max_mean, m) # max_std = max(max_std, s) #if debug: # print min_mean, min_std # print avg_mean, avg_std # print max_mean, max_std # #score = uv_score, min_mean, avg_mean, avg_std, max_mean uv_score = uv_score, avg_std score = cv.CountNonZero(l) / total, cv.CountNonZero(l2) / total, \ diff_stat[0], diff_stat[1], uv_score return l, score
def measure(im, debug=False): gray = image.rgb2gray(im) size = cv.GetSize(im) total = float(size[0] * size[1]) l = image.sub(gray, image.gaussian(gray, 5)) l2 = image.sub(gray, image.gaussian(gray, 9)) edges = image.dilate(image.auto_edges(im, percentage=0.2)) if debug: image.show(image.threshold(l, threshold=1), "Before Edge Removal (kernel=5)") image.show(image.threshold(l2, threshold=1), "Before Edge Removal (kernel=9)") cv.Set(l, 0, image.threshold(edges, threshold=1)) cv.Set(l2, 0, image.threshold(edges, threshold=1)) l = image.threshold(l, threshold=1) l2 = image.threshold(l2, threshold=1) if debug: image.show(image.threshold(edges, threshold=1), "Edges") image.show(l, "After Edge Removal (kernel=5)") image.show(l2, "After Edge Removal (kernel=9)") noise2 = image.new_from(gray) cv.EqualizeHist(gray, noise2) cv.AbsDiff(noise2, gray, noise2) cv.Set(noise2, 0, image.threshold(image.sobel(im, xorder=2, yorder=2), threshold=4)) diff = image.cv2array(noise2) if debug: image.show(noise2, "DIFF") print "M", diff.mean(), "S", diff.std() diff_stat = (diff.mean(), diff.std()) percent_noise = cv.CountNonZero(noise2) / total if debug: image.show(noise2, "NOISE2") # magical, I don't understand how this works _, sat, _ = image.split(image.rgb2hsv(im)) edges = image.auto_edges(im) l,u,v = tuple(map(image.equalize_hist, image.split(image.rgb2luv(im)))) u,v = tuple(map(image.gaussian, (u,v))) if debug: image.show(l, "1. L") image.show(u, "1. U") image.show(v, "1. V") la,ua,va,uva = tuple(map(image.cv2array, (l,u,v, image.And(l,u,v)))) test = image.new_from(gray) test2 = image.new_from(gray) cv.Xor(u,v,test) if debug: image.show(test, "2. U Xor V") cv.Set(test, 0, image.dilate(edges)) #cv.Set(test, 0, image.invert(image.threshold(sat, threshold=8))) uv_score = cv.CountNonZero(test) / total if debug: image.show(test, "3. U Xor V - dilate(Edges) - invert(threshold(Saturation))") g = Grid(size) images = map(image.cv2array, g.split_into(test, 6)) arr = image.cv2array(test) avg_mean, avg_std = arr.mean(), arr.std() #ms = [(a.mean(), a.std()) for a in images] #min_mean = min_std = 255 #max_mean = max_std = 0 #for m,s in ms: # min_mean = min(min_mean, m) # min_std = min(min_std, s) # max_mean = max(max_mean, m) # max_std = max(max_std, s) #if debug: # print min_mean, min_std # print avg_mean, avg_std # print max_mean, max_std # #score = uv_score, min_mean, avg_mean, avg_std, max_mean uv_score = uv_score, avg_std score = cv.CountNonZero(l) / total, cv.CountNonZero(l2) / total, \ diff_stat[0], diff_stat[1], uv_score return l, score