2016-09-09 110 views
1

作爲項目的一部分,我現在正在工作,我需要繪製類似於MATLAB的樣式的矢量。在研究了一些與CIMG相關的可能性之後,這些可能性很容易融入到我的程序中,恰到好處地滿足我的需求。我在C++方面很新,從未使用過Cimg。使用Cimg在C++中繪製矢量

繼我來到這個程序來繪製矢量(本名爲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

更新應答

你是完全錯誤的,馬克!您可以像這樣拍攝情節的快照。

#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。您的圖片看起來像這樣在屏幕上:

enter image description here

所以,作爲一個粗略的估計,如果你做一些修改自己的代碼是這樣的:

#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 

你會得到這樣的:

enter image description here

+0

感謝您的迴應,不幸的是,這不是答案。當我這樣做時,它保存的圖像是1像素的sizeecg,我懷疑它只是圖像形式中的矢量。我想用display_graph函數看到圖像時保存圖像 –

+0

@DiegoFernandoPava你可以上傳你的輸出圖像並分享嗎? –

+0

我剛剛在@MarkSetchell先謝謝您。 –