第一步:在类的构造函数中创建画布
image = QImage(500,300, QImage::Format_RGB32); //画布的初始化大小设为470*401,使用32位颜色
QColor backColor = qRgb(255, 255, 255); //画布初始化背景色使用白色
image.fill(backColor);//对画布进行填充
第二步:打开操作进行直方图绘制
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
QString fileName = QFileDialog::getOpenFileName(this, tr("Pick a image file to open..."), QDir::currentPath(), tr("jpg(*.jpg);;tiff(*.tif);;img(*.img);;All files(*.*)"));
if (!fileName.isNull())//如果图像不为空,读取该影像
{
poDataset = (GDALDataset*)GDALOpen(fileName.toLocal8Bit().data(), GA_ReadOnly);
if (poDataset == NULL)
{
QMessageBox::critical(this, tr("Error!"), tr("Can not open file %1").arg(path));
return;
}
GDALRasterBand* outputRasterBand = poDataset->GetRasterBand(1);
double dMinMax[2] = { -1.0, 1.0 };//计算最大最小值
outputRasterBand->ComputeRasterMinMax(FALSE, dMinMax);
GUIntBig panHistogram[1000] = { 0 };
outputRasterBand->GetHistogram(dMinMax[0] - 0.5, dMinMax[1] + 0.5, 1000, panHistogram, TRUE, FALSE, NULL, NULL);//进行直方图统计
Paint(panHistogram,dMinMax[0],dMinMax[1]);//开始绘图,这是一个函数,需要自己写出来,网上能搜到
GDALClose(poDataset);//一定要释放这片内存
}
结果显示: