您的位置:首页 > 其它

使用QWT实现折线统计图

2016-06-04 17:15 281 查看
QWT,全称是Qt Widgets for Technical Applications,是一个基于LGPL版权协议的开源项目, 可生成各种统计图

最近两天对QWT有一个简单的学习,不敢说完全掌握了,但还是完成了简单的统计图绘制,在这里做个总结,方便自己以后浏览
一个简单的实例,其基于VTK获取图像直方图的数据,然后由QWT负责显示折线图
大概用到的类
class vtkImageAccumulate;//获取直方图
class QwtPlot;//统计图容器 在上面添加画布 折线等元素
class QwtPlotCurve;//画布 负责显示折线图
class QwtPlotGrid;//折线图背景网格
class QwtPlotPicker;//折线图坐标点拾取
class QwtPlotMagnifier;//缩小放大折线图
class QwtPlotPanner;//平移折线图
统计直方图初始化:
void HistDialog::InitCanvas()
{
//![1]拖拽
panner=new QwtPlotPanner(ui->m_plot->canvas());
panner->setAxisEnabled(QwtPlot::yLeft,false);
panner->setMouseButton(Qt::RightButton);
//[1]
//PlotMagnifier 缩放器
magnifier=new QwtPlotMagnifier(ui->m_plot->canvas());
magnifier->setAxisEnabled(QwtPlot::yLeft,false);
//picker
picker=new QwtPlotPicker(QwtPlot::xBottom,QwtPlot::yLeft,
QwtPlotPicker::CrossRubberBand,
QwtPlotPicker::AlwaysOn,
ui->m_plot->canvas());
picker->setStateMachine(new QwtPickerDragPointMachine()); //拖拽点起作用
picker->setRubberBandPen(QPen(QColor(Qt::red)));
picker->setTrackerPen(QPen(QColor(Qt::blue)));

//canvas background
ui->m_plot->setAutoFillBackground(true);
ui->m_plot->canvas()->setPalette(QPalette(QColor(Qt::white)));

grid->enableXMin(true);
grid->setMajorPen(QPen(Qt::black, 0, Qt::DotLine));//大格子
grid->setMinorPen(QPen(Qt::gray, 0 , Qt::DotLine));//大格子里的小格子
grid->attach(ui->m_plot);

//这个会根据画板中的图在RightLegend显示一个图例
ui->m_plot->insertLegend(new QwtLegend(),QwtPlot::RightLegend);

InitPlotCurve();
InitAxes();
}
void HistDialog::InitPlotCurve()
{
//R Component curve
Rcurve->setRenderHint(QwtPlotItem::RenderAntialiased);
Rcurve->setLegendAttribute(QwtPlotCurve::LegendShowLine);
Rcurve->setPen(QPen(Qt::red));
Rcurve->attach(ui->m_plot);
Rcurve->hide();
//G Component curve
Gcurve->setRenderHint(QwtPlotItem::RenderAntialiased);
Gcurve->setLegendAttribute(QwtPlotCurve::LegendShowLine);
Gcurve->setPen(QPen(Qt::green));
Gcurve->attach(ui->m_plot);
Gcurve->hide();
//B Component curve
Bcurve->setRenderHint(QwtPlotItem::RenderAntialiased);
Bcurve->setLegendAttribute(QwtPlotCurve::LegendShowLine);
Bcurve->setPen(QPen(Qt::blue));
Bcurve->attach(ui->m_plot);
Bcurve->hide();
}
void HistDialog::InitAxes()
{
ui->m_plot->setAxisTitle(QwtPlot::xBottom, tr("Intensity"));
ui->m_plot->setAxisAutoScale(QwtPlot::xBottom,true);
ui->m_plot->setAxisTitle(QwtPlot::yLeft, tr("Frequency"));
ui->m_plot->setAxisAutoScale(QwtPlot::yLeft,true);
}获取直方图数据
void HistDialog::Update()
{
//Init variable
int nComp=Image->GetNumberOfScalarComponents();
double xrange[2];
hist.clear();
hist.resize(nComp);//resize QVector
for(int i=0;i<nComp;++i)
{
//get image components
vtkSmartPointer<vtkImageExtractComponents> extract=
vtkSmartPointer<vtkImageExtractComponents>::New();
extract->SetInputData(Image);
extract->SetComponents(i);
extract->Update();

extract->GetOutput()->GetScalarRange(xrange);
int extent=static_cast<int>(xrange[1])-
static_cast<int>(xrange[0]);

//get image histogram
vtkSmartPointer<vtkImageAccumulate> histogram=
vtkSmartPointer<vtkImageAccumulate>::New();
histogram->SetInputData(extract->GetOutput());
histogram->SetComponentExtent(0,extent,0,0,0,0);
histogram->SetComponentOrigin(xrange[0],0,0);
histogram->SetComponentSpacing(1,0,0);
histogram->SetIgnoreZero(true);//ignore 0 pixel value
histogram->Update();

//storage histogram to QVector container
hist[i].clear();
hist[i].resize(extent);
for(int j=xrange[0]+1;j<=xrange[1];++j)
{
const double Frequency =histogram->
GetOutput()->GetScalarComponentAsDouble(j,0,0,0);
hist[i]<<QPointF(j,Frequency);
// qDebug()<<intensity;
}
}
Rcurve->setSamples(hist[0]);
Gcurve->setSamples(hist[1]);
Bcurve->setSamples(hist[2]);
Rcurve->show();
Gcurve->show();
Bcurve->show();
ui->m_plot->replot();

ui->minEdit->setText(QVariant(xrange[0]).toString());
ui->maxEdit->setText(QVariant(xrange[1]).toString());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: