作为我现在正在工作的项目的一部分,我需要以类似于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");
}
答案 0 :(得分:0)
更新答案
你完全错了,马克!您可以像这样拍摄情节快照。
#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
你会得到这个: