| 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"cxx . . $ `<file>/include/frei0r.h`/ |
| 36 | #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/ |
| 37 | |
| 38 | #include "variance.h"cxx . . $ `<file>/src/filter/autothresh0ld/variance.h`/ |
| 39 | |
| 40 | typedef structcxx . . $ $anonymous_type_aad3c2a4544995af_0# { |
| 41 | unsigned int widthcxx . . $ $anonymous_type_aad3c2a4544995af_0#width., heightcxx . . $ $anonymous_type_aad3c2a4544995af_0#height.; |
| 42 | float slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope.; |
| 43 | |
| 44 | uint8_tcxx . . $ uint8_t# lutcxx . . $ $anonymous_type_aad3c2a4544995af_0#lut.[256][256]; |
| 45 | uint8_tcxx . . $ uint8_t# *lumaframecxx . . $ $anonymous_type_aad3c2a4544995af_0#lumaframe.; |
| 46 | } s0ft0tsu_tcxx . . $ s0ft0tsu_t#; |
| 47 | |
| 48 | static void gen_sigmoid_lutcxx . . $ gen_sigmoid_lut(e1e036e831037f3a). (uint8_tcxx . . $ uint8_t# lut[256][256], float slope) |
| 49 | { |
| 50 | float k = expfcxx . . $ expf(fbcf3211dfe1bfd1).(slope) / 255.0; |
| 51 | |
| 52 | for (int j = 0; j < 256; ++j) |
| 53 | for (int i = 0; i < 256; ++i) |
| 54 | lut[j][i] = CLAMPcxx . . $ `include/frei0r/math.h:48:9`! (255.0 / (1.0 + expf(-k * (i - j))), 0, 255); |
| 55 | } |
| 56 | |
| 57 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() {} |
| 63 | |
| 64 | f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 65 | { |
| 66 | s0ft0tsu_tcxx . . $ s0ft0tsu_t#* s = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(s0ft0tsu_tcxx . . $ s0ft0tsu_t#)); |
| 67 | |
| 68 | s->widthcxx . . $ $anonymous_type_aad3c2a4544995af_0#width. = width; |
| 69 | s->heightcxx . . $ $anonymous_type_aad3c2a4544995af_0#height. = height; |
| 70 | s->slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope. = 4.0; |
| 71 | s->lumaframecxx . . $ $anonymous_type_aad3c2a4544995af_0#lumaframe. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(s->widthcxx . . $ $anonymous_type_aad3c2a4544995af_0#width. * s->heightcxx . . $ $anonymous_type_aad3c2a4544995af_0#height.); |
| 72 | |
| 73 | gen_sigmoid_lutcxx . . $ gen_sigmoid_lut(e1e036e831037f3a).(s->lutcxx . . $ $anonymous_type_aad3c2a4544995af_0#lut., s->slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope.); |
| 74 | |
| 75 | return s; |
| 76 | } |
| 77 | |
| 78 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* info) |
| 79 | { |
| 80 | info->namecxx . . $ f0r_plugin_info#name. = "autothreshold"; |
| 81 | info->authorcxx . . $ f0r_plugin_info#author. = "Cynthia"; |
| 82 | info->explanationcxx . . $ f0r_plugin_info#explanation. = "Automatically threshold moving pictures"; |
| 83 | info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 84 | info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1; |
| 85 | info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 86 | info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!; |
| 87 | info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!; |
| 88 | info->num_paramscxx . . $ f0r_plugin_info#num_params. = 1; |
| 89 | } |
| 90 | |
| 91 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t# *info, int index) |
| 92 | { |
| 93 | switch (index) |
| 94 | { |
| 95 | case 0: |
| 96 | info->namecxx . . $ f0r_param_info#name. = "Slope"; |
| 97 | info->explanationcxx . . $ f0r_param_info#explanation. = "Slope of sigmoidal transfer"; |
| 98 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# inst, f0r_param_tcxx . . $ f0r_param_t# param, int index) |
| 104 | { |
| 105 | s0ft0tsu_tcxx . . $ s0ft0tsu_t#* s = inst; |
| 106 | |
| 107 | switch (index) |
| 108 | { |
| 109 | case 0: |
| 110 | *(double*)param = s->slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope. / 7.0; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# inst, f0r_param_tcxx . . $ f0r_param_t# param, int index) |
| 116 | { |
| 117 | s0ft0tsu_tcxx . . $ s0ft0tsu_t#* s = inst; |
| 118 | |
| 119 | switch (index) |
| 120 | { |
| 121 | case 0: |
| 122 | s->slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope. = *(double*)param * 7.0; |
| 123 | |
| 124 | gen_sigmoid_lutcxx . . $ gen_sigmoid_lut(e1e036e831037f3a).(s->lutcxx . . $ $anonymous_type_aad3c2a4544995af_0#lut., s->slopecxx . . $ $anonymous_type_aad3c2a4544995af_0#slope.); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# inst, double time, |
| 130 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 131 | { |
| 132 | s0ft0tsu_tcxx . . $ s0ft0tsu_t# *s = inst; |
| 133 | |
| 134 | uint8_tcxx . . $ uint8_t# *src = (uint8_tcxx . . $ uint8_t#*)inframe; |
| 135 | uint8_tcxx . . $ uint8_t# *dst = s->lumaframecxx . . $ $anonymous_type_aad3c2a4544995af_0#lumaframe.; |
| 136 | uint8_tcxx . . $ uint8_t# r, g, b, luma; |
| 137 | size_t len = s->widthcxx . . $ $anonymous_type_aad3c2a4544995af_0#width. * s->heightcxx . . $ $anonymous_type_aad3c2a4544995af_0#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 = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(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_tcxx . . $ 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_variancecxx . . $ find_variance(779de2cc40116283).(hist, i); |
| 165 | if (var > max_var) |
| 166 | { |
| 167 | thresh = i; |
| 168 | max_var = var; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | src = (uint8_tcxx . . $ uint8_t#*)s->lumaframecxx . . $ $anonymous_type_aad3c2a4544995af_0#lumaframe.; // Replenish |
| 173 | dst = (uint8_tcxx . . $ uint8_t#*)outframe; |
| 174 | |
| 175 | for (int i = 0; i < len; ++i) |
| 176 | { |
| 177 | // Select the appropriate transfer |
| 178 | uint8_tcxx . . $ uint8_t#* lut = s->lutcxx . . $ $anonymous_type_aad3c2a4544995af_0#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_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# inst) |
| 190 | { |
| 191 | s0ft0tsu_tcxx . . $ s0ft0tsu_t#* s = inst; |
| 192 | freecxx . . $ free(5d22bdacc48458e8).(s->lumaframecxx . . $ $anonymous_type_aad3c2a4544995af_0#lumaframe.); |
| 193 | freecxx . . $ free(5d22bdacc48458e8).(s); |
| 194 | } |