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 图片: