Instrument Neutral Distributed Interface INDI 2.2.5
test_frame_tracker.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2026 by Jasem Mutlaq <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "stream/frametracker.h"
20
21#include <gtest/gtest.h>
22
23#include <cmath>
24#include <random>
25#include <vector>
26
28
29namespace
30{
31
32// Synthetic frame with one or more uniformly lit, anti-aliased disks on a dark sky
33struct SyntheticFrame
34{
35 size_t width, height, components, bytesPerComponent;
36 std::vector<double> samples;
37
38 SyntheticFrame(size_t w, size_t h, size_t c = 1, size_t bytes = 1)
39 : width(w), height(h), components(c), bytesPerComponent(bytes), samples(w * h * c, 0) {}
40
41 double fullScale() const
42 {
43 return bytesPerComponent == 1 ? 255.0 : 65535.0;
44 }
45
46 // Adds a disk; brightness is a fraction of full scale. Edges are 4x4 supersampled.
47 void addDisk(double cx, double cy, double radius, double brightness)
48 {
49 const int x0 = std::max(0, static_cast<int>(cx - radius - 2));
50 const int x1 = std::min(static_cast<int>(width) - 1, static_cast<int>(cx + radius + 2));
51 const int y0 = std::max(0, static_cast<int>(cy - radius - 2));
52 const int y1 = std::min(static_cast<int>(height) - 1, static_cast<int>(cy + radius + 2));
53 for (int y = y0; y <= y1; y++)
54 {
55 for (int x = x0; x <= x1; x++)
56 {
57 int inside = 0;
58 for (int sy = 0; sy < 4; sy++)
59 for (int sx = 0; sx < 4; sx++)
60 {
61 const double dx = x + (sx + 0.5) / 4 - 0.5 - cx;
62 const double dy = y + (sy + 0.5) / 4 - 0.5 - cy;
63 inside += (dx * dx + dy * dy <= radius * radius);
64 }
65 for (size_t c = 0; c < components; c++)
66 samples[(y * width + x) * components + c] += brightness * fullScale() * inside / 16.0;
67 }
68 }
69 }
70
71 void addNoise(double sky, double sigma, unsigned seed = 42)
72 {
73 std::mt19937 rng(seed);
74 std::normal_distribution<double> noise(sky * fullScale(), sigma * fullScale());
75 for (double &sample : samples)
76 sample += noise(rng);
77 }
78
79 // Mosaic the frame in place: keep one RGGB channel per pixel, scaled as a typical CFA response
80 std::vector<double> bayer() const
81 {
82 std::vector<double> mosaic(width * height);
83 for (size_t y = 0; y < height; y++)
84 for (size_t x = 0; x < width; x++)
85 {
86 const double response = (y % 2 == 0 && x % 2 == 0) ? 0.6 : (y % 2 == 1 && x % 2 == 1) ? 0.4 : 1.0;
87 mosaic[y * width + x] = samples[(y * width + x) * components] * response;
88 }
89 return mosaic;
90 }
91
92 std::vector<uint8_t> encode(const std::vector<double> &values) const
93 {
94 std::vector<uint8_t> buffer(values.size() * bytesPerComponent);
95 for (size_t i = 0; i < values.size(); i++)
96 {
97 const double value = std::min(fullScale(), std::max(0.0, std::round(values[i])));
98 if (bytesPerComponent == 1)
99 buffer[i] = static_cast<uint8_t>(value);
100 else
101 reinterpret_cast<uint16_t *>(buffer.data())[i] = static_cast<uint16_t>(value);
102 }
103 return buffer;
104 }
105
106 FrameTracker::Measurement measure(double referenceArea = 0) const
107 {
108 const std::vector<uint8_t> buffer = encode(samples);
110 image.data = buffer.data();
111 image.width = width;
112 image.height = height;
113 image.components = components;
114 image.bytesPerComponent = bytesPerComponent;
115 return FrameTracker::measure(image, referenceArea);
116 }
117};
118
119FrameTracker::Measurement validAt(double x, double y, double area = 1000)
120{
121 FrameTracker::Measurement measurement;
122 measurement.valid = true;
123 measurement.x = x;
124 measurement.y = y;
125 measurement.area = area;
126 measurement.quality = 1;
127 return measurement;
128}
129
130}
131
132TEST(FrameTracker, MonoDiskSubPixelPosition)
133{
134 for (double cx : {200.0, 321.25, 400.6})
135 {
136 for (double cy : {150.0, 240.5, 301.8})
137 {
138 SyntheticFrame frame(640, 480);
139 frame.addDisk(cx, cy, 60, 0.8);
140 const auto measurement = frame.measure();
141 ASSERT_TRUE(measurement.valid);
142 EXPECT_NEAR(measurement.x, cx, 0.1) << "cy=" << cy;
143 EXPECT_NEAR(measurement.y, cy, 0.1) << "cx=" << cx;
144 EXPECT_NEAR(measurement.area, M_PI * 60 * 60, M_PI * 60 * 60 * 0.15);
145 EXPECT_GT(measurement.quality, 0.95);
146 }
147 }
148}
149
150TEST(FrameTracker, NoisyRgb16Disk)
151{
152 SyntheticFrame frame(1280, 1024, 3, 2);
153 frame.addDisk(700.3, 480.7, 120, 0.5);
154 frame.addNoise(0.02, 0.005);
155 const auto measurement = frame.measure();
156 ASSERT_TRUE(measurement.valid);
157 EXPECT_NEAR(measurement.x, 700.3, 0.5);
158 EXPECT_NEAR(measurement.y, 480.7, 0.5);
159 EXPECT_GT(measurement.quality, 0.8);
160}
161
162TEST(FrameTracker, IgnoresSmallSatellites)
163{
164 SyntheticFrame frame(640, 480);
165 frame.addDisk(320, 240, 50, 0.8);
166 // Bright moons on one side only would pull a plain centroid towards them
167 frame.addDisk(420, 240, 3, 0.9);
168 frame.addDisk(460, 238, 2, 0.9);
169 const auto measurement = frame.measure();
170 ASSERT_TRUE(measurement.valid);
171 EXPECT_NEAR(measurement.x, 320, 0.2);
172 EXPECT_NEAR(measurement.y, 240, 0.2);
173}
174
175TEST(FrameTracker, BayerMosaic)
176{
177 SyntheticFrame frame(1280, 1024);
178 frame.addDisk(611.4, 500.2, 90, 0.7);
179 const std::vector<uint8_t> buffer = frame.encode(frame.bayer());
180
182 image.data = buffer.data();
183 image.width = frame.width;
184 image.height = frame.height;
185 image.bayer = true;
186 const auto measurement = FrameTracker::measure(image);
187 ASSERT_TRUE(measurement.valid);
188 EXPECT_NEAR(measurement.x, 611.4, 0.5);
189 EXPECT_NEAR(measurement.y, 500.2, 0.5);
190}
191
192TEST(FrameTracker, EmptyFrameIsInvalid)
193{
194 SyntheticFrame frame(640, 480);
195 EXPECT_FALSE(frame.measure().valid);
196}
197
198TEST(FrameTracker, PureNoiseHasNoQuality)
199{
200 SyntheticFrame frame(640, 480, 1, 2);
201 frame.addNoise(0.05, 0.01);
202 const auto measurement = frame.measure();
203 EXPECT_LT(measurement.quality, 0.1);
204}
205
206TEST(FrameTracker, FaintTargetHasLowQuality)
207{
208 SyntheticFrame frame(640, 480, 1, 2);
209 frame.addDisk(320, 240, 50, 0.002);
210 const auto measurement = frame.measure();
211 EXPECT_LT(measurement.quality, 0.2);
212}
213
214TEST(FrameTracker, EdgeContactLowersQuality)
215{
216 SyntheticFrame frame(640, 480);
217 frame.addDisk(20, 240, 60, 0.8);
218 const auto measurement = frame.measure();
219 ASSERT_TRUE(measurement.valid);
220 EXPECT_LE(measurement.quality, 0.5);
221}
222
223TEST(FrameTracker, AreaChangeLowersQuality)
224{
225 SyntheticFrame frame(640, 480);
226 frame.addDisk(320, 240, 50, 0.8);
227 const double area = frame.measure().area;
228
229 EXPECT_GT(frame.measure(area).quality, 0.95);
230 // Same target, but the reference was recorded on something four times larger
231 EXPECT_LT(frame.measure(area * 4).quality, 0.05);
232}
233
234TEST(FrameTracker, FirstValidMeasurementAcquiresLock)
235{
236 FrameTracker tracker;
238
239 EXPECT_FALSE(tracker.addMeasurement(FrameTracker::Measurement(), 0.0, 1.0, report));
240 EXPECT_FALSE(tracker.hasLock());
241
242 EXPECT_FALSE(tracker.addMeasurement(validAt(100, 50, 2000), 0.5, 1.0, report));
243 ASSERT_TRUE(tracker.hasLock());
244 EXPECT_DOUBLE_EQ(tracker.lockX(), 100);
245 EXPECT_DOUBLE_EQ(tracker.lockY(), 50);
246 EXPECT_DOUBLE_EQ(tracker.lockArea(), 2000);
247}
248
249TEST(FrameTracker, AveragesOverWindow)
250{
251 FrameTracker tracker;
252 tracker.setLock(100, 100);
254
255 EXPECT_FALSE(tracker.addMeasurement(validAt(101, 99), 10.0, 1.0, report));
256 EXPECT_FALSE(tracker.addMeasurement(validAt(103, 97), 10.4, 1.0, report));
257 ASSERT_TRUE(tracker.addMeasurement(validAt(105, 95), 11.0, 1.0, report));
258
259 EXPECT_TRUE(report.valid);
260 EXPECT_EQ(report.samples, 3u);
261 EXPECT_DOUBLE_EQ(report.windowStart, 10.0);
262 EXPECT_DOUBLE_EQ(report.x, 103);
263 EXPECT_DOUBLE_EQ(report.y, 97);
264 EXPECT_DOUBLE_EQ(report.offsetX, 3);
265 EXPECT_DOUBLE_EQ(report.offsetY, -3);
266 EXPECT_DOUBLE_EQ(report.quality, 1);
267
268 // The next window starts with the next sample
269 EXPECT_FALSE(tracker.addMeasurement(validAt(100, 100), 11.2, 1.0, report));
270 ASSERT_TRUE(tracker.addMeasurement(validAt(100, 100), 12.2, 1.0, report));
271 EXPECT_DOUBLE_EQ(report.windowStart, 11.2);
272 EXPECT_DOUBLE_EQ(report.offsetX, 0);
273}
274
275TEST(FrameTracker, InvalidSamplesScaleQuality)
276{
277 FrameTracker tracker;
278 tracker.setLock(0, 0);
280
281 tracker.addMeasurement(validAt(2, 2), 0.0, 1.0, report);
282 ASSERT_TRUE(tracker.addMeasurement(FrameTracker::Measurement(), 1.0, 1.0, report));
283 EXPECT_TRUE(report.valid);
284 EXPECT_DOUBLE_EQ(report.offsetX, 2);
285 EXPECT_DOUBLE_EQ(report.quality, 0.5);
286}
287
288TEST(FrameTracker, LostTargetReportsInvalid)
289{
290 FrameTracker tracker;
291 tracker.setLock(0, 0);
293
294 tracker.addMeasurement(FrameTracker::Measurement(), 0.0, 1.0, report);
295 ASSERT_TRUE(tracker.addMeasurement(FrameTracker::Measurement(), 1.0, 1.0, report));
296 EXPECT_FALSE(report.valid);
297 EXPECT_DOUBLE_EQ(report.quality, 0);
298 // The lock survives so tracking resumes when the target comes back
299 EXPECT_TRUE(tracker.hasLock());
300}
301
302TEST(FrameTracker, SetLockRestartsWindow)
303{
304 FrameTracker tracker;
305 tracker.setLock(0, 0);
307
308 tracker.addMeasurement(validAt(50, 50), 0.0, 1.0, report);
309 tracker.setLock(10, 10);
310 EXPECT_FALSE(tracker.addMeasurement(validAt(12, 12), 1.0, 1.0, report));
311 ASSERT_TRUE(tracker.addMeasurement(validAt(12, 12), 2.0, 1.0, report));
312 EXPECT_DOUBLE_EQ(report.offsetX, 2);
313 EXPECT_DOUBLE_EQ(report.offsetY, 2);
314}
static Measurement measure(const Image &image, double referenceArea=0)
measure Find the target in a single frame.
The FrameTracker class measures where a bright extended target (planet, Moon or Sun disk) sits in a v...
bool hasLock() const
double lockArea() const
void setLock(double x, double y)
Lock to an explicit position. Restarts the averaging window.
static Measurement measure(const Image &image, double referenceArea=0)
measure Find the target in a single frame.
double lockY() const
double lockX() const
bool addMeasurement(const Measurement &measurement, double time, double window, Report &report)
addMeasurement Add a measurement to the averaging window.
#define M_PI
Definition dsp.h:52
std::vector< uint8_t > buffer
const uint8_t * data
bool bayer
Raw Bayer mosaic: binning is kept even so each bin averages whole CFA cells.
size_t bytesPerComponent
1 for 8-bit, 2 for 16-bit (native endian) samples
size_t components
1 for mono or Bayer, 3 for interleaved RGB
double quality
0 (unusable) to 1 (clean, well separated disk)
double area
Blob area in frame pixels.
double x
Centroid in frame pixels.
double offsetX
Averaged centroid minus lock point.
double quality
Mean quality of the valid samples, scaled by the fraction of samples that were valid.
double windowStart
Time of the first sample in the window.
bool valid
False if no sample in the window was valid (target lost).
uint32_t samples
Number of samples in the window.
double x
Averaged centroid in frame pixels.
TEST(FrameTracker, MonoDiskSubPixelPosition)