#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QFile>
#include <qDebug>
#include <QMessageBox>
#include <QDataStream>
#include <QFileDialog>
#include <QStandardPaths>
struct Depth
{
int x;
int y;
int z;
quint8 c;
void write(QDataStream &ds) const
{
ds << x << y << z << c;
}
void read(QDataStream &ds)
{
ds >> x >> y >> z >> c;
}
};
QDataStream& operator<<(QDataStream &ds, const Depth &t)
{
t.write(ds);
return ds;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//void MainWindow::on_tbDepth_clicked()
//{
// //1. 加载所有图片
// QList<HImage> list;
// for (int i = 1; i <= 10; i++)
// {
// QString sNum = i < 10 ? QString("focus_0%1.png").arg(i) : QString("focus_%1.png").arg(i);
// QString sPath = QString("C:/Users/Public/Documents/MVTec/HALCON-12.0/examples/images/dff/%1").arg(sNum);
// HImage hImage(sPath.toUtf8().data());
// list.append(hImage);
// }
// makeDepthFromFocus(list);
//}
void MainWindow::on_tbDepth_clicked()
{
QString sPath = ui->lePath->text();
if (sPath.isEmpty()) return;
QStringList list = getImgFileNames(sPath);
//1. 加载所有图片
QList<HImage> listImage;
int iSize = list.size();
for (int i = 0; i < iSize; i++)
{
QString sFilePath = sPath + "/" + list[i];
HImage hImage(sFilePath.toUtf8().data());
listImage.append(hImage);
}
//2. 景深合成
makeDepthFromFocus(listImage);
}
//景深合成
void MainWindow::makeDepthFromFocus(QList<HImage> list)
{
//1. 把所有图片和成N个通道图
HImage hAllImage;
int iSize = list.size();
for (int i = 0; i < iSize; i++) hAllImage = (i == 1) ? list[i] : hAllImage.AppendChannel(list[i]);
//2. 景深合成(hDepth:记录深度(Z轴)。hConfidence:记录最佳亮度)
HImage hConfidence;
HImage hDepth = hAllImage.DepthFromFocus(&hConfidence, "highpass", "next_maximum");
hDepth.WriteImage("bmp", 0, "C:/Users/MARK/Desktop/hDepth.bmp");
hConfidence.WriteImage("bmp", 0, "C:/Users/MARK/Desktop/hConfidence.bmp");
//3. 使用均值滤波平滑景深合成的图片
HImage hDepthHighConf = hDepth.MeanImage(51, 51);
HImage hSharpImage = hAllImage.SelectGrayvaluesFromChannels(hDepthHighConf);
hSharpImage.WriteImage("bmp", 0, "C:/Users/MARK/Desktop/hSharpImage.bmp");
//4. 平滑深度图
HImage hImageScaleMax = hDepthHighConf.ScaleImageMax();
HImage hDepthMean = hImageScaleMax.MeanImage(51, 51);
//HImage hTexturedImage = hDepthMean.Compose2(hSharpImage);
//5. 取出x、y、z、c,其中hDepthMean保存了x、y、z,hSharpImage保存了c。
// 把x、y、z、c数据写到二进制文件
int w = hDepthMean.Width();
int h = hDepthMean.Height();
QFile file("./depth.txt");
file.open(QFile::WriteOnly);
QDataStream out(&file);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
HTuple ZZ = hDepthMean.GetGrayval(y, x);
HTuple CC = hSharpImage.GetGrayval(y, x);
int z = ZZ[0].I();
char c = CC[0].I();
Depth depth;
depth.x = x;
depth.y = y;
depth.z = z;
depth.c = c;
out << depth;
}
}
file.close();
QMessageBox::information(this, "景深合成", "完成景深合成");
}
//选择图片目录
void MainWindow::on_tbPath_clicked()
{
QString sDesktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString sPath = QFileDialog::getExistingDirectory(this, "选择目录", sDesktop, QFileDialog::ShowDirsOnly);
ui->lePath->setText(sPath);
}
//获取指定目录下的所有图片
QStringList MainWindow::getImgFileNames(const QString &sPath)
{
if (sPath.isEmpty()) return QStringList();
//获取指定路径下的所有图片名称
QDir dir(sPath);
QStringList lisFilters;
lisFilters << "*.bmp" << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff";
QStringList listImgFileNames = dir.entryList(lisFilters, QDir::Files | QDir::Readable, QDir::Name);
return listImgFileNames;
}