单纯备份一下源代码,详细流程参考https://blog.csdn.net/deirjie/article/details/37872743
//open_image.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_open_image.h"
#include "gdal_priv.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QTextCodec>
#include<QWheelEvent>
#include<QStandardItemModel>
#include<QFileInfo>
#include<QImage>
#include<QPixmap>
#include<QGraphicsPixmapItem>
#include<QMatrix>
#include<QScrollBar>
#include <QListView>
class open_image : public QMainWindow
{
Q_OBJECT
public slots:
void readimage();
public:
QSize sizeHint() const;
void CloseCurrentImg();
open_image(QWidget *parent = Q_NULLPTR);
bool m_showColor;
float m_scaleFactor;
QPoint lastEventCursorPos;
protected:
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void ZoomIn()
{
ScaleImg(1.2);
};
void ZoomOut()
{
ScaleImg(0.8);
};
private:
void ShowBand(GDALRasterBand* band);
void ShowImg(QList<GDALRasterBand*>*imgBand);
unsigned char* ImgSketch(float* buffer, GDALRasterBand* currentBand, int size, double noValue);
Ui::open_imageClass ui;
void ScaleImg(double factor)
{
m_scaleFactor *= factor;
QMatrix matrix;
matrix.scale(m_scaleFactor, m_scaleFactor);
ui.image_show->setMatrix(matrix);
};
QStandardItemModel *imgMetaModel;
QStandardItemModel *fileListModel;
GDALDataset *poDataset;
void ShowImgInfor(const QString filename);
void ShowFileList(const QString filename);
};
//open_image.cpp
#include "open_image.h"
open_image::open_image(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
poDataset = NULL;
m_showColor = true;
m_scaleFactor = 1.0;
m_showColor = true;
imgMetaModel = new QStandardItemModel;
imgMetaModel->setColumnCount(2);
fileListModel = new QStandardItemModel;
connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(readimage()));
}
void open_image::readimage(){
QString file = QFileDialog::getOpenFileName(this,
tr("Open Image"), ".",
tr("Open tiff files(*.tiff *.tif)"));
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
poDataset = (GDALDataset*)GDALOpen(file.toStdString().c_str(), GA_ReadOnly);
if (poDataset == NULL)
{
QMessageBox::critical(this, tr("Error!"), tr("Can not open file %1").arg(file));
return;
}
ShowFileList(file);
ShowImgInfor(file);
if (poDataset->GetRasterCount() != 3)
{
int num=poDataset->GetRasterCount();
m_showColor = false;
ShowBand(poDataset->GetRasterBand(1));
}
// 如果图像正好三个波段,则默认以RGB的顺序显示彩色图
else
{
m_showColor = true;
QList<GDALRasterBand*> bandList;
bandList.append(poDataset->GetRasterBand(1));
bandList.append(poDataset->GetRasterBand(2));
bandList.append(poDataset->GetRasterBand(3));
ShowImg(&bandList);
}
GDALClose(poDataset);
}
void open_image::ShowBand(GDALRasterBand *band){
if (band == NULL)
{
return;
}
QList<GDALRasterBand*> myBand;
myBand.append(band);
myBand.append(band);
myBand.append(band);
ShowImg(&myBand);
}
void open_image::ShowImg(QList<GDALRasterBand *> *imgBand){
if (imgBand->size() != 3)
{
return;
}
int imgWidth = imgBand->at(0)->GetXSize();
int imgHeight = imgBand->at(0)->GetYSize();
m_scaleFactor = this->height() * 1.0 / imgHeight;
int iScaleWidth = (int)(imgWidth *m_scaleFactor - 1);
int iScaleHeight = (int)(imgHeight *m_scaleFactor - 1);
GDALDataType dataType = imgBand->at(0)->GetRasterDataType();
// 首先分别读取RGB三个波段
float* rBand = new float[iScaleWidth *iScaleHeight];
float* gBand = new float[iScaleWidth *iScaleHeight];
float* bBand = new float[iScaleWidth *iScaleHeight];
unsigned char *rBandUC, *gBandUC, *bBandUC;
// 根据是否显示彩色图像,判断RGB三个波段的组成方式,并分别读取
if (m_showColor == true)
{
imgBand->at(0)->RasterIO(GF_Read, 0, 0, imgWidth, imgHeight, rBand, iScaleWidth, iScaleHeight, GDT_Float32, 0, 0);
imgBand->at(1)->RasterIO(GF_Read, 0, 0, imgWidth, imgHeight, gBand, iScaleWidth, iScaleHeight, GDT_Float32, 0, 0);
imgBand->at(2)->RasterIO(GF_Read, 0, 0, imgWidth, imgHeight, bBand, iScaleWidth, iScaleHeight, GDT_Float32, 0, 0);
// 分别拉伸每个波段并将Float转换为unsigned char
rBandUC = ImgSketch(rBand, imgBand->at(0), iScaleWidth * iScaleHeight, imgBand->at(0)->GetNoDataValue());
gBandUC = ImgSketch(gBand, imgBand->at(1), iScaleWidth * iScaleHeight, imgBand->at(1)->GetNoDataValue());
bBandUC = ImgSketch(bBand, imgBand->at(2), iScaleWidth * iScaleHeight, imgBand->at(2)->GetNoDataValue());
}
else
{
imgBand->at(0)->RasterIO(GF_Read, 0, 0, imgWidth, imgHeight, rBand, iScaleWidth, iScaleHeight, GDT_Float32, 0, 0);
rBandUC = ImgSketch(rBand, imgBand->at(0), iScaleWidth * iScaleHeight, imgBand->at(0)->GetNoDataValue());
gBandUC = rBandUC;
bBandUC = rBandUC;
}
// 将三个波段组合起来
int bytePerLine = (iScaleWidth * 24 + 31) / 8;
unsigned char* allBandUC = new unsigned char[bytePerLine * iScaleHeight];
for (int h = 0; h < iScaleHeight; h++)
{
for (int w = 0; w < iScaleWidth; w++)
{
allBandUC[h * bytePerLine + w * 3 + 0] = rBandUC[h * iScaleWidth + w];
allBandUC[h * bytePerLine + w * 3 + 1] = gBandUC[h * iScaleWidth + w];
allBandUC[h * bytePerLine + w * 3 + 2] = bBandUC[h * iScaleWidth + w];
}
}
// 构造图像并显示
QGraphicsPixmapItem *imgItem = new QGraphicsPixmapItem(QPixmap::fromImage(QImage(allBandUC, iScaleWidth, iScaleHeight, bytePerLine, QImage::Format_RGB888)));
QGraphicsScene *myScene = new QGraphicsScene();
myScene->addItem(imgItem);
ui.image_show->setScene(myScene);
}
unsigned char* open_image::ImgSketch(float* buffer, GDALRasterBand* currentBand, int bandSize, double noValue)
{
unsigned char* resBuffer = new unsigned char[bandSize];
double max, min;
double minmax[2];
currentBand->ComputeRasterMinMax(1, minmax);
min = minmax[0];
max = minmax[1];
if (min <= noValue && noValue <= max)
{
min = 0;
}
for (int i = 0; i < bandSize; i++)
{
if (buffer[i] > max)
{
resBuffer[i] = 255;
}
else if (buffer[i] <= max&& buffer[i] >= min)
{
resBuffer[i] = static_cast<uchar>(255 - 255 * (max - buffer[i]) / (max - min));
}
else
{
resBuffer[i] = 0;
}
}
return resBuffer;
}
void open_image::wheelEvent(QWheelEvent *event)
{
// 滚轮向上滑动,放大图像
if (event->delta() > 0)
{
ZoomIn();
}
// 滚轮向下滑动,缩小图像
if (event->delta() < 0)
{
ZoomOut();
}
}
void open_image::CloseCurrentImg()
{
poDataset = NULL;
imgMetaModel->clear();
fileListModel->clear();
}
void open_image::ShowImgInfor(const QString filename){
if (filename == "" || poDataset == NULL)
{
return;
}
int row = 0; // 用来记录数据模型的行号
imgMetaModel->setHorizontalHeaderLabels(QStringList() << QStringLiteral("数据类型")<<QStringLiteral("数值"));
// 图像的格式
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Description")));
imgMetaModel->setItem(row++, 1, new QStandardItem(poDataset->GetDriver()->GetDescription()));
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Meta Infor")));
imgMetaModel->setItem(row++, 1, new QStandardItem(poDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME)));
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Data Type")));
imgMetaModel->setItem(row++, 1, new QStandardItem(GDALGetDataTypeName((poDataset->GetRasterBand(1)->GetRasterDataType()))));
// 图像的大小和波段个数
imgMetaModel->setItem(row, 0, new QStandardItem(tr("X Size")));
imgMetaModel->setItem(row++, 1, new QStandardItem(QString::number(poDataset->GetRasterXSize())));
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Y Size")));
imgMetaModel->setItem(row++, 1, new QStandardItem(QString::number(poDataset->GetRasterYSize())));
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Bands Count")));
imgMetaModel->setItem(row++, 1, new QStandardItem(QString::number(poDataset->GetRasterCount())));
// 图像的投影信息
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Projection")));
imgMetaModel->setItem(row++, 1, new QStandardItem(poDataset->GetProjectionRef()));
// 图像的坐标和分辨率信息
double adfGeoTransform[6];
QString origin = "";
QString pixelSize = "";
if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None)
{
origin = QString::number(adfGeoTransform[0]) + ", " + QString::number(adfGeoTransform[3]);
pixelSize = QString::number(adfGeoTransform[1]) + ", " + QString::number(adfGeoTransform[5]);
}
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Origin")));
imgMetaModel->setItem(row++, 1, new QStandardItem(origin));
imgMetaModel->setItem(row, 0, new QStandardItem(tr("Pixel Size")));
imgMetaModel->setItem(row++, 1, new QStandardItem(pixelSize));
ui.tableView->setModel(imgMetaModel);
}
void open_image::ShowFileList(const QString filename){
if (filename == "" || poDataset == NULL)
{
return;
}
fileListModel->setHorizontalHeaderLabels(QStringList() << QStringLiteral("影像名"));
QStandardItem *rootItem = new QStandardItem(filename);
for (int i = 0; i < poDataset->GetRasterCount(); i++)
{
QStandardItem *childItem = new QStandardItem(tr("Band %1").arg(i + 1));
rootItem->setChild(i, childItem);
//childItem->setCheckable(true);//加二态复选框
}
fileListModel->setItem(0, rootItem);
ui.treeView->setModel(fileListModel);
}
QSize open_image::sizeHint() const
{
return QSize(1024, 768);
}
void open_image::mousePressEvent(QMouseEvent *event)
{
// 滚轮键按下,平移图像
if (event->button() == Qt::MidButton)
{
ui.image_show->setDragMode(QGraphicsView::ScrollHandDrag);
ui.image_show->setInteractive(false);
lastEventCursorPos = event->pos();
}
}
void open_image::mouseMoveEvent(QMouseEvent *event)
{
if (ui.image_show->dragMode() == QGraphicsView::ScrollHandDrag)
{
QPoint delta = (event->pos() - lastEventCursorPos) / 10;
ui.image_show->horizontalScrollBar()->setValue(ui.image_show->horizontalScrollBar()->value() + (isRightToLeft() ? delta.x() : -delta.x()));
ui.image_show->verticalScrollBar()->setValue(ui.image_show->verticalScrollBar()->value() - delta.y());
ui.image_show->viewport()->setCursor(Qt::ClosedHandCursor);
}
}
void open_image::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
ui.image_show->setDragMode(QGraphicsView::NoDrag);
}
}
结果展示:
只打开图片的话,没有投影信息
*//工程文件,有需要可以下载,主要是为了备份 用了GDAL+QT+OPENCV(包括有:图像打开,元数据查看,VDVI计算,灰度转换、直方图均衡化,拉普拉斯滤波、高斯滤波、线性拉伸、区域生长分割),还有很多bug没修改------
链接:https://pan.baidu.com/s/1hA_0nECmNBI0ipG926JzZA
提取码:ywet 复制这段内容后打开百度网盘手机App,操作更方便哦*
直方图均衡化截图有问题—不知道为啥
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)