21#include <gtest/gtest.h>
35 size_t width, height, components, bytesPerComponent;
36 std::vector<double> samples;
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) {}
41 double fullScale()
const
43 return bytesPerComponent == 1 ? 255.0 : 65535.0;
47 void addDisk(
double cx,
double cy,
double radius,
double brightness)
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++)
55 for (
int x = x0; x <= x1; x++)
58 for (
int sy = 0; sy < 4; sy++)
59 for (
int sx = 0; sx < 4; sx++)
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);
65 for (
size_t c = 0; c < components; c++)
66 samples[(y * width + x) * components + c] += brightness * fullScale() * inside / 16.0;
71 void addNoise(
double sky,
double sigma,
unsigned seed = 42)
73 std::mt19937 rng(seed);
74 std::normal_distribution<double> noise(sky * fullScale(), sigma * fullScale());
75 for (
double &sample : samples)
80 std::vector<double> bayer()
const
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++)
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;
92 std::vector<uint8_t> encode(
const std::vector<double> &values)
const
94 std::vector<uint8_t>
buffer(values.size() * bytesPerComponent);
95 for (
size_t i = 0; i < values.size(); i++)
97 const double value = std::min(fullScale(), std::max(0.0, std::round(values[i])));
98 if (bytesPerComponent == 1)
101 reinterpret_cast<uint16_t *
>(
buffer.data())[i] =
static_cast<uint16_t
>(
value);
108 const std::vector<uint8_t>
buffer = encode(samples);
122 measurement.
valid =
true;
125 measurement.
area = area;
134 for (
double cx : {200.0, 321.25, 400.6})
136 for (
double cy : {150.0, 240.5, 301.8})
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);
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);
164 SyntheticFrame frame(640, 480);
165 frame.addDisk(320, 240, 50, 0.8);
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);
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());
182 image.
data = buffer.data();
183 image.
width = frame.width;
184 image.
height = frame.height;
187 ASSERT_TRUE(measurement.valid);
188 EXPECT_NEAR(measurement.x, 611.4, 0.5);
189 EXPECT_NEAR(measurement.y, 500.2, 0.5);
194 SyntheticFrame frame(640, 480);
195 EXPECT_FALSE(frame.measure().valid);
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);
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);
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);
225 SyntheticFrame frame(640, 480);
226 frame.addDisk(320, 240, 50, 0.8);
227 const double area = frame.measure().area;
229 EXPECT_GT(frame.measure(area).quality, 0.95);
231 EXPECT_LT(frame.measure(area * 4).quality, 0.05);
240 EXPECT_FALSE(tracker.
hasLock());
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);
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));
259 EXPECT_TRUE(report.
valid);
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);
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));
272 EXPECT_DOUBLE_EQ(report.
offsetX, 0);
283 EXPECT_TRUE(report.
valid);
284 EXPECT_DOUBLE_EQ(report.
offsetX, 2);
285 EXPECT_DOUBLE_EQ(report.
quality, 0.5);
296 EXPECT_FALSE(report.
valid);
297 EXPECT_DOUBLE_EQ(report.
quality, 0);
299 EXPECT_TRUE(tracker.
hasLock());
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);
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...
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.
bool addMeasurement(const Measurement &measurement, double time, double window, Report &report)
addMeasurement Add a measurement to the averaging window.
std::vector< uint8_t > buffer
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)