使用 Cimg 在 C++ 中绘制矢量

plotting a vector in C++ with Cimg

作为我现在正在工作的项目的一部分,我需要以类似于 MATLAB 的方式绘制一个矢量。在研究了一些可能性之后,我发现了 CIMG,它似乎很容易放入我的程序中,而且正好适合我的需要。我是 C++ 的新手,之前从未使用过 Cimg。

按照指南中提供的示例之一,我得到了这个程序来绘制矢量(在本例中名为 ecg_r),我的代码如下所示:

// Read command line argument   cimg_usage("Simple plotter of ECG signal");
const char *const formula = cimg_option("-f", "x", "Formula to plot");
const float x0 = cimg_option("-x0", 0.0f, "Minimal X-value");
const float x1 = cimg_option("-x1", 20.0f, "Maximal X-value");
int sizeecg = ecg_r.size();
const int resolution = cimg_option("-r", sizeecg, "Plot resolution");
const unsigned int nresolution = resolution>1 ? resolution : sizeecg;
const unsigned int plot_type = cimg_option("-p", 1, "Plot type");
const unsigned int vertex_type = cimg_option("-v", 1, "Vertex type");

// Create plot data.
CImg<double> values(1, nresolution, 1, 1, 0);

const unsigned int r = nresolution - 1;

for (int i1 = 0; i1 < sizeecg; ++i1)
{
    double xtime = x0 + i1*(x1 - x0) / r;
    values(0, i1) = ecg_r.at(i1);
}  

// Display interactive plot window.
values.display_graph(formula, plot_type, vertex_type, "X-axis", x0, x1, "Y-axis");

我在显示 window 中看到的图像正是我所期待的,但是当我尝试使用以下方法将图像保存为 bmp 格式时:

values.save_bmp("test.bmp");

图像全黑,如何保存在显示功能中看到的图像?我昨天下午花了很多时间浏览文档,但找不到任何线索。

提前谢谢你..

这是我正在尝试做的 MCVE,我希望能够将我在显示器中看到的内容保存在 bmp 中 window。谢谢

#include "CImg.h"
#include <vector>

using namespace cimg_library;

int main(int argc, char** const argv)
{
    cimg_usage("Simple plotter of mathematical formulas");
    const char *const formula = cimg_option("-f", "sin(x)", "Formula to    plot");
    const float x0 = cimg_option("-x0", -5.0f, "Minimal X-value");
    const float x1 = cimg_option("-x1", 5.0f, "Maximal X-value");
    const int resolution = cimg_option("-r", 5000, "Plot resolution");
    const unsigned int nresolution = resolution>1 ? resolution : 5000;
    const unsigned int plot_type = cimg_option("-p", 1, "Plot type");
    const unsigned int vertex_type = cimg_option("-v", 1, "Vertex type");

    // Create plot data.
    CImg<double> values(1, nresolution, 1, 1, 0);

    const unsigned int r = nresolution - 1;

    for (int i1 = 0; i1 < resolution; ++i1)
    {
        double xtime = x0 + i1*(x1 - x0) / r;
        values(0, i1) = sin(xtime);
    }

    CImg<double> values2;
    values2 = values.display_graph(formula, plot_type, vertex_type, "X Axis", x0, x1, "Y Axis");
    values.normalize(0, 255);
    values.save_bmp("test.bmp");

}

更新答案

你完全错了,马克!您可以像这样对剧情进行快照。

#include "CImg.h"
#include <vector>

using namespace cimg_library;

int main(int argc, char** const argv)
{
    cimg_usage("Simple plotter of mathematical formulas");
    const char *const formula = cimg_option("-f", "sin(x)", "Formula to    plot");
    const float x0 = cimg_option("-x0", -5.0f, "Minimal X-value");
    const float x1 = cimg_option("-x1", 5.0f, "Maximal X-value");
    const int resolution = cimg_option("-r", 5000, "Plot resolution");
    const unsigned int nresolution = resolution>1 ? resolution : 5000;
    const unsigned int plot_type = cimg_option("-p", 1, "Plot type");
    const unsigned int vertex_type = cimg_option("-v", 1, "Vertex type");

    // Create plot data.
    CImg<double> values(1, nresolution, 1, 1, 0);

    const unsigned int r = nresolution - 1;

    for (int i1 = 0; i1 < resolution; ++i1)
    {
        double xtime = x0 + i1*(x1 - x0) / r;
        values(0, i1) = sin(xtime);
    }

    CImgDisplay disp;
    CImg<double> values2;
    values.display_graph(disp, plot_type, vertex_type, "X Axis", x0, x1, "Y Axis");
    disp.snapshot(values2);
    values2.save_bmp("result.bmp");
}

原答案

我很高兴有人告诉我我完全错了,如果有人知道得更多,并告诉我们方法,但我不相信 CImg 会给你这样的情节一个你想要的位图文件。

所以,我尝试使用 gnuplot 来完成它。您的图像在屏幕上看起来像这样:

因此,作为一个粗略的近似值,如果您像这样对代码进行一些编辑:

#include "CImg.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace cimg_library;
using namespace std;

int main(int argc, char** const argv)
{
    cimg_usage("Simple plotter of mathematical formulas");
    const char *const formula = cimg_option("-f", "sin(x)", "Formula to    plot");
    const float x0 = cimg_option("-x0", -5.0f, "Minimal X-value");
    const float x1 = cimg_option("-x1", 5.0f, "Maximal X-value");
    const int resolution = cimg_option("-r", 5000, "Plot resolution");
    const unsigned int nresolution = resolution>1 ? resolution : 5000;
    const unsigned int plot_type = cimg_option("-p", 1, "Plot type");
    const unsigned int vertex_type = cimg_option("-v", 1, "Vertex type");

    // Create plot data.
    CImg<double> values(1, nresolution, 1, 1, 0);

    const unsigned int r = nresolution - 1;

    ofstream data;
    data.open("plot.dat");

    for (int i1 = 0; i1 < resolution; ++i1)
    {
        double xtime = x0 + i1*(x1 - x0) / r;
        values(0, i1) = sin(xtime);
        double x=x0 + i1*(x1-x0)/nresolution;
        data << x << " " << values(0,i1) << endl;
    }
    data.close();

    CImg<double> values2;
    values2 = values.display_graph(formula, plot_type, vertex_type, "X Axis", x0, x1, "Y Axis");
    cout << "set terminal png size 900,600 enhanced font \"Helvetica,20\"" << endl;
    cout << "set output 'out.png'" << endl;
    cout << "set xrange [" << x0 << ":" << x1 << "]" << endl;
    cout << "set grid" << endl;
    cout << "set ylabel 'Y Axis'" << endl;
    cout << "set xlabel 'X Axis'" << endl;
    cout << "plot \"plot.dat\" with linespoint lc 4" << endl;
}

然后 运行 像这样:

./yourProgram | gnuplot

你会得到这个: