Debian Codegraph @

src/filter/autothresh0ld/autothresh0ld.c

raw symbols SCIP
1 /**
2 * (c) Copyright 2025 Cynthia <[email protected]>
3 *
4 * This is based on Otsu's algorithm to segment an image into foreground
5 * or background based on the shape of the histogram. Instead of doing a
6 * hard threshold, we use the threshold obtained from Otsu's algorithm
7 * as the base for a sigmoidal transfer with a high slope; this produces
8 * a more eye-soothing threshold effect as seen in ImageMagick.
9 *
10 * This has the added benefit that, whereas the sigmoidal filter's base
11 * requires manual tuning, here it is determined algorithmically and thus
12 * can adapt on a frame-to-frame basis.
13 *
14 * This file is a Frei0r plugin.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <math.h>
34
35 #include "frei0r.h"
36 #include "frei0r/math.h"
37
38 #include "variance.h"
39
40 typedef struct {
41 unsigned int width, height;
42 float slope;
43
44 uint8_t lut[256][256];
45 uint8_t *lumaframe;
46 } s0ft0tsu_t;
47
48 static void gen_sigmoid_lut (uint8_t lut[256][256], float slope)
49 {
50 float k = expf(slope) / 255.0;
51
52 for (int j = 0; j < 256; ++j)
53 for (int i = 0; i < 256; ++i)
54 lut[j][i] = CLAMP (255.0 / (1.0 + expf(-k * (i - j))), 0, 255);
55 }
56
57 int f0r_init()
58 {
59 return 0;
60 }
61
62 void f0r_deinit() {}
63
64 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
65 {
66 s0ft0tsu_t* s = calloc(1, sizeof(s0ft0tsu_t));
67
68 s->width = width;
69 s->height = height;
70 s->slope = 4.0;
71 s->lumaframe = malloc(s->width * s->height);
72
73 gen_sigmoid_lut(s->lut, s->slope);
74
75 return s;
76 }
77
78 void f0r_get_plugin_info(f0r_plugin_info_t* info)
79 {
80 info->name = "autothreshold";
81 info->author = "Cynthia";
82 info->explanation = "Automatically threshold moving pictures";
83 info->major_version = 0;
84 info->minor_version = 1;
85 info->frei0r_version = FREI0R_MAJOR_VERSION;
86 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
87 info->color_model = F0R_COLOR_MODEL_RGBA8888;
88 info->num_params = 1;
89 }
90
91 void f0r_get_param_info(f0r_param_info_t *info, int index)
92 {
93 switch (index)
94 {
95 case 0:
96 info->name = "Slope";
97 info->explanation = "Slope of sigmoidal transfer";
98 info->type = F0R_PARAM_DOUBLE;
99 break;
100 }
101 }
102
103 void f0r_get_param_value(f0r_instance_t inst, f0r_param_t param, int index)
104 {
105 s0ft0tsu_t* s = inst;
106
107 switch (index)
108 {
109 case 0:
110 *(double*)param = s->slope / 7.0;
111 break;
112 }
113 }
114
115 void f0r_set_param_value(f0r_instance_t inst, f0r_param_t param, int index)
116 {
117 s0ft0tsu_t* s = inst;
118
119 switch (index)
120 {
121 case 0:
122 s->slope = *(double*)param * 7.0;
123
124 gen_sigmoid_lut(s->lut, s->slope);
125 break;
126 }
127 }
128
129 void f0r_update(f0r_instance_t inst, double time,
130 const uint32_t* inframe, uint32_t* outframe)
131 {
132 s0ft0tsu_t *s = inst;
133
134 uint8_t *src = (uint8_t*)inframe;
135 uint8_t *dst = s->lumaframe;
136 uint8_t r, g, b, luma;
137 size_t len = s->width * s->height;
138
139 // Normalised histogram (L = 256)
140 float hist[256] = {0};
141
142 for (int i = 0; i < len; ++i)
143 {
144 r = *src++;
145 g = *src++;
146 b = *src++;
147 src++; // Ignore alpha
148
149 luma = CLAMP(0.299 * r + 0.587 * g + 0.114 * b, 0, 255);
150 *dst++ = luma;
151
152 ++hist[luma]; // Add to histogram
153 }
154
155 // Normalise histogram; becomes a probability distribution
156 for (int i = 0; i < 256; ++i)
157 hist[i] /= len;
158
159 uint8_t thresh = 0.0;
160 float max_var = 0.0; // Maximum inter-class variance.
161
162 for (int i = 1; i < 256; ++i)
163 {
164 float var = find_variance(hist, i);
165 if (var > max_var)
166 {
167 thresh = i;
168 max_var = var;
169 }
170 }
171
172 src = (uint8_t*)s->lumaframe; // Replenish
173 dst = (uint8_t*)outframe;
174
175 for (int i = 0; i < len; ++i)
176 {
177 // Select the appropriate transfer
178 uint8_t* lut = s->lut[thresh];
179
180 luma = lut[*src++];
181
182 *dst++ = luma;
183 *dst++ = luma;
184 *dst++ = luma;
185 *dst++ = 0xFF; // TODO Copy alpha
186 }
187 }
188
189 void f0r_destruct(f0r_instance_t inst)
190 {
191 s0ft0tsu_t* s = inst;
192 free(s->lumaframe);
193 free(s);
194 }