QT中使用GDAL对遥感图像Histrogram读取Qwt插件显示

 网上对遥感图像处理的资源相对较少,最近在做这方面的工作,记录一下自己的心得与积累。

1准备工作

1、QT 5.14.0 ,QtCreator4.11.1 64位安装

       下载:http://download.qt.io/archive/qt/5.14/5.14.0/(默认QtCreator4.11.1 32bit)         QtCreator需要自己下载安装QtCreator4.11.1 64bit。
      下载地址:https://www.filehorse.com/download-qt-creator-64/47260/ 
      为什么要重新安装qtcreator 64位,因为我的编译器是64位的,QWT控件通过64位编译器得到,使用默认32位QtCreator,工具栏不能加载QWT控件的工具。 

2、QWT6.1.4安装

      下载地址 : https://sourceforge.net/projects/qwt/
      qwt编译安装可参考:
      https://blog.csdn.net/qq_40758751/article/details/102602355?
      我习惯将dll放入项目里面,也可将dll设置在环境变量里面,和Opencv相似。网上资料很多,这里就不啰嗦了。 

实现

直接上代码:(主要)

   //加载图片时显示直方图

void myHistogram::gdalhistograms(GDALDataset *gdhist)
{
if(gdhist->GetRasterCount()==1)
{
//接收256个灰度级的值
GUIntBig panHistogram [256];
gdhist->GetRasterBand(1)->GetHistogram(-0.5,255.5,256,panHistogram,false,false,NULL,NULL);
//x坐标
QVector xdata;
//y坐标
QVector ydata;
//将x,y值放入vector中
for(int i=0;i<256;i++)
{
xdata.append((double)i);
ydata.append((double)panHistogram[i]);
}
//new 曲线
QwtPlotCurve * curve=new QwtPlotCurve(“Curve 1”);
//获取最大灰度值,重绘y坐标轴
GUIntBig max=panHistogram[0];
for(int j=0;j<255;j++)
{
if(max<panHistogram[j+1])
{
max=panHistogram[j+1];
}
}
//重设y坐标值
this->setAxisScale(QwtPlot::yLeft,0,max,max/10);
//new画布
QBrush brush(QColor(255,0,0));
curve->setBrush(brush);
//将点绑定在线上
curve->setSamples(xdata,ydata);
//设置划线颜色
curve->setPen(Qt::red,1);
//将线绑定在Qwt控件上
curve->attach(this);
//qwtplot重绘
this->replot();
//显示
this->show();
}
else if(gdhist->GetRasterCount()==3)
{
//接收256个灰度级的值
GUIntBig panHistogram1 [256];
GUIntBig panHistogram2 [256];
GUIntBig panHistogram3 [256];
gdhist->GetRasterBand(1)->GetHistogram(-0.5,255.5,256,panHistogram1,false,false,NULL,NULL);
gdhist->GetRasterBand(2)->GetHistogram(-0.5,255.5,256,panHistogram2,false,false,NULL,NULL);
gdhist->GetRasterBand(3)->GetHistogram(-0.5,255.5,256,panHistogram3,false,false,NULL,NULL);
//x坐标
QVector x1data;
QVector x2data;
QVector x3data;
//y坐标
QVector y1data;
QVector y2data;
QVector y3data;
//将x,y值放入vector中
for(int i=0;i<256;i++)
{
x1data.append((double)i);
y1data.append((double)panHistogram1[i]);
x2data.append((double)i);
y2data.append((double)panHistogram2[i]);
x3data.append((double)i);
y3data.append((double)panHistogram3[i]);
}
//获取最大灰度值,重绘y坐标轴
GUIntBig max1=panHistogram1[0];
GUIntBig max2=panHistogram2[0];
GUIntBig max3=panHistogram3[0];

    for(int j=0;j<255;j++)
    {
        if(max1<panHistogram1[j+1])
           max1=panHistogram1[j+1];
        if(max2<panHistogram2[j+1])
           max2=panHistogram2[j+1];
        if(max3<panHistogram3[j+1])
           max3=panHistogram3[j+1];
    }
    //找到最大值重设y值
    GUIntBig temp=max1;
    if(max1<max2)
    {
        temp=max2;
        if(max2<max3)
        {
           temp=max3;
        }
    }
    else
    {
        if(max1<max3)
        {
            temp=max3;
        }
    }
    //new 曲线
    QwtPlotCurve * curve1=new QwtPlotCurve("Curve 1");
    QwtPlotCurve * curve2=new QwtPlotCurve("Curve 2");
    QwtPlotCurve * curve3=new QwtPlotCurve("Curve 3");
    this->setAxisScale(QwtPlot::yLeft,0,temp,temp/10);
    //将点绑定在线上
    curve1->setSamples(x1data,y1data);
    curve2->setSamples(x2data,y2data);
    curve3->setSamples(x3data,y3data);
    //设置划线颜色
    curve1->setPen(Qt::red,1);
    curve2->setPen(Qt::green,1);
    curve3->setPen(Qt::blue,1);
    //将线绑定在Qwt控件上
    curve1->attach(this);
    curve2->attach(this);
    curve3->attach(this);
    //qwtplot重绘
    this->replot();
    //显示
    this->show();
}

}

插入链接与图片

完整项目在一下链接,切记:.pro文件中的绝对路径是我电脑上的,你的lib、include在哪就将路径修改成自己的。
链接: link.
https://download.csdn.net/download/qq_40828839/12993514
图片:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值