3 * Copyright (C) 2013 Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Alternatively, the contents of this file may be used under the
24 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25 * which case the following provisions apply instead of the ones
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41 * Boston, MA 02110-1301, USA.
45 * SECTION:element-skindetect
47 * Human skin detection on videos and images
49 * ## Example launch line
52 * gst-launch-1.0 videotestsrc ! decodebin ! videoconvert ! skindetect ! videoconvert ! xvimagesink
60 #include "gstskindetect.h"
61 #include <opencv2/imgproc.hpp>
63 GST_DEBUG_CATEGORY_STATIC (gst_skin_detect_debug);
64 #define GST_CAT_DEFAULT gst_skin_detect_debug
66 /* Filter signals and args */
84 } GstSkindetectMethod;
86 #define GST_TYPE_SKIN_DETECT_METHOD (gst_skin_detect_method_get_type ())
88 gst_skin_detect_method_get_type (void)
90 static GType etype = 0;
92 static const GEnumValue values[] = {
93 {HSV, "Classic HSV thresholding", "hsv"},
94 {RGB, "Normalised-RGB colorspace thresholding", "rgb"},
97 etype = g_enum_register_static ("GstSkindetectMethod", values);
102 G_DEFINE_TYPE_WITH_CODE (GstSkinDetect, gst_skin_detect,
103 GST_TYPE_OPENCV_VIDEO_FILTER,
104 GST_DEBUG_CATEGORY_INIT (gst_skin_detect_debug, "skindetect", 0,
105 "Performs skin detection on videos and images");
107 GST_ELEMENT_REGISTER_DEFINE (skindetect, "skindetect", GST_RANK_NONE,
108 GST_TYPE_SKIN_DETECT);
110 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
113 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
115 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
118 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
121 static void gst_skin_detect_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_skin_detect_get_property (GObject * object, guint prop_id,
124 GValue * value, GParamSpec * pspec);
126 static GstFlowReturn gst_skin_detect_transform (GstOpencvVideoFilter * filter,
127 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
129 static void gst_skin_detect_finalize (GObject * object);
131 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
132 gint in_width, gint in_height, int in_cv_type,
133 gint out_width, gint out_height, int out_cv_type);
135 /* initialize the skindetect's class */
137 gst_skin_detect_class_init (GstSkinDetectClass * klass)
139 GObjectClass *gobject_class;
140 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
141 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
143 gobject_class = (GObjectClass *) klass;
144 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
146 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_skin_detect_finalize);
147 gobject_class->set_property = gst_skin_detect_set_property;
148 gobject_class->get_property = gst_skin_detect_get_property;
150 gstopencvbasefilter_class->cv_trans_func = gst_skin_detect_transform;
152 g_object_class_install_property (gobject_class, PROP_POSTPROCESS,
153 g_param_spec_boolean ("postprocess", "Postprocess",
154 "Apply opening-closing to skin detection to extract large, significant blobs ",
156 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
157 g_object_class_install_property (gobject_class, PROP_METHOD,
158 g_param_spec_enum ("method",
161 GST_TYPE_SKIN_DETECT_METHOD, HSV,
162 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
164 gst_element_class_set_static_metadata (element_class,
166 "Filter/Effect/Video",
167 "Performs non-parametric skin detection on input",
168 "Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>");
170 gst_element_class_add_static_pad_template (element_class, &src_factory);
171 gst_element_class_add_static_pad_template (element_class, &sink_factory);
173 gstopencvbasefilter_class->cv_set_caps = gst_skin_detect_set_caps;
175 gst_type_mark_as_plugin_api (GST_TYPE_SKIN_DETECT_METHOD, (GstPluginAPIFlags) 0);
178 /* initialize the new element
179 * instantiate pads and add them to element
180 * set pad callback functions
181 * initialize instance structure
184 gst_skin_detect_init (GstSkinDetect * filter)
186 filter->postprocess = TRUE;
187 filter->method = HSV;
189 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
195 gst_skin_detect_set_property (GObject * object, guint prop_id,
196 const GValue * value, GParamSpec * pspec)
198 GstSkinDetect *filter = GST_SKIN_DETECT (object);
201 case PROP_POSTPROCESS:
202 filter->postprocess = g_value_get_boolean (value);
205 filter->method = g_value_get_enum (value);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gst_skin_detect_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec)
217 GstSkinDetect *filter = GST_SKIN_DETECT (object);
220 case PROP_POSTPROCESS:
221 g_value_set_boolean (value, filter->postprocess);
224 g_value_set_enum (value, filter->method);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 /* GstElement vmethod implementations */
233 /* this function handles the link with other elements */
235 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
236 gint in_width, gint in_height, int in_cv_type,
237 gint out_width, gint out_height, int out_cv_type)
239 GstSkinDetect *filter = GST_SKIN_DETECT (transform);
240 cv::Size size = cv::Size (in_width, in_height);
242 filter->cvRGB.create (size, CV_8UC3);
243 filter->cvChA.create (size, CV_8UC1);
244 filter->width = in_width;
245 filter->height = in_height;
247 filter->cvHSV.create (size, CV_8UC3);
248 filter->cvH.create (size, CV_8UC1); /* Hue component. */
249 filter->cvH2.create (size, CV_8UC1); /* Hue component, 2nd threshold */
250 filter->cvS.create (size, CV_8UC1); /* Saturation component. */
251 filter->cvV.create (size, CV_8UC1); /* Brightness component. */
252 filter->cvSkinPixels1.create (size, CV_8UC1); /* Greyscale output image */
254 filter->cvR.create (size, CV_8UC1); /* R component. */
255 filter->cvG.create (size, CV_8UC1); /* G component. */
256 filter->cvB.create (size, CV_8UC1); /* B component. */
257 filter->cvAll.create (size, CV_32FC1); /* (R+G+B) component. */
258 filter->cvR2.create (size, CV_32FC1); /* R component, 32bits */
259 filter->cvRp.create (size, CV_32FC1); /* R' and >0.4 */
260 filter->cvGp.create (size, CV_32FC1); /* G' and > 0.28 */
261 filter->cvRp2.create (size, CV_32FC1); /* R' <0.6 */
262 filter->cvGp2.create (size, CV_32FC1); /* G' <0.4 */
263 filter->cvSkinPixels2.create (size, CV_32FC1); /* Greyscale output image. */
264 filter->cvdraft.create (size, CV_8UC1); /* Greyscale output image. */
271 gst_skin_detect_finalize (GObject * object)
273 GstSkinDetect *filter = GST_SKIN_DETECT (object);
275 filter->cvRGB.release ();
276 filter->cvChA.release ();
277 filter->cvHSV.release ();
278 filter->cvH.release ();
279 filter->cvH2.release ();
280 filter->cvS.release ();
281 filter->cvV.release ();
282 filter->cvSkinPixels1.release ();
283 filter->cvR.release ();
284 filter->cvG.release ();
285 filter->cvB.release ();
286 filter->cvAll.release ();
287 filter->cvR2.release ();
288 filter->cvRp.release ();
289 filter->cvGp.release ();
290 filter->cvRp2.release ();
291 filter->cvGp2.release ();
292 filter->cvdraft.release ();
293 filter->cvSkinPixels2.release ();
295 G_OBJECT_CLASS (gst_skin_detect_parent_class)->finalize (object);
299 gst_skin_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
300 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
302 GstSkinDetect *filter = GST_SKIN_DETECT (base);
304 std::vector < cv::Mat > channels (3);
305 filter->cvRGB = cv::Mat (img);
307 /* SKIN COLOUR BLOB DETECTION */
308 if (HSV == filter->method) {
309 cv::cvtColor (filter->cvRGB, filter->cvHSV, cv::COLOR_RGB2HSV);
310 cv::split (filter->cvHSV, channels);
311 filter->cvH = channels.at (0);
312 filter->cvS = channels.at (1);
313 filter->cvV = channels.at (2);
315 /* Detect which pixels in each of the H, S and V channels are probably skin pixels.
316 Assume that skin has a Hue between 0 to 18 (out of 180), and Saturation above 50, and Brightness above 80. */
317 cv::threshold (filter->cvH, filter->cvH2, 10, UCHAR_MAX, cv::THRESH_BINARY); /* (hue > 10) */
318 cv::threshold (filter->cvH, filter->cvH, 20, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (hue < 20) */
319 cv::threshold (filter->cvS, filter->cvS, 48, UCHAR_MAX, cv::THRESH_BINARY); /* (sat > 48) */
320 cv::threshold (filter->cvV, filter->cvV, 80, UCHAR_MAX, cv::THRESH_BINARY); /* (val > 80) */
322 /* erode the HUE to get rid of noise. */
323 cv::erode (filter->cvH, filter->cvH, cv::Mat (), cv::Point (-1, -1), 1);
325 /* Combine all 3 thresholded color components, so that an output pixel will only
326 be white (255) if the H, S and V pixels were also white.
327 imageSkin = (hue > 10) ^ (hue < 20) ^ (sat > 48) ^ (val > 80), where ^ mean pixels-wise AND */
328 cv::bitwise_and (filter->cvH, filter->cvS, filter->cvSkinPixels1);
329 cv::bitwise_and (filter->cvSkinPixels1, filter->cvH2,
330 filter->cvSkinPixels1);
331 cv::bitwise_and (filter->cvSkinPixels1, filter->cvV, filter->cvSkinPixels1);
333 cv::cvtColor (filter->cvSkinPixels1, filter->cvRGB, cv::COLOR_GRAY2RGB);
334 } else if (RGB == filter->method) {
335 cv::split (filter->cvRGB, channels);
336 filter->cvR = channels.at (0);
337 filter->cvG = channels.at (1);
338 filter->cvB = channels.at (2);
339 cv::add (filter->cvR, filter->cvG, filter->cvAll);
340 cv::add (filter->cvB, filter->cvAll, filter->cvAll); /* All = R + G + B */
341 cv::divide (filter->cvR, filter->cvAll, filter->cvRp, 1.0, filter->cvRp.type ()); /* R' = R / ( R + G + B) */
342 cv::divide (filter->cvG, filter->cvAll, filter->cvGp, 1.0, filter->cvGp.type ()); /* G' = G / ( R + G + B) */
344 filter->cvR.convertTo (filter->cvR2, filter->cvR2.type (), 1.0, 0.0);
345 filter->cvGp.copyTo (filter->cvGp2);
346 filter->cvRp.copyTo (filter->cvRp2);
348 cv::threshold (filter->cvR2, filter->cvR2, 60, UCHAR_MAX, cv::THRESH_BINARY); /* (R > 60) */
349 cv::threshold (filter->cvRp, filter->cvRp, 0.42, UCHAR_MAX, cv::THRESH_BINARY); /* (R'> 0.4) */
350 cv::threshold (filter->cvRp2, filter->cvRp2, 0.6, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (R'< 0.6) */
351 cv::threshold (filter->cvGp, filter->cvGp, 0.28, UCHAR_MAX, cv::THRESH_BINARY); /* (G'> 0.28) */
352 cv::threshold (filter->cvGp2, filter->cvGp2, 0.4, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (G'< 0.4) */
354 /* Combine all 3 thresholded color components, so that an output pixel will only
355 be white (255) if the H, S and V pixels were also white. */
357 cv::bitwise_and (filter->cvR2, filter->cvRp, filter->cvSkinPixels2);
358 cv::bitwise_and (filter->cvRp, filter->cvSkinPixels2,
359 filter->cvSkinPixels2);
360 cv::bitwise_and (filter->cvRp2, filter->cvSkinPixels2,
361 filter->cvSkinPixels2);
362 cv::bitwise_and (filter->cvGp, filter->cvSkinPixels2,
363 filter->cvSkinPixels2);
364 cv::bitwise_and (filter->cvGp2, filter->cvSkinPixels2,
365 filter->cvSkinPixels2);
367 filter->cvSkinPixels2.convertTo (filter->cvdraft, filter->cvdraft.type (),
369 cv::cvtColor (filter->cvdraft, filter->cvRGB, cv::COLOR_GRAY2RGB);
372 /* After this we have a RGB Black and white image with the skin, in
373 filter->cvRGB. We can postprocess by applying 1 erode-dilate and 1
374 dilate-erode, or alternatively 1 opening-closing all together, with
375 the goal of removing small (spurious) skin spots and creating large
377 if (filter->postprocess) {
378 cv::split (filter->cvRGB, channels);
379 filter->cvChA = channels.at (0);
382 cv::getStructuringElement (cv::MORPH_RECT, cv::Size (3, 3),
384 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
385 cv::dilate (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 2);
386 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
388 cv::cvtColor (filter->cvChA, filter->cvRGB, cv::COLOR_GRAY2RGB);
391 filter->cvRGB.copyTo (outimg);