|
|
|
@ -242,13 +242,16 @@ static void trim_tail_runs(std::vector<uint8_t>& image, int w, int h,
|
|
|
|
// ==================== 点集 → 图像 ====================
|
|
|
|
// ==================== 点集 → 图像 ====================
|
|
|
|
std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
double* area_out,
|
|
|
|
double* area_out,
|
|
|
|
int* out_w, int* out_h)
|
|
|
|
int* out_w, int* out_h,
|
|
|
|
|
|
|
|
double* z_min_out,
|
|
|
|
|
|
|
|
const double* z_min_forced)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const int MIN_DIM = 8; // 最小尺寸保护
|
|
|
|
const int MIN_DIM = 8; // 最小尺寸保护
|
|
|
|
const int MAX_DIM = 20000; // 单边最大尺寸保护(防止z过大导致内存爆炸)
|
|
|
|
const int MAX_DIM = 20000; // 单边最大尺寸保护(防止z过大导致内存爆炸)
|
|
|
|
if (area_out) *area_out = 0.0;
|
|
|
|
if (area_out) *area_out = 0.0;
|
|
|
|
if (out_w) *out_w = 0;
|
|
|
|
if (out_w) *out_w = 0;
|
|
|
|
if (out_h) *out_h = 0;
|
|
|
|
if (out_h) *out_h = 0;
|
|
|
|
|
|
|
|
if (z_min_out) *z_min_out = 0.0;
|
|
|
|
if (points.empty()) return std::vector<uint8_t>((size_t)MIN_DIM * MIN_DIM, 0);
|
|
|
|
if (points.empty()) return std::vector<uint8_t>((size_t)MIN_DIM * MIN_DIM, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 宽: radar_cfg().filter.polygon 多边形 X 最大最小值直接构成 (1mm=1px)
|
|
|
|
// 宽: radar_cfg().filter.polygon 多边形 X 最大最小值直接构成 (1mm=1px)
|
|
|
|
@ -266,13 +269,25 @@ std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
double spanX = polyXMax - polyXMin;
|
|
|
|
double spanX = polyXMax - polyXMin;
|
|
|
|
if (spanX <= 0.0) spanX = 1.0;
|
|
|
|
if (spanX <= 0.0) spanX = 1.0;
|
|
|
|
|
|
|
|
|
|
|
|
// 高: z 最大值直接构成 (Z=运动方向, 从0开始递增, 1单位=1px)
|
|
|
|
// 高: z 轴有点范围 z_max - z_min (Z=运动方向, 1单位=1px)
|
|
|
|
double maxZ = 0.0;
|
|
|
|
// 只保留 z 轴有点的部分, 裁掉下方的空白区域。
|
|
|
|
|
|
|
|
// z_min_forced 非空时以指定值为起点(layer 图与全点图共用同一 z 范围对齐)。
|
|
|
|
|
|
|
|
double minZ = 0.0;
|
|
|
|
|
|
|
|
if (z_min_forced)
|
|
|
|
|
|
|
|
minZ = *z_min_forced;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
minZ = points.front().z_val;
|
|
|
|
|
|
|
|
for (const auto& p : points) minZ = std::min(minZ, p.z_val);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
double maxZ = minZ;
|
|
|
|
for (const auto& p : points) maxZ = std::max(maxZ, p.z_val);
|
|
|
|
for (const auto& p : points) maxZ = std::max(maxZ, p.z_val);
|
|
|
|
if (maxZ <= 0.0) maxZ = 1.0;
|
|
|
|
double spanZ = maxZ - minZ;
|
|
|
|
|
|
|
|
if (spanZ <= 0.0) spanZ = 1.0;
|
|
|
|
|
|
|
|
if (z_min_out) *z_min_out = minZ;
|
|
|
|
|
|
|
|
|
|
|
|
int imageWidth = (int)std::lround(spanX);
|
|
|
|
int imageWidth = (int)std::lround(spanX);
|
|
|
|
int imageHeight = (int)std::lround(maxZ);
|
|
|
|
int imageHeight = (int)std::lround(spanZ);
|
|
|
|
if (imageWidth < MIN_DIM) imageWidth = MIN_DIM;
|
|
|
|
if (imageWidth < MIN_DIM) imageWidth = MIN_DIM;
|
|
|
|
if (imageHeight < MIN_DIM) imageHeight = MIN_DIM;
|
|
|
|
if (imageHeight < MIN_DIM) imageHeight = MIN_DIM;
|
|
|
|
if (imageWidth > MAX_DIM) imageWidth = MAX_DIM;
|
|
|
|
if (imageWidth > MAX_DIM) imageWidth = MAX_DIM;
|
|
|
|
@ -281,9 +296,9 @@ std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
if (out_h) *out_h = imageHeight;
|
|
|
|
if (out_h) *out_h = imageHeight;
|
|
|
|
std::vector<uint8_t> image((size_t)imageWidth * imageHeight, 0); // 0=白底
|
|
|
|
std::vector<uint8_t> image((size_t)imageWidth * imageHeight, 0); // 0=白底
|
|
|
|
|
|
|
|
|
|
|
|
// X坐标相对多边形左边界偏移到0;Z从0开始
|
|
|
|
// X坐标相对多边形左边界偏移到0;Z相对有点范围的下边界偏移到0
|
|
|
|
double scaleX = (double)imageWidth / spanX; // ≈1: 宽直接由多边形X跨度构成
|
|
|
|
double scaleX = (double)imageWidth / spanX; // ≈1: 宽直接由多边形X跨度构成
|
|
|
|
double scaleY = (double)imageHeight / maxZ; // ≈1: 高直接由z最大值构成
|
|
|
|
double scaleY = (double)imageHeight / spanZ; // ≈1: 高直接由z有点范围构成
|
|
|
|
double xOffset = -polyXMin;
|
|
|
|
double xOffset = -polyXMin;
|
|
|
|
|
|
|
|
|
|
|
|
// 优化:Step1 先按1x1网格标记点位置(重复点只写一次),
|
|
|
|
// 优化:Step1 先按1x1网格标记点位置(重复点只写一次),
|
|
|
|
@ -295,7 +310,7 @@ std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
for (const auto& p : points)
|
|
|
|
for (const auto& p : points)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int px = (int)((p.x_mm + xOffset) * scaleX);
|
|
|
|
int px = (int)((p.x_mm + xOffset) * scaleX);
|
|
|
|
int py = (int)(p.z_val * scaleY);
|
|
|
|
int py = (int)((p.z_val - minZ) * scaleY);
|
|
|
|
if (px < 0 || px >= imageWidth || py < 0 || py >= imageHeight) continue;
|
|
|
|
if (px < 0 || px >= imageWidth || py < 0 || py >= imageHeight) continue;
|
|
|
|
size_t idx = (size_t)py * imageWidth + px;
|
|
|
|
size_t idx = (size_t)py * imageWidth + px;
|
|
|
|
if (image[idx] == 0) // 未标记过才记录
|
|
|
|
if (image[idx] == 0) // 未标记过才记录
|
|
|
|
@ -333,7 +348,9 @@ std::vector<uint8_t> points_to_image(const std::vector<CountPoint>& points,
|
|
|
|
for (size_t i = 0; i < image.size(); i++)
|
|
|
|
for (size_t i = 0; i < image.size(); i++)
|
|
|
|
if (image[i] != 0) totalPixels++;
|
|
|
|
if (image[i] != 0) totalPixels++;
|
|
|
|
|
|
|
|
|
|
|
|
double pixelToAreaRatio = spanX * maxZ / (double)(imageWidth * imageHeight);
|
|
|
|
// 每像素实际面积: (spanX/宽)×(spanZ/高)。必须用 spanZ(z有点范围)而非 maxZ ——
|
|
|
|
|
|
|
|
// 图像高度已改为 spanZ, 若沿用 maxZ, z 不从 0 开始的数据面积会虚大 maxZ/spanZ 倍
|
|
|
|
|
|
|
|
double pixelToAreaRatio = spanX * spanZ / (double)(imageWidth * imageHeight);
|
|
|
|
if (area_out) *area_out = totalPixels * pixelToAreaRatio;
|
|
|
|
if (area_out) *area_out = totalPixels * pixelToAreaRatio;
|
|
|
|
return image;
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -693,6 +710,10 @@ int calc_item_count(const std::vector<CountPoint>& points, const CountParams& bo
|
|
|
|
if (points.empty() || box.l <= 0 || box.w <= 0 || box.h <= 0 || box.maxPerLayer <= 0)
|
|
|
|
if (points.empty() || box.l <= 0 || box.w <= 0 || box.h <= 0 || box.maxPerLayer <= 0)
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 全点 z 范围下界: layer 图与 count_points.jpg 共用同一 z 范围, 宽高完全一致
|
|
|
|
|
|
|
|
double all_zmin = points.front().z_val;
|
|
|
|
|
|
|
|
for (const auto& p : points) all_zmin = std::min(all_zmin, p.z_val);
|
|
|
|
|
|
|
|
|
|
|
|
const bool use_half_max = (radar_cfg().count.layer_algorithm == "HalfMax");
|
|
|
|
const bool use_half_max = (radar_cfg().count.layer_algorithm == "HalfMax");
|
|
|
|
const bool use_kde_peak = (radar_cfg().count.layer_algorithm == "KdePeak");
|
|
|
|
const bool use_kde_peak = (radar_cfg().count.layer_algorithm == "KdePeak");
|
|
|
|
// 分层容差(三个算法统一): 基准 + 每层递增
|
|
|
|
// 分层容差(三个算法统一): 基准 + 每层递增
|
|
|
|
@ -811,7 +832,8 @@ int calc_item_count(const std::vector<CountPoint>& points, const CountParams& bo
|
|
|
|
{
|
|
|
|
{
|
|
|
|
double area = 0.0;
|
|
|
|
double area = 0.0;
|
|
|
|
int img_w = 0, img_h = 0;
|
|
|
|
int img_w = 0, img_h = 0;
|
|
|
|
std::vector<uint8_t> img = points_to_image(it->second, &area, &img_w, &img_h);
|
|
|
|
std::vector<uint8_t> img = points_to_image(it->second, &area, &img_w, &img_h,
|
|
|
|
|
|
|
|
nullptr, &all_zmin);
|
|
|
|
layer_area[h] = area;
|
|
|
|
layer_area[h] = area;
|
|
|
|
layer_img[h] = std::move(img);
|
|
|
|
layer_img[h] = std::move(img);
|
|
|
|
layer_img_size[h] = std::make_pair(img_w, img_h);
|
|
|
|
layer_img_size[h] = std::make_pair(img_w, img_h);
|
|
|
|
@ -911,7 +933,15 @@ void save_count_files_async(const std::string& radar_id,
|
|
|
|
double all_area = 0.0;
|
|
|
|
double all_area = 0.0;
|
|
|
|
int all_w = 0, all_h = 0;
|
|
|
|
int all_w = 0, all_h = 0;
|
|
|
|
std::vector<uint8_t> all_img = points_to_image(points, &all_area, &all_w, &all_h);
|
|
|
|
std::vector<uint8_t> all_img = points_to_image(points, &all_area, &all_w, &all_h);
|
|
|
|
if (save_jpg_gray(out_dir + "/count_points.jpg", all_w, all_h, all_img))
|
|
|
|
// 全黑图(所有点被过滤光/点集为空)不落盘 —— 与 count_layer.jpg 行为一致:
|
|
|
|
|
|
|
|
// 没有有效内容就不生成, 而不是存一张黑图。
|
|
|
|
|
|
|
|
auto has_ink = [](const std::vector<uint8_t>& img)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (uint8_t v : img) if (v) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (has_ink(all_img) &&
|
|
|
|
|
|
|
|
save_jpg_gray(out_dir + "/count_points.jpg", all_w, all_h, all_img))
|
|
|
|
std::cout << "[count] 点云图已保存: " << out_dir << "/count_points.jpg ("
|
|
|
|
std::cout << "[count] 点云图已保存: " << out_dir << "/count_points.jpg ("
|
|
|
|
<< all_w << "x" << all_h << ", X-Z平面, 面积≈"
|
|
|
|
<< all_w << "x" << all_h << ", X-Z平面, 面积≈"
|
|
|
|
<< std::fixed << std::setprecision(1)
|
|
|
|
<< std::fixed << std::setprecision(1)
|
|
|
|
@ -920,7 +950,8 @@ void save_count_files_async(const std::string& radar_id,
|
|
|
|
// 图片2:被计数层(最高有效层)图像
|
|
|
|
// 图片2:被计数层(最高有效层)图像
|
|
|
|
if (top_lv > 0 && !top_layer_img.empty() && top_img_w > 0 && top_img_h > 0)
|
|
|
|
if (top_lv > 0 && !top_layer_img.empty() && top_img_w > 0 && top_img_h > 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (save_jpg_gray(out_dir + "/count_layer.jpg", top_img_w, top_img_h, top_layer_img))
|
|
|
|
if (has_ink(top_layer_img) &&
|
|
|
|
|
|
|
|
save_jpg_gray(out_dir + "/count_layer.jpg", top_img_w, top_img_h, top_layer_img))
|
|
|
|
std::cout << "[count] 有效层图已保存: " << out_dir << "/count_layer.jpg ("
|
|
|
|
std::cout << "[count] 有效层图已保存: " << out_dir << "/count_layer.jpg ("
|
|
|
|
<< top_img_w << "x" << top_img_h << ", 层" << top_lv
|
|
|
|
<< top_img_w << "x" << top_img_h << ", 层" << top_lv
|
|
|
|
<< ", 面积≈" << std::fixed
|
|
|
|
<< ", 面积≈" << std::fixed
|
|
|
|
|