safesight-edge/tests/test_color_analyzer.cpp

57 lines
1.6 KiB
C++

#include <gtest/gtest.h>
#include <cstdint>
#include <memory>
#include <vector>
#include "frame/frame.h"
#include "../plugins/logic_gate/color_analyzer.h"
namespace rk3588 {
namespace {
Frame MakeRgbFrame(int width, int height, uint8_t top_value, uint8_t bottom_value) {
auto pixels = std::make_shared<std::vector<uint8_t>>(static_cast<size_t>(width * height * 3), 0);
for (int y = 0; y < height; ++y) {
const uint8_t value = (y < (height * 2 / 3)) ? top_value : bottom_value;
for (int x = 0; x < width; ++x) {
const size_t idx = static_cast<size_t>((y * width + x) * 3);
(*pixels)[idx] = value;
(*pixels)[idx + 1] = value;
(*pixels)[idx + 2] = value;
}
}
Frame frame;
frame.width = width;
frame.height = height;
frame.format = PixelFormat::RGB;
frame.stride = width * 3;
frame.data = pixels->data();
frame.data_size = pixels->size();
frame.data_owner = pixels;
return frame;
}
TEST(ColorAnalyzerTest, UsesBottomThirdInsteadOfCenterRegionForBrightness) {
ColorConfig config;
config.method = ColorMethod::RGB;
config.dark_threshold = 80;
config.roi_expand = 1.0f;
config.debug_output = false;
ColorAnalyzer analyzer(config);
Frame frame = MakeRgbFrame(9, 9, 180, 20);
Rect bbox{0.0f, 0.0f, 9.0f, 9.0f};
const ColorResult result = analyzer.Analyze(frame, bbox);
ASSERT_TRUE(result.valid);
EXPECT_TRUE(result.is_dark);
EXPECT_LT(result.brightness, static_cast<float>(config.dark_threshold));
EXPECT_GT(result.dark_ratio, 0.6f);
}
} // namespace
} // namespace rk3588