【发布时间】: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");
}
【问题讨论】: