aboutsummaryrefslogtreecommitdiffstats
path: root/src/camorama-filter-reichardt.c
blob: 4c2ab194276c3c94da0bca919167fd8073ec5516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This file is part of camorama
 *
 * AUTHORS
 *     Sven Herzberg  <herzi@gnome-de.org>
 *
 * Copyright (C) 2006  Sven Herzberg <herzi@gnome-de.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

/*

  Hassenstein-Reichardt movement detection filter written by Adrian Bowyer

  12 September 2016

  http://adrianbowyer.com
  https://reprapltd.com

  For an introduction to how this works in biological vision
  (fly's eyes and yours) see:

  https://en.wikipedia.org/wiki/Motion_sensing_in_vision#The_Reichardt-Hassenstein_model


*/

#include <math.h>               // We just need one square root
#include "filter.h"

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <glib/gi18n.h>

#include <stdio.h>
#include <stdlib.h>

/* GType stuff for CamoramaFilterReichardt */
typedef struct _CamoramaFilter CamoramaFilterReichardt;
typedef struct _CamoramaFilterClass CamoramaFilterReichardtClass;

G_DEFINE_TYPE(CamoramaFilterReichardt, camorama_filter_reichardt,
              CAMORAMA_TYPE_FILTER);

static void camorama_filter_reichardt_init(CamoramaFilterReichardt *self)
{
}

static int count = 0;

static gint oldWidth = -1;
static gint oldHeight = -1;
static gint oldDepth = -1;

// Arrays for holding processed image memories

static long *lastSignal = 0;
static long *lastHigh = 0;
static long *lastHighThenLow = 0;
static long *output = 0;

// Low and high pass filter time constants

static int lowPassTC = 1;
static int highPassTC = 5;

// Set to 1 to print debugging info on standard output

char debug = 0;

//-----------------------------------------------------------------------------

// When we start, or when the image size changes, we need to malloc
// some space to put the frame to frame calculations in.

static void MaybeNewMemory(gint width, gint height, gint depth)
{
    long needed, i, memory;

    if (width == oldWidth && height == oldHeight && depth == oldDepth)
        return;

    if (lastSignal)
        free(lastSignal);
    if (lastHigh)
        free(lastHigh);
    if (lastHighThenLow)
        free(lastHighThenLow);
    if (output)
        free(output);

    if (debug)
        printf("\n\nwidth: %d, height: %d, depth: %d\n\n",
               width, height, depth);

    needed = 5 + (width + 1) * (height + 1);
    memory = needed * sizeof(long);
    lastSignal = (long *)malloc(memory);
    if (!lastSignal) {
        printf("ERROR: Cannot malloc image memory for lastSignal\n");
        return;
    }
    lastHigh = (long *)malloc(memory);
    if (!lastHigh) {
        printf("ERROR: Cannot malloc image memory for lastHigh\n");
        return;
    }
    lastHighThenLow = (long *)malloc(memory);
    if (!lastHighThenLow) {
        printf("ERROR: Cannot malloc image memory for lastHighThenLow\n");
        return;
    }
    output = (long *)malloc(memory);
    if (!output) {
        printf("ERROR: Cannot malloc image memory for output\n");
        return;
    }
    for (i = 0; i < needed; i++) {
        lastSignal[i] = 127;
        lastHigh[i] = 127;
        lastHighThenLow[i] = 127;
    }

    oldWidth = width;
    oldHeight = height;
    oldDepth = depth;
}

//----------------------------------------------------------------------------

// This is the function that implements the filter

static void camorama_filter_reichardt_filter(void *filter, guchar *image,
                                             gint width, gint height,
                                             gint depth)
{
    gint x, y, z, row_length, row, column, thisPixel, thisXY, thatXY;

    long signal, thisHigh, thisHighThenLow, thatHigh, thatHighThenLow;
    long thatSignal, newValue, max, min, scale, mean, var;

    MaybeNewMemory(width, height, depth);

    max = LONG_MIN;
    min = LONG_MAX;
    thatSignal = 0;
    mean = 0;
    var = 0;

    // Run through each pixel doing the filter calculation with the one
    // immediately to its left.  Note that this means that the extreme
    // left (x=0) column of pixels is not filtered.

    row_length = width * depth;
    for (y = 0; y < height; y++) {
        row = y * row_length;
        for (x = 1; x < width; x++) {
            column = x * depth;
            thisPixel = row + column;
            thisXY = y * width + x;
            thatXY = thisXY - 1;

            // Go from colour to grey

            signal = 0;
            for (z = 0; z < depth; z++)
                    signal += image[thisPixel + z];
            signal = signal / depth;

            // Apply Reichardt

            thisHigh = (highPassTC * (lastHigh[thisXY] + signal - lastSignal[thisXY])) / (highPassTC + 1);
            thisHighThenLow = (thisHigh + lastHighThenLow[thisXY] * lowPassTC) / (lowPassTC + 1);
            thatHigh = (highPassTC * (lastHigh[thatXY] + thatSignal - lastSignal[thatXY])) / (highPassTC + 1);
            thatHighThenLow = (thatHigh + lastHighThenLow[thatXY] * lowPassTC) / (lowPassTC + 1);

            newValue = thisHighThenLow * thatHigh - thatHighThenLow * thisHigh;
            output[thisXY] = newValue;

        if (newValue > max)
                max = newValue;
        if (newValue < min)
                min = newValue;

            mean += newValue;
            var += newValue * newValue;

            // Remember for next time

            thatSignal = signal;
            lastHigh[thisXY] = thisHigh;
            lastHighThenLow[thisXY] = thisHighThenLow;
            lastSignal[thisXY] = signal;
        }
    }

    mean = mean / (height * width);
    var = var / (height * width) - mean * mean;
    var = (long)(sqrt((double)var));

    // If debugging, print stats every 50 frames

    count++;
    if (debug && !(count % 50))
        printf("\nmax: %ld, min: %ld mean: %ld, sd: %ld\n", max, min, mean,
               var);

    // If anything's going on (var > 10, say) let's see it
    // This scales the filtered output to the range 0-255
    // The range scaled is four standard deviations (i.e. mean +/- 2 SD).
    // Anything outside that range is clipped to 0 or 255.

    if (var > 10) {
        /*
         * NB - if you are just concerned to detect movement
         * (wildlife camera trap, burglar alarm etc) put the call to start
         * recording (or phone the police) in at this point.
         */
        scale = 4 * var;

        for (y = 0; y < height; y++) {
            row = y * row_length;
            for (z = 0; z < depth; z++)
                image[row + z] = 0;
            for (x = 1; x < width; x++) {
                column = x * depth;
                thisPixel = row + column;
                thisXY = y * width + x;
                signal = output[thisXY];
                signal = ((2 * var + signal - mean) * 255) / scale;
                if (signal < 0)
                    signal = 0;
                if (signal > 255)
                    signal = 255;
                for (z = 0; z < depth; z++)
                    image[thisPixel + z] = (guchar) signal;
            }
        }
    } else {
        for (y = 0; y < height; y++) {
            row = y * row_length;
            for (x = 0; x < width; x++) {
                column = x * depth;
                thisPixel = row + column;
                for (z = 0; z < depth; z++) {
                    image[thisPixel + z] = 127;
                }
            }
        }
    }
}

static void
camorama_filter_reichardt_class_init(CamoramaFilterReichardtClass *
                                     self_class)
{
    self_class->filter = camorama_filter_reichardt_filter;
    // TRANSLATORS: This is a noun
    self_class->name = _("Reichardt");
}

Privacy Policy