Debian Codegraph @

src/filter/bgsubtract0r/bgsubtract0r.c

raw symbols SCIP
1 /* bgsubtract0r.c */
2
3 /*
4 * Frei0r plugin to perform background subtraction on a still video.
5 *
6 * Copyright (C) 2013 Samuel Mimram
7 *
8 * This plugin operates on still videos, i.e. the camera is not moving, and its
9 * aim is to remove the background image, keeping only the moving parts. It is
10 * thus a way to have a green screen without a green screen. The first image is
11 * taken as reference for the background, so the video should start without the
12 * moving stuff, who should come in afterwards.
13 *
14 * A few settings can be set such as:
15 * - the threshold in order to decide whether a pixel is the same as in the
16 * reference image
17 * - whether to remove "noise" (i.e. isolated pixels)
18 * - optional blurring of edges
19 *
20 * Some recommendations:
21 * - obviously the background should be of a (really) different color than the
22 * moving actor
23 * - light in order to avoid shadows on the background
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40 #include <stdlib.h>
41 #include <assert.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "frei0r.h"
46
47 typedef struct bgsubtract0r_instance
48 {
49 unsigned int width;
50 unsigned int height;
51 uint8_t threshold;
52 char denoise; /* Remove noise from mask. */
53 uint32_t* reference; /* The reference image. */
54 uint8_t* mask; /* Where the mask is computed. */
55 int blur; /* Width of alpha-channel blurring. */
56 } bgsubtract0r_instance_t;
57
58 int f0r_init()
59 {
60 return 1;
61 }
62
63 void f0r_deinit()
64 {
65 }
66
67 void f0r_get_plugin_info(f0r_plugin_info_t* bgsubtract0r_info)
68 {
69 bgsubtract0r_info->name = "BgSubtract0r";
70 bgsubtract0r_info->author = "Samuel Mimram";
71 bgsubtract0r_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
72 bgsubtract0r_info->color_model = F0R_COLOR_MODEL_RGBA8888;
73 bgsubtract0r_info->frei0r_version = FREI0R_MAJOR_VERSION;
74 bgsubtract0r_info->major_version = 0;
75 bgsubtract0r_info->minor_version = 3;
76 bgsubtract0r_info->num_params = 3;
77 bgsubtract0r_info->explanation = "Bluescreen the background of a static video.";
78 }
79
80 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
81 {
82 bgsubtract0r_instance_t* inst = (bgsubtract0r_instance_t*)calloc(1, sizeof(*inst));
83 inst->width = width;
84 inst->height = height;
85 inst->denoise = 1;
86 inst->blur = 0;
87 inst->threshold = 26;
88 inst->reference = NULL;
89 inst->mask = malloc(sizeof(uint8_t)*width*height);
90 return (f0r_instance_t)inst;
91 }
92
93 void f0r_destruct(f0r_instance_t instance)
94 {
95 bgsubtract0r_instance_t* inst = (bgsubtract0r_instance_t*)instance;
96 free(inst->reference);
97 free(inst->mask);
98 free(inst);
99 }
100
101 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
102 {
103 switch(param_index)
104 {
105 case 0:
106 info->name = "threshold";
107 info->type = F0R_PARAM_DOUBLE;
108 info->explanation = "Threshold for difference";
109 break;
110
111 case 1:
112 info->name = "denoise";
113 info->type = F0R_PARAM_BOOL;
114 info->explanation = "Remove noise";
115 break;
116
117 case 2:
118 info->name = "blur";
119 info->type = F0R_PARAM_DOUBLE;
120 info->explanation = "Blur alpha channel by given radius (to remove sharp edges)";
121 break;
122 }
123 }
124
125 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
126 {
127 assert(instance);
128 bgsubtract0r_instance_t* inst = (bgsubtract0r_instance_t*)instance;
129
130 switch(param_index)
131 {
132 case 0:
133 inst->threshold = *((double*)param) * 255.;
134 break;
135
136 case 1:
137 inst->denoise = *((double*)param) >= 0.5;
138 break;
139
140 case 2:
141 inst->blur = (int)(*((double*)param)+0.5);
142 break;
143 }
144 }
145
146 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
147 {
148 assert(instance);
149 bgsubtract0r_instance_t* inst = (bgsubtract0r_instance_t*)instance;
150
151 switch(param_index)
152 {
153 case 0:
154 *((double*)param) = (double)inst->threshold / 255.;
155 break;
156
157 case 1:
158 *((double*)param) = inst->denoise ? 1. : 0.;
159 break;
160
161 case 2:
162 *((double*)param) = inst->blur;
163 break;
164 }
165 }
166
167 #define MAX(x,y) ((x) > (y) ? (x) : (y));
168
169 inline static uint8_t dst(uint32_t x, uint32_t y)
170 {
171 uint8_t d;
172 uint8_t* px = (uint8_t*)&x;
173 uint8_t* py = (uint8_t*)&y;
174
175 d = abs(px[0]-py[0]);
176 d = MAX(d, abs(px[1]-py[1]));
177 d = MAX(d, abs(px[2]-py[2]));
178
179 return d;
180 }
181
182 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
183 {
184 assert(instance);
185 bgsubtract0r_instance_t* inst = (bgsubtract0r_instance_t*)instance;
186 unsigned int width = inst->width;
187 unsigned int height = inst->height;
188 unsigned int len = width * height;
189 uint8_t *mask = inst->mask;
190 int blur = inst->blur;
191 int i;
192 int j;
193 int n;
194 uint8_t* pi;
195 uint8_t* po;
196
197 if (!inst->reference)
198 {
199 int blen = sizeof(uint32_t)*len;
200 inst->reference = malloc(blen);
201 memmove(inst->reference, inframe, blen);
202 memset(mask, 0, sizeof(uint8_t)*len);
203 }
204 else
205 {
206 for (i=0; i<len; i++)
207 mask[i] = (dst(inst->reference[i], inframe[i]) > inst->threshold) ? 0xff : 0;
208 }
209
210 /* Clean up the mask. */
211 if (inst->denoise)
212 for (j=1; j<height-1; j++)
213 for (i=1; i<width-1; i++)
214 {
215 n = (mask[width*j+i-1]+mask[width*j+i+1]+mask[width*(j-1)+i]+mask[width*(j+1)+i]
216 + mask[width*(j-1)+i-1]+mask[width*(j-1)+i+1]+mask[width*(j+1)+i-1]+mask[width*(j+1)+i+1])/0xff;
217 if (mask[width*j+i])
218 {
219 if (n<=2) mask[width*j+i] = 0;
220 }
221 else
222 {
223 if (n>=6) mask[width*j+i] = 0xff;
224 }
225 }
226
227 for (i=0; i<len; i++)
228 {
229 pi = (uint8_t*)&inframe[i];
230 po = (uint8_t*)&outframe[i];
231 po[0] = pi[0];
232 po[1] = pi[1];
233 po[2] = pi[2];
234 po[3] = mask[i];
235 }
236
237 if (blur)
238 {
239 int ii, jj;
240 int di, dj;
241 unsigned int a;
242 // Number of pixels in the surface
243 unsigned int s = (2*blur+1)*(2*blur+1);
244
245 for (j=0; j<height; j++)
246 for (i=0; i<width; i++)
247 {
248 a = 0;
249 for (dj=-blur; dj<=blur; dj++)
250 for (di=-blur; di<=blur; di++)
251 {
252 ii = i+di;
253 jj = j+dj;
254 if (ii < 0 || ii >= width || jj < 0 || jj >= height)
255 a += 0xff;
256 else
257 a += mask[width*jj+ii];
258 }
259 a /= s;
260 po = (uint8_t*)&outframe[width*j+i];
261 po[3] = a;
262 }
263 }
264 }