【问题标题】:plotting a vector in C++ with Cimg使用 Cimg 在 C++ 中绘制向量
【发布时间】:2016-09-09 14:36:54
【问题描述】:

作为我现在正在工作的项目的一部分,我需要以类似于 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");

我在创建的显示窗口中看到的图像正是我所期望的,但是当我尝试使用 bmp 保存图像时:

values.save_bmp("test.bmp");

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

提前谢谢你..

这是我正在尝试做的 MCVE,我希望能够将我在显示窗口中看到的内容保存在 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);
    }

    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");

}

【问题讨论】:

    标签: c++ plot cimg


    【解决方案1】:

    更新答案

    你完全错了,马克!您可以像这样拍摄情节的快照。

    #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
    

    你会得到这个:

    【讨论】:

    • 感谢您的回复,不幸的是,这不是答案。当我这样做时,它保存的图像是 1 像素的 sizeecg,我怀疑这只是图像形式的矢量。我想用 display_graph 函数保存我看到的图像
    • @DiegoFernandoPava 你能上传你的输出图像并分享吗?
    • @DiegoFernandoPava 您能否提供制作 MCVE 所需的所有代码和数据 - stackoverflow.com/help/mcve
    • 非常感谢,昨天我终于能够通过创建一个类似于 display_graph 的函数来做到这一点,但有一个额外的 CImg 对象作为输入,然后将创建的图像存储在那里。这样我就可以控制图表并可以正确保存它。不过,我会尝试快照。 CImg 给我留下了深刻的印象,我希望它有更好的文档。
    • 我同意——它是一个非常强大的工具,但很少有像样的例子来说明如何使用它。如果有足够多的人听说过 StackOverflow 并尝试使用它,也许会在 StackOverflow 上建立一个社区……并像你一样提出问题。无论如何,祝你的项目好运!
    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2017-07-06
    • 2020-10-06
    相关资源
    最近更新 更多