| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // Copyright (C) 2024 Erik H. Beck, [email protected] |
| 3 | // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html |
| 4 | |
| 5 | /* This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | * Also see: |
| 21 | * |
| 22 | * https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /** This is the source file for the euclid eraser two-input mixer for |
| 27 | frei0r. It uses euclidean distance to remove a static background |
| 28 | from a video. See file ./euclid_eraser.md for more info |
| 29 | */ |
| 30 | |
| 31 | #include <math.h> |
| 32 | #include "frei0r.hpp" |
| 33 | #include "frei0r/math.h" |
| 34 | |
| 35 | #define NBYTES 4 |
| 36 | #define CHANNELS 3 // Actually 4; 0-3 |
| 37 | |
| 38 | double euclidDistance(uint8_t x_r, uint8_t x_g, uint8_t x_b, |
| 39 | uint8_t y_r, uint8_t y_g, uint8_t y_b) |
| 40 | { |
| 41 | //calculating color channel differences for next steps |
| 42 | double red_d = x_r - y_r; |
| 43 | double green_d = x_g - y_g; |
| 44 | double blue_d = x_b - y_b; |
| 45 | |
| 46 | double sq_sum, dist; |
| 47 | |
| 48 | //calculating Euclidean distance |
| 49 | sq_sum = pow(red_d, 2) + pow(green_d, 2) + pow (blue_d, 2); |
| 50 | dist = sqrt(sq_sum); |
| 51 | |
| 52 | return dist; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | class euclid_eraser : public frei0r::mixer2 |
| 57 | { |
| 58 | |
| 59 | public: |
| 60 | euclid_eraser(unsigned int width, unsigned int height) |
| 61 | { |
| 62 | threshold = 5.6; // Default distance threshold value |
| 63 | register_param(threshold, "threshold", "Matching Threshold"); |
| 64 | } |
| 65 | |
| 66 | void update(double time, |
| 67 | uint32_t* out, |
| 68 | const uint32_t* in1, |
| 69 | const uint32_t* in2) |
| 70 | { |
| 71 | |
| 72 | const uint8_t *src1 = reinterpret_cast<const uint8_t*>(in1); //frst track (0) |
| 73 | const uint8_t *src2 = reinterpret_cast<const uint8_t*>(in2); //second trk (1) |
| 74 | uint8_t *dst = reinterpret_cast<uint8_t*>(out); |
| 75 | |
| 76 | double e_dist; |
| 77 | |
| 78 | uint32_t sizeCounter = size; |
| 79 | uint32_t b; |
| 80 | |
| 81 | while (sizeCounter--) |
| 82 | { |
| 83 | |
| 84 | // Loop over rgb |
| 85 | // Copy pixels from src2 to destination |
| 86 | |
| 87 | for (b=0; b<3; b++) |
| 88 | { |
| 89 | dst[b]=src2[b]; |
| 90 | } |
| 91 | e_dist=euclidDistance(src1[0],src1[1],src1[2], |
| 92 | src2[0],src2[1],src2[2]); |
| 93 | |
| 94 | if (e_dist <= euclid_eraser::threshold) { |
| 95 | // Make alpha channel for pixel fully transparent |
| 96 | dst[3]=0; |
| 97 | } |
| 98 | else { |
| 99 | // Make alpha channel for the pixel fully opaque |
| 100 | dst[3]=255; |
| 101 | } |
| 102 | |
| 103 | src1 += NBYTES; |
| 104 | src2 += NBYTES; |
| 105 | dst += NBYTES; |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | private: |
| 110 | double threshold; |
| 111 | |
| 112 | }; |
| 113 | |
| 114 | frei0r::construct<euclid_eraser> plugin("euclid_eraser", |
| 115 | "Erasing backgrounds with euclidean distance", |
| 116 | "Erik H. Beck", |
| 117 | 0,1, |
| 118 | F0R_COLOR_MODEL_RGBA8888); |
| 119 |