Debian Codegraph @

src/filter/bgsubtract0r/bgsubtract0r.c

raw symbols SCIP
1 /* bgsubtract0r.c */cxx . . $ `<file>/src/filter/bgsubtract0r/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"cxx . . $ `<file>/include/frei0r.h`/
46
47 typedef struct bgsubtract0r_instancecxx . . $ bgsubtract0r_instance#
48 {
49 unsigned int widthcxx . . $ bgsubtract0r_instance#width.;
50 unsigned int heightcxx . . $ bgsubtract0r_instance#height.;
51 uint8_tcxx . . $ uint8_t# thresholdcxx . . $ bgsubtract0r_instance#threshold.;
52 char denoisecxx . . $ bgsubtract0r_instance#denoise.; /* Remove noise from mask. */
53 uint32_tcxx . . $ uint32_t#* referencecxx . . $ bgsubtract0r_instance#reference.; /* The reference image. */
54 uint8_tcxx . . $ uint8_t#* maskcxx . . $ bgsubtract0r_instance#mask.; /* Where the mask is computed. */
55 int blurcxx . . $ bgsubtract0r_instance#blur.; /* Width of alpha-channel blurring. */
56 } bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#;
57
58 int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).()
59 {
60 return 1;
61 }
62
63 void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).()
64 {
65 }
66
67 void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* bgsubtract0r_info)
68 {
69 bgsubtract0r_info->namecxx . . $ f0r_plugin_info#name. = "BgSubtract0r";
70 bgsubtract0r_info->authorcxx . . $ f0r_plugin_info#author. = "Samuel Mimram";
71 bgsubtract0r_info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!;
72 bgsubtract0r_info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!;
73 bgsubtract0r_info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!;
74 bgsubtract0r_info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0;
75 bgsubtract0r_info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 3;
76 bgsubtract0r_info->num_paramscxx . . $ f0r_plugin_info#num_params. = 3;
77 bgsubtract0r_info->explanationcxx . . $ f0r_plugin_info#explanation. = "Bluescreen the background of a static video.";
78 }
79
80 f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height)
81 {
82 bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#* inst = (bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst));
83 inst->widthcxx . . $ bgsubtract0r_instance#width. = width;
84 inst->heightcxx . . $ bgsubtract0r_instance#height. = height;
85 inst->denoisecxx . . $ bgsubtract0r_instance#denoise. = 1;
86 inst->blurcxx . . $ bgsubtract0r_instance#blur. = 0;
87 inst->thresholdcxx . . $ bgsubtract0r_instance#threshold. = 26;
88 inst->referencecxx . . $ bgsubtract0r_instance#reference. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
89 inst->maskcxx . . $ bgsubtract0r_instance#mask. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(uint8_tcxx . . $ uint8_t#)*width*height);
90 return (f0r_instance_tcxx . . $ f0r_instance_t#)inst;
91 }
92
93 void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance)
94 {
95 bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#* inst = (bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#*)instance;
96 freecxx . . $ free(5d22bdacc48458e8).(inst->referencecxx . . $ bgsubtract0r_instance#reference.);
97 freecxx . . $ free(5d22bdacc48458e8).(inst->maskcxx . . $ bgsubtract0r_instance#mask.);
98 freecxx . . $ free(5d22bdacc48458e8).(inst);
99 }
100
101 void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index)
102 {
103 switch(param_index)
104 {
105 case 0:
106 info->namecxx . . $ f0r_param_info#name. = "threshold";
107 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
108 info->explanationcxx . . $ f0r_param_info#explanation. = "Threshold for difference";
109 break;
110
111 case 1:
112 info->namecxx . . $ f0r_param_info#name. = "denoise";
113 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_BOOLcxx . . $ `include/frei0r.h:377:9`!;
114 info->explanationcxx . . $ f0r_param_info#explanation. = "Remove noise";
115 break;
116
117 case 2:
118 info->namecxx . . $ f0r_param_info#name. = "blur";
119 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
120 info->explanationcxx . . $ f0r_param_info#explanation. = "Blur alpha channel by given radius (to remove sharp edges)";
121 break;
122 }
123 }
124
125 void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
126 {
127 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
128 bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#* inst = (bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#*)instance;
129
130 switch(param_index)
131 {
132 case 0:
133 inst->thresholdcxx . . $ bgsubtract0r_instance#threshold. = *((double*)param) * 255.;
134 break;
135
136 case 1:
137 inst->denoisecxx . . $ bgsubtract0r_instance#denoise. = *((double*)param) >= 0.5;
138 break;
139
140 case 2:
141 inst->blurcxx . . $ bgsubtract0r_instance#blur. = (int)(*((double*)param)+0.5);
142 break;
143 }
144 }
145
146 void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
147 {
148 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
149 bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#* inst = (bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#*)instance;
150
151 switch(param_index)
152 {
153 case 0:
154 *((double*)param) = (double)inst->thresholdcxx . . $ bgsubtract0r_instance#threshold. / 255.;
155 break;
156
157 case 1:
158 *((double*)param) = inst->denoisecxx . . $ bgsubtract0r_instance#denoise. ? 1. : 0.;
159 break;
160
161 case 2:
162 *((double*)param) = inst->blurcxx . . $ bgsubtract0r_instance#blur.;
163 break;
164 }
165 }
166
167 #define MAXcxx . . $ `src/filter/bgsubtract0r/bgsubtract0r.c:167:9`!(x,y) ((x) > (y) ? (x) : (y));
168
169 inline static uint8_tcxx . . $ uint8_t# dstcxx . . $ dst(e77b674d7ec06db6).(uint32_tcxx . . $ uint32_t# x, uint32_tcxx . . $ uint32_t# y)
170 {
171 uint8_tcxx . . $ uint8_t# d;
172 uint8_tcxx . . $ uint8_t#* px = (uint8_tcxx . . $ uint8_t#*)&x;
173 uint8_tcxx . . $ uint8_t#* py = (uint8_tcxx . . $ uint8_t#*)&y;
174
175 d = abscxx . . $ abs(7864480464b09eea).(px[0]-py[0]);
176 d = MAXcxx . . $ `src/filter/bgsubtract0r/bgsubtract0r.c:167:9`!(d, abs(px[1]-py[1]));
177 d = MAXcxx . . $ `src/filter/bgsubtract0r/bgsubtract0r.c:167:9`!(d, abs(px[2]-py[2]));
178
179 return d;
180 }
181
182 void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe)
183 {
184 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
185 bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#* inst = (bgsubtract0r_instance_tcxx . . $ bgsubtract0r_instance_t#*)instance;
186 unsigned int width = inst->widthcxx . . $ bgsubtract0r_instance#width.;
187 unsigned int height = inst->heightcxx . . $ bgsubtract0r_instance#height.;
188 unsigned int len = width * height;
189 uint8_tcxx . . $ uint8_t# *mask = inst->maskcxx . . $ bgsubtract0r_instance#mask.;
190 int blur = inst->blurcxx . . $ bgsubtract0r_instance#blur.;
191 int i;
192 int j;
193 int n;
194 uint8_tcxx . . $ uint8_t#* pi;
195 uint8_tcxx . . $ uint8_t#* po;
196
197 if (!inst->referencecxx . . $ bgsubtract0r_instance#reference.)
198 {
199 int blen = sizeof(uint32_tcxx . . $ uint32_t#)*len;
200 inst->referencecxx . . $ bgsubtract0r_instance#reference. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(blen);
201 memmovecxx . . $ memmove(cc817cb175f282d).(inst->referencecxx . . $ bgsubtract0r_instance#reference., inframe, blen);
202 memsetcxx . . $ memset(d1de1e4fb215f1c6).(mask, 0, sizeof(uint8_tcxx . . $ uint8_t#)*len);
203 }
204 else
205 {
206 for (i=0; i<len; i++)
207 mask[i] = (dstcxx . . $ dst(e77b674d7ec06db6).(inst->referencecxx . . $ bgsubtract0r_instance#reference.[i], inframe[i]) > inst->thresholdcxx . . $ bgsubtract0r_instance#threshold.) ? 0xff : 0;
208 }
209
210 /* Clean up the mask. */
211 if (inst->denoisecxx . . $ bgsubtract0r_instance#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_tcxx . . $ uint8_t#*)&inframe[i];
230 po = (uint8_tcxx . . $ 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_tcxx . . $ uint8_t#*)&outframe[width*j+i];
261 po[3] = a;
262 }
263 }
264 }