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-grabcut
48 * This element is a wrapper around OpenCV grabcut implementation. GrabCut is an
49 * image segmentation method based on graph cuts technique. It can be seen as a
50 * way of fine-grain segmenting the image from some FG and BG "seed" areas. The
51 * OpenCV implementation follows the article [1].
52 * The "seed" areas are taken in this element from either an input bounding box
53 * coming from a face detection, or from alpha channel values. The input box is
54 * taken from a "face" event such as the one generated from the 'facedetect'
55 * element. The Alpha channel values should be one of the following (cv.hpp):
57 * GC_BGD = 0, //!< background
58 * GC_FGD = 1, //!< foreground
59 * GC_PR_BGD = 2, //!< most probably background
60 * GC_PR_FGD = 3 //!< most probably foreground
62 * with values over GC_PR_FGD interpreted as GC_PR_FGD. IN CASE OF no alpha mask
63 * input (all 0's or all 1's), the 'GstOpenCvFaceDetect-face' downstream event
64 * is used to create a bbox of PR_FG elements. If both foreground alpha
65 * is not specified and there is no face detection, nothing is done.
67 * [1] C. Rother, V. Kolmogorov, and A. Blake, "GrabCut: Interactive foreground
68 * extraction using iterated graph cuts, ACM Trans. Graph., vol. 23, pp. 309–314,
71 * ## Example launch line
74 * gst-launch-1.0 --gst-debug=grabcut=4 v4l2src device=/dev/video0 ! videoconvert ! grabcut ! videoconvert ! video/x-raw,width=320,height=240 ! ximagesink
76 * Another example launch line
78 * gst-launch-1.0 --gst-debug=grabcut=4 v4l2src device=/dev/video0 ! videoconvert ! facedetect display=0 ! videoconvert ! grabcut test-mode=true ! videoconvert ! video/x-raw,width=320,height=240 ! ximagesink
86 #include "gstgrabcut.h"
87 #include <opencv2/imgproc.hpp>
89 GST_DEBUG_CATEGORY_STATIC (gst_grabcut_debug);
90 #define GST_CAT_DEFAULT gst_grabcut_debug
93 /* Filter signals and args */
107 #define DEFAULT_TEST_MODE FALSE
108 #define DEFAULT_SCALE 1.6
110 G_DEFINE_TYPE (GstGrabcut, gst_grabcut, GST_TYPE_OPENCV_VIDEO_FILTER);
111 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
114 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGBA")));
116 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGBA")));
122 static void gst_grabcut_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_grabcut_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static GstFlowReturn gst_grabcut_transform_ip (GstOpencvVideoFilter * filter,
128 GstBuffer * buf, Mat img);
129 static gboolean gst_grabcut_set_caps (GstOpencvVideoFilter * filter,
130 gint in_width, gint in_height, int in_cv_type,
131 gint out_width, gint out_height, int out_cv_type);
133 //static void gst_grabcut_release_all_pointers (GstGrabcut * filter);
135 static void compose_matrix_from_image (Mat output, Mat input);
137 static int run_grabcut_iteration (Mat image_c, Mat mask_c, Mat bgdModel,
139 static int run_grabcut_iteration2 (Mat image_c, Mat mask_c, Mat bgdModel,
140 Mat fgdModel, Rect bbox);
144 gst_grabcut_finalize (GObject * obj)
146 GstGrabcut *filter = GST_GRABCUT (obj);
148 filter->cvRGBin.release ();
149 filter->cvA.release ();
150 filter->cvB.release ();
151 filter->cvC.release ();
152 filter->cvD.release ();
153 filter->grabcut_mask.release ();
154 filter->bgdModel.release ();
155 filter->fgdModel.release ();
157 G_OBJECT_CLASS (gst_grabcut_parent_class)->finalize (obj);
160 /* initialize the grabcut's class */
162 gst_grabcut_class_init (GstGrabcutClass * klass)
164 GObjectClass *gobject_class = (GObjectClass *) klass;
165 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
166 GstOpencvVideoFilterClass *cvbasefilter_class =
167 (GstOpencvVideoFilterClass *) klass;
168 GstBaseTransformClass *btrans_class = (GstBaseTransformClass *) klass;
170 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_grabcut_finalize);
171 gobject_class->set_property = gst_grabcut_set_property;
172 gobject_class->get_property = gst_grabcut_get_property;
174 btrans_class->passthrough_on_same_caps = TRUE;
176 cvbasefilter_class->cv_trans_ip_func = gst_grabcut_transform_ip;
177 cvbasefilter_class->cv_set_caps = gst_grabcut_set_caps;
179 g_object_class_install_property (gobject_class, PROP_TEST_MODE,
180 g_param_spec_boolean ("test-mode", "test-mode",
181 "If true, the output RGB is overwritten with the segmented foreground. Alpha channel same as normal case ",
182 DEFAULT_TEST_MODE, (GParamFlags)
183 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
185 g_object_class_install_property (gobject_class, PROP_SCALE,
186 g_param_spec_float ("scale", "scale",
187 "Grow factor for the face bounding box, if present", 1.0,
189 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
191 gst_element_class_set_static_metadata (element_class,
192 "Grabcut-based image FG/BG segmentation", "Filter/Effect/Video",
193 "Runs Grabcut algorithm on input alpha. Values: BG=0, FG=1, PR_BG=2, PR_FGD=3; \
194 NOTE: larger values of alpha (notably 255) are interpreted as PR_FGD too. \n\
195 IN CASE OF no alpha mask input (all 0's or all 1's), the 'face' \
196 downstream event is used to create a bbox of PR_FG elements.\n\
197 IF nothing is present, then nothing is done.", "Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>");
199 gst_element_class_add_static_pad_template (element_class, &src_factory);
200 gst_element_class_add_static_pad_template (element_class, &sink_factory);
204 /* initialize the new element
205 * instantiate pads and add them to element
206 * set pad callback functions
207 * initialize instance structure
210 gst_grabcut_init (GstGrabcut * filter)
212 filter->test_mode = DEFAULT_TEST_MODE;
213 filter->scale = DEFAULT_SCALE;
214 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER (filter), TRUE);
219 gst_grabcut_set_property (GObject * object, guint prop_id,
220 const GValue * value, GParamSpec * pspec)
222 GstGrabcut *grabcut = GST_GRABCUT (object);
226 grabcut->test_mode = g_value_get_boolean (value);
229 grabcut->scale = g_value_get_float (value);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gst_grabcut_get_property (GObject * object, guint prop_id,
239 GValue * value, GParamSpec * pspec)
241 GstGrabcut *filter = GST_GRABCUT (object);
245 g_value_set_boolean (value, filter->test_mode);
248 g_value_set_float (value, filter->scale);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 /* GstElement vmethod implementations */
257 /* this function handles the link with other elements */
259 gst_grabcut_set_caps (GstOpencvVideoFilter * filter, gint in_width,
260 gint in_height, int in_cv_type, gint out_width,
261 gint out_height, int out_cv_type)
263 GstGrabcut *grabcut = GST_GRABCUT (filter);
266 size = Size (in_width, in_height);
269 grabcut->cvRGBin.create (size, CV_8UC3);
271 grabcut->cvA.create (size, CV_8UC1);
272 grabcut->cvB.create (size, CV_8UC1);
273 grabcut->cvC.create (size, CV_8UC1);
274 grabcut->cvD.create (size, CV_8UC1);
276 grabcut->grabcut_mask = Mat::zeros (size, CV_8UC1);
277 grabcut->bgdModel = Mat ();
278 grabcut->fgdModel = Mat ();
279 //initialise_grabcut (&(grabcut->GC), grabcut->cvRGBin, grabcut->grabcut_mask);
285 gst_grabcut_transform_ip (GstOpencvVideoFilter * filter, GstBuffer * buffer,
288 GstGrabcut *gc = GST_GRABCUT (filter);
290 std::vector < Mat > channels (4);
292 GstVideoRegionOfInterestMeta *meta;
293 meta = gst_buffer_get_video_region_of_interest_meta (buffer);
295 gc->facepos.x = (meta->x) - ((gc->scale - 1) * meta->w / 2);
296 gc->facepos.y = (meta->y) - ((gc->scale - 1) * meta->h / 2);
297 gc->facepos.width = meta->w * gc->scale * 0.9;
298 gc->facepos.height = meta->h * gc->scale * 1.1;
300 memset (static_cast < void *>(&(gc->facepos)), 0, sizeof (gc->facepos));
303 /* normally input should be RGBA */
304 split (img, channels);
305 gc->cvA = channels.at (0);
306 gc->cvB = channels.at (1);
307 gc->cvC = channels.at (2);
308 gc->cvD = channels.at (3);
309 cvtColor (img, gc->cvRGBin, COLOR_BGRA2BGR);
310 compose_matrix_from_image (gc->grabcut_mask, gc->cvD);
312 /* Pass cvD to grabcut_mask for the graphcut stuff but that only if
313 really there is something in the mask! otherwise -->input bbox is
315 alphapixels = countNonZero (gc->cvD);
316 if ((0 < alphapixels) && (alphapixels < (gc->width * gc->height))) {
317 GST_INFO ("running on mask");
318 run_grabcut_iteration (gc->cvRGBin, gc->grabcut_mask, gc->bgdModel,
321 if ((abs (gc->facepos.width) > 2) && (abs (gc->facepos.height) > 2)) {
322 GST_INFO ("running on bbox (%d,%d),(%d,%d)", gc->facepos.x, gc->facepos.y,
323 gc->facepos.width, gc->facepos.height);
324 run_grabcut_iteration2 (gc->cvRGBin, gc->grabcut_mask, gc->bgdModel,
325 gc->fgdModel, gc->facepos);
327 GST_WARNING ("No face info present, skipping frame.");
332 /* if we want to display, just overwrite the output */
334 /* get only FG, PR_FG */
335 bitwise_and (gc->grabcut_mask, Scalar (1), gc->grabcut_mask);
336 /* (saturated) FG, PR_FG --> 255 */
337 gc->grabcut_mask.convertTo (gc->grabcut_mask, -1, 255.0, 0.0);
339 bitwise_and (gc->grabcut_mask, gc->cvA, gc->cvA);
340 bitwise_and (gc->grabcut_mask, gc->cvB, gc->cvB);
341 bitwise_and (gc->grabcut_mask, gc->cvC, gc->cvC);
344 merge (channels, img);
348 Point (gc->facepos.x, gc->facepos.y),
349 Point (gc->facepos.x + gc->facepos.width,
350 gc->facepos.y + gc->facepos.height), CV_RGB (255, 0, 255), 1, 8, 0);
356 /* entry point to initialize the plug-in
357 * initialize the plug-in itself
358 * register the element factories and other features
361 gst_grabcut_plugin_init (GstPlugin * plugin)
363 /* debug category for fltering log messages
366 GST_DEBUG_CATEGORY_INIT (gst_grabcut_debug, "grabcut",
368 "Grabcut image segmentation on either input alpha or input bounding box");
370 return gst_element_register (plugin, "grabcut", GST_RANK_NONE,
375 compose_matrix_from_image (Mat output, Mat input)
379 for (x = 0; x < output.cols; x++) {
380 for (y = 0; y < output.rows; y++) {
381 output.data[output.step[0] * y + x] =
382 (input.data[input.step[0] * y + x] <=
383 GC_PR_FGD) ? input.data[input.step[0] * y + x] : GC_PR_FGD;
389 run_grabcut_iteration (Mat image_c, Mat mask_c, Mat bgdModel, Mat fgdModel)
391 if (countNonZero (mask_c))
392 grabCut (image_c, mask_c, Rect (),
393 bgdModel, bgdModel, 1, GC_INIT_WITH_MASK);
399 run_grabcut_iteration2 (Mat image_c, Mat mask_c, Mat bgdModel, Mat fgdModel,
402 grabCut (image_c, mask_c, bbox, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT);