You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Gnuplot绘制嵌套向量坐标及霍夫变换的技术求助

解决方案:Gnuplot绘制嵌套Vector数据与霍夫变换结果展示

一、绘制非等间距X-Y嵌套Vector数据

你的vector_2d中每个子vector存储(X,Y)数值对,非等间距无需特殊处理,直接用Gnuplot按点绘制即可。以下两种实现方式任选:

方式1:使用gnuplot-iostream库(推荐)

该库可直接对接C++ STL容器,无需手动读写文件。若未集成,可通过vcpkg或手动下载配置:

#include <gnuplot-iostream.h>
#include <vector>
#include <algorithm>

int main() {
    constexpr int WIDTH = 2;
    constexpr int LENGTH = 1280;
    std::vector<std::vector<double>> vector_2d(LENGTH, std::vector<double>(WIDTH, 0));
    // 假设此处已完成数据填充

    Gnuplot gp;
    gp << "set style data points\n";
    gp << "set title 'Non-uniform X-Y Data'\n";
    gp << "plot '-' using 1:2 with points pt 7 ps 0.5\n";

    // 过滤数据中的-300异常值(按需调整)
    std::vector<std::vector<double>> filtered;
    std::copy_if(vector_2d.begin(), vector_2d.end(), std::back_inserter(filtered),
        [](const auto& pair) { return pair[1] != -300.0; });
    gp.send1d(filtered);

    gp << "pause -1\n";
    return 0;
}

方式2:手动写入临时文件调用Gnuplot

无需第三方库,将数据写入文本文件后通过系统命令触发绘图:

#include <fstream>
#include <vector>
#include <cstdlib>

int main() {
    // 初始化并填充vector_2d...

    std::ofstream out("temp_data.txt");
    for (const auto& pair : vector_2d) {
        if (pair[1] != -300.0) { // 过滤无效点
            out << pair[0] << " " << pair[1] << "\n";
        }
    }
    out.close();

    // 调用Gnuplot并保持窗口
    system("gnuplot -persist -e \"set style data points; plot 'temp_data.txt' using 1:2 with points pt 7 ps 0.5\"");
    remove("temp_data.txt"); // 清理临时文件
    return 0;
}

二、霍夫变换结果展示(实时+计算后)

首先需要补全霍夫变换的核心逻辑——统计rho-theta空间的累加器计数,现有代码仅计算了rho值,未完成统计:

#include <vector>
#include <cmath>
#include <algorithm>

int main() {
    // 假设vector_2d、n_theta、q_theta、arrIndex已定义并初始化
    double theta = 0.0;
    float rho, cos_theta, sin_theta;

    // 1. 计算rho的取值范围
    double max_rho = 0.0, min_rho = 0.0;
    for (long i = 0; i < arrIndex; i++) {
        double x = vector_2d[i][0], y = vector_2d[i][1];
        if (y == -300.0) continue;
        double current_rho = std::sqrt(x*x + y*y);
        max_rho = std::max(max_rho, current_rho);
        min_rho = std::min(min_rho, -current_rho);
    }
    int n_rho = 200; // 自定义rho量化步数
    double rho_step = (max_rho - min_rho) / n_rho;

    // 2. 创建霍夫累加器:行对应theta,列对应rho
    std::vector<std::vector<int>> hough_accum(n_theta, std::vector<int>(n_rho, 0));

    // 3. 执行霍夫变换统计
    for (int ti = 0; ti < n_theta; ti++) {
        cos_theta = static_cast<float>(cos(theta * M_PI / 180.0)); // 角度转弧度
        sin_theta = static_cast<float>(sin(theta * M_PI / 180.0));
        
        for (long i = 0; i < arrIndex; i++) {
            double x = vector_2d[i][0], y = vector_2d[i][1];
            if (y == -300.0) continue;
            rho = x * cos_theta + y * sin_theta;
            int rho_bin = static_cast<int>((rho - min_rho) / rho_step);
            if (rho_bin >= 0 && rho_bin < n_rho) { // 确保索引合法
                hough_accum[ti][rho_bin]++;
            }
        }
        theta += q_theta;
    }

    // 后续展示逻辑...
    return 0;
}

1. 计算完成后展示霍夫空间

霍夫空间是theta-rho的二维热力图,用Gnuplot的pm3d或image样式绘制:

用gnuplot-iostream实现

Gnuplot gp;
gp << "set title 'Hough Transform Space'\n";
gp << "set xlabel 'Theta (degrees)'\n";
gp << "set ylabel 'Rho'\n";
gp << "set pm3d map\n";
gp << "set palette defined (0 'blue', 1 'green', 2 'yellow', 3 'red')\n";
gp << "set xrange [0:" << n_theta*q_theta << "]\n";
gp << "set yrange [" << min_rho << ":" << max_rho << "]\n";
gp << "plot '-' matrix with image\n";
gp.send2d(hough_accum);
gp << "pause -1\n";

写入文件后绘制

std::ofstream hough_out("hough_data.txt");
for (const auto& row : hough_accum) {
    for (int cnt : row) {
        hough_out << cnt << " ";
    }
    hough_out << "\n";
}
hough_out.close();

system("gnuplot -persist -e \"set title 'Hough Transform Space'; set xlabel 'Theta'; set ylabel 'Rho'; set pm3d map; set palette defined (0 'blue',1 'green',2 'yellow',3 'red'); plot 'hough_data.txt' matrix with image\"");
remove("hough_data.txt");

2. 实时显示霍夫变换

实时显示难度较高,会因频繁刷新拖慢计算速度,仅推荐用于调试或小数据量场景。核心思路是每计算若干个theta后,刷新Gnuplot窗口:

Gnuplot gp;
gp << "set title 'Real-time Hough Transform'\n";
gp << "set xlabel 'Theta (degrees)'\n";
gp << "set ylabel 'Rho'\n";
gp << "set pm3d map\n";
gp << "set palette defined (0 'blue', 1 'green', 2 'yellow', 3 'red')\n";
gp << "set xrange [0:" << n_theta*q_theta << "]\n";
gp << "set yrange [" << min_rho << ":" << max_rho << "]\n";
gp << "set terminal x11 persist\n";

std::vector<std::vector<int>> temp_accum(n_theta, std::vector<int>(n_rho, 0));
theta = 0.0;

for (int ti = 0; ti < n_theta; ti++) {
    cos_theta = static_cast<float>(cos(theta * M_PI / 180.0));
    sin_theta = static_cast<float>(sin(theta * M_PI / 180.0));
    
    for (long i = 0; i < arrIndex; i++) {
        double x = vector_2d[i][0], y = vector_2d[i][1];
        if (y == -300.0) continue;
        rho = x * cos_theta + y * sin_theta;
        int rho_bin = static_cast<int>((rho - min_rho) / rho_step);
        if (rho_bin >=0 && rho_bin < n_rho) {
            temp_accum[ti][rho_bin]++;
        }
    }

    // 每10个theta刷新一次(可调,减少频率提升性能)
    if (ti % 10 == 0) {
        gp << "replot '-' matrix with image\n";
        gp.send2d(temp_accum);
    }
    theta += q_theta;
}

// 绘制最终完整结果
gp << "replot '-' matrix with image\n";
gp.send2d(temp_accum);
gp << "pause -1\n";

内容的提问来源于stack exchange,提问作者excellent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 23:03:25