1179 lines
47 KiB
C++
1179 lines
47 KiB
C++
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
// StereoCalibDialog.cpp ─ 在线双目标定 (v3)
|
||
|
|
//
|
||
|
|
// [v3]
|
||
|
|
// - AppSettings 记忆 caltab 和保存目录
|
||
|
|
// - 预标定内参加载 + 仅优化外参 (固定内参)
|
||
|
|
// - onValidateAll 跳过已验证对
|
||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
||
|
|
#include "StereoCalibDialog.h"
|
||
|
|
#include "CameraManager.h"
|
||
|
|
#include "AppSettings.h"
|
||
|
|
#include "UserVisibleLog.h"
|
||
|
|
|
||
|
|
#define NOMINMAX
|
||
|
|
#include <VisionGuideApi.h>
|
||
|
|
using namespace VisionGuideApi;
|
||
|
|
|
||
|
|
#include <QVBoxLayout>
|
||
|
|
#include <QHBoxLayout>
|
||
|
|
#include <QFormLayout>
|
||
|
|
#include <QSplitter>
|
||
|
|
#include <QGroupBox>
|
||
|
|
#include <QLabel>
|
||
|
|
#include <QTreeWidget>
|
||
|
|
#include <QTreeWidgetItem>
|
||
|
|
#include <QHeaderView>
|
||
|
|
#include <QLineEdit>
|
||
|
|
#include <QPushButton>
|
||
|
|
#include <QProgressBar>
|
||
|
|
#include <QTextEdit>
|
||
|
|
#include <QComboBox>
|
||
|
|
#include <QDoubleSpinBox>
|
||
|
|
#include <QCheckBox>
|
||
|
|
#include <QFileDialog>
|
||
|
|
#include <QDir>
|
||
|
|
#include <QFile>
|
||
|
|
#include <QFileInfo>
|
||
|
|
#include <QDateTime>
|
||
|
|
#include <QApplication>
|
||
|
|
#include <QMessageBox>
|
||
|
|
#include <QFont>
|
||
|
|
#include <cmath>
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
constexpr int kMinimumStereoPairs = 10;
|
||
|
|
constexpr int kHoneycombBoard = 0;
|
||
|
|
constexpr int kRectangularDotBoard = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
QString StereoCalibDialog::defaultSaveDir()
|
||
|
|
{
|
||
|
|
return QString::fromUtf8("D:/Qt/firstdemo/firstdemo/StereoCalib");
|
||
|
|
}
|
||
|
|
|
||
|
|
struct StereoCalibDialog::Impl
|
||
|
|
{
|
||
|
|
HTuple calibHandle;
|
||
|
|
HTuple camParamL, camParamR, relPose;
|
||
|
|
bool handleValid = false;
|
||
|
|
|
||
|
|
// 用户加载的预标定内参(可选,用于"仅优化外参"功能)
|
||
|
|
HTuple preCamParamL, preCamParamR;
|
||
|
|
bool preLLoaded = false, preRLoaded = false;
|
||
|
|
|
||
|
|
static HTuple initialCamParam()
|
||
|
|
{
|
||
|
|
HTuple p;
|
||
|
|
p[0] = "area_scan_division";
|
||
|
|
p[1] = 0.035;
|
||
|
|
p[2] = 0.0;
|
||
|
|
p[3] = 2.5e-6;
|
||
|
|
p[4] = 2.5e-6;
|
||
|
|
p[5] = 2560.0;
|
||
|
|
p[6] = 2560.0;
|
||
|
|
p[7] = 5120;
|
||
|
|
p[8] = 5120;
|
||
|
|
return p;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void findCalibObject(const HObject& image, const HTuple& handle,
|
||
|
|
Hlong cameraIdx, Hlong calibObjIdx,
|
||
|
|
Hlong itemIdx, int boardType,
|
||
|
|
double sigma, double alpha,
|
||
|
|
double gapTolerance, double maxDiamMarks,
|
||
|
|
bool skipFindCaltab)
|
||
|
|
{
|
||
|
|
HTuple names, values;
|
||
|
|
if (boardType == kHoneycombBoard) {
|
||
|
|
names.Append("sigma");
|
||
|
|
values.Append(sigma);
|
||
|
|
} else {
|
||
|
|
names.Append("alpha");
|
||
|
|
values.Append(alpha);
|
||
|
|
names.Append("gap_tolerance");
|
||
|
|
values.Append(gapTolerance);
|
||
|
|
if (maxDiamMarks > 0.0) {
|
||
|
|
names.Append("max_diam_marks");
|
||
|
|
values.Append(maxDiamMarks);
|
||
|
|
}
|
||
|
|
names.Append("skip_find_caltab");
|
||
|
|
values.Append(skipFindCaltab ? "true" : "false");
|
||
|
|
}
|
||
|
|
FindCalibObject(image, handle, cameraIdx, calibObjIdx, itemIdx,
|
||
|
|
names, values);
|
||
|
|
}
|
||
|
|
|
||
|
|
void clearHandle()
|
||
|
|
{
|
||
|
|
if (handleValid) {
|
||
|
|
try {
|
||
|
|
ClearCalibData(calibHandle);
|
||
|
|
} catch (...) {}
|
||
|
|
handleValid = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
StereoCalibDialog::StereoCalibDialog(CameraManager* camMgr, QWidget* parent)
|
||
|
|
: QDialog(parent)
|
||
|
|
, m_camMgr(camMgr)
|
||
|
|
, m_d(std::make_unique<Impl>())
|
||
|
|
{
|
||
|
|
setWindowTitle(QString::fromUtf8("在线双目标定工具"));
|
||
|
|
setMinimumSize(1440, 960);
|
||
|
|
resize(1500, 1000);
|
||
|
|
|
||
|
|
m_saveDir = AppSettings::instance().get(
|
||
|
|
AppSettings::K_CalibSaveDir, defaultSaveDir());
|
||
|
|
QDir().mkpath(m_saveDir);
|
||
|
|
|
||
|
|
buildUi();
|
||
|
|
setupConnections();
|
||
|
|
refreshList();
|
||
|
|
|
||
|
|
log(QString::fromUtf8("双目标定对话框已打开。"));
|
||
|
|
log(QString::fromUtf8("图像保存至: ") +
|
||
|
|
QCoreApplication::applicationDirPath() + "/calib_images/");
|
||
|
|
log(QString::fromUtf8("结果保存至: ") + m_saveDir);
|
||
|
|
}
|
||
|
|
|
||
|
|
StereoCalibDialog::~StereoCalibDialog()
|
||
|
|
{
|
||
|
|
m_d->clearHandle();
|
||
|
|
}
|
||
|
|
|
||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
||
|
|
// UI
|
||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
||
|
|
void StereoCalibDialog::buildUi()
|
||
|
|
{
|
||
|
|
// ── ① 顶部: 标定配置 + 预标定内参 ──────────────────────────────
|
||
|
|
auto* topGroup = new QGroupBox(QString::fromUtf8("标定配置"));
|
||
|
|
auto* topForm = new QFormLayout(topGroup);
|
||
|
|
|
||
|
|
// caltab (记忆)
|
||
|
|
QString lastCaltab = AppSettings::instance().get(
|
||
|
|
AppSettings::K_Caltab,
|
||
|
|
"D:/Qt/firstdemo/firstdemo/VisionGuideData/LD-HG75-13x15.cpd");
|
||
|
|
m_editCaltab = new QLineEdit(lastCaltab);
|
||
|
|
auto* btnCaltab = new QPushButton(QString::fromUtf8("浏览..."));
|
||
|
|
btnCaltab->setFixedWidth(80);
|
||
|
|
auto* caltabRow = new QHBoxLayout;
|
||
|
|
caltabRow->addWidget(m_editCaltab);
|
||
|
|
caltabRow->addWidget(btnCaltab);
|
||
|
|
connect(btnCaltab, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onBrowseCaltab);
|
||
|
|
topForm->addRow(QString::fromUtf8("标定板文件:"), caltabRow);
|
||
|
|
|
||
|
|
auto& settings = AppSettings::instance();
|
||
|
|
m_comboBoardType = new QComboBox;
|
||
|
|
m_comboBoardType->addItem(QString::fromUtf8("蜂窝圆点板(六边形排列)"), kHoneycombBoard);
|
||
|
|
m_comboBoardType->addItem(QString::fromUtf8("矩形圆点板(传统 视觉引导)"), kRectangularDotBoard);
|
||
|
|
const int savedBoardType = settings.getRaw(AppSettings::K_CalibBoardType, "0").toInt();
|
||
|
|
m_comboBoardType->setCurrentIndex(savedBoardType == kRectangularDotBoard ? 1 : 0);
|
||
|
|
topForm->addRow(QString::fromUtf8("标定板类型:"), m_comboBoardType);
|
||
|
|
|
||
|
|
auto makeParamSpin = [](double minValue, double maxValue, double value,
|
||
|
|
int decimals, double step) {
|
||
|
|
auto* spin = new QDoubleSpinBox;
|
||
|
|
spin->setRange(minValue, maxValue);
|
||
|
|
spin->setDecimals(decimals);
|
||
|
|
spin->setSingleStep(step);
|
||
|
|
spin->setValue(value);
|
||
|
|
return spin;
|
||
|
|
};
|
||
|
|
m_spinFindSigma = makeParamSpin(0.1, 10.0,
|
||
|
|
settings.getRaw(AppSettings::K_CalibFindSigma, "1.9").toDouble(), 2, 0.1);
|
||
|
|
m_spinFindSigma->setToolTip(QString::fromUtf8("蜂窝板圆点轮廓平滑系数;图像噪声或倾斜较大时可逐步增大"));
|
||
|
|
m_spinFindAlpha = makeParamSpin(0.1, 10.0,
|
||
|
|
settings.getRaw(AppSettings::K_CalibFindAlpha, "0.9").toDouble(), 2, 0.1);
|
||
|
|
m_spinFindAlpha->setToolTip(QString::fromUtf8("矩形圆点板轮廓提取参数;alpha越大,平滑程度越低"));
|
||
|
|
m_spinGapTolerance = makeParamSpin(0.1, 5.0,
|
||
|
|
settings.getRaw(AppSettings::K_CalibGapTolerance, "1.0").toDouble(), 2, 0.05);
|
||
|
|
m_spinGapTolerance->setToolTip(QString::fromUtf8("矩形圆点板间距容差;强透视时可适当增大"));
|
||
|
|
m_spinMaxDiamMarks = makeParamSpin(0.0, 5000.0,
|
||
|
|
settings.getRaw(AppSettings::K_CalibMaxDiamMarks, "0").toDouble(), 1, 10.0);
|
||
|
|
m_spinMaxDiamMarks->setSpecialValueText(QString::fromUtf8("自动"));
|
||
|
|
m_spinMaxDiamMarks->setToolTip(QString::fromUtf8("0表示由视觉引导自动估计;仅用于矩形圆点板"));
|
||
|
|
m_checkSkipFindCaltab = new QCheckBox(QString::fromUtf8("跳过矩形板边框定位(高级)"));
|
||
|
|
m_checkSkipFindCaltab->setChecked(
|
||
|
|
settings.getRaw(AppSettings::K_CalibSkipFindCaltab, "0").toInt() != 0);
|
||
|
|
topForm->addRow(QString::fromUtf8("蜂窝板 sigma:"), m_spinFindSigma);
|
||
|
|
topForm->addRow(QString::fromUtf8("矩形板 alpha:"), m_spinFindAlpha);
|
||
|
|
topForm->addRow(QString::fromUtf8("矩形板间距容差:"), m_spinGapTolerance);
|
||
|
|
topForm->addRow(QString::fromUtf8("矩形板最大圆点直径(px):"), m_spinMaxDiamMarks);
|
||
|
|
topForm->addRow(QString(), m_checkSkipFindCaltab);
|
||
|
|
|
||
|
|
auto detectionSettingsChanged = [this]() {
|
||
|
|
auto& s = AppSettings::instance();
|
||
|
|
s.set(AppSettings::K_CalibBoardType,
|
||
|
|
QString::number(m_comboBoardType->currentData().toInt()));
|
||
|
|
s.set(AppSettings::K_CalibFindSigma, QString::number(m_spinFindSigma->value(), 'f', 2));
|
||
|
|
s.set(AppSettings::K_CalibFindAlpha, QString::number(m_spinFindAlpha->value(), 'f', 2));
|
||
|
|
s.set(AppSettings::K_CalibGapTolerance,
|
||
|
|
QString::number(m_spinGapTolerance->value(), 'f', 2));
|
||
|
|
s.set(AppSettings::K_CalibMaxDiamMarks,
|
||
|
|
QString::number(m_spinMaxDiamMarks->value(), 'f', 1));
|
||
|
|
s.set(AppSettings::K_CalibSkipFindCaltab,
|
||
|
|
m_checkSkipFindCaltab->isChecked() ? "1" : "0");
|
||
|
|
updateBoardParameterUi();
|
||
|
|
invalidateDetectionResults(QString::fromUtf8("标定板提取参数已改变"));
|
||
|
|
};
|
||
|
|
connect(m_comboBoardType, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||
|
|
this, [detectionSettingsChanged](int) { detectionSettingsChanged(); });
|
||
|
|
for (QDoubleSpinBox* spin : {m_spinFindSigma, m_spinFindAlpha,
|
||
|
|
m_spinGapTolerance, m_spinMaxDiamMarks}) {
|
||
|
|
connect(spin, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
|
||
|
|
this, [detectionSettingsChanged](double) { detectionSettingsChanged(); });
|
||
|
|
}
|
||
|
|
connect(m_checkSkipFindCaltab, &QCheckBox::toggled,
|
||
|
|
this, [detectionSettingsChanged](bool) { detectionSettingsChanged(); });
|
||
|
|
updateBoardParameterUi();
|
||
|
|
|
||
|
|
// 保存目录
|
||
|
|
m_editSaveDir = new QLineEdit(m_saveDir);
|
||
|
|
auto* btnSaveDir = new QPushButton(QString::fromUtf8("浏览..."));
|
||
|
|
btnSaveDir->setFixedWidth(80);
|
||
|
|
auto* saveDirRow = new QHBoxLayout;
|
||
|
|
saveDirRow->addWidget(m_editSaveDir);
|
||
|
|
saveDirRow->addWidget(btnSaveDir);
|
||
|
|
connect(btnSaveDir, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onBrowseSaveDir);
|
||
|
|
topForm->addRow(QString::fromUtf8("结果保存目录:"), saveDirRow);
|
||
|
|
|
||
|
|
// 预标定内参 - 左
|
||
|
|
m_editLeftIntrinsic = new QLineEdit(
|
||
|
|
AppSettings::instance().get(AppSettings::K_LeftCal, ""));
|
||
|
|
m_editLeftIntrinsic->setPlaceholderText(
|
||
|
|
QString::fromUtf8("可选:仅优化外参时使用"));
|
||
|
|
auto* btnLoadL = new QPushButton(QString::fromUtf8("加载..."));
|
||
|
|
btnLoadL->setFixedWidth(80);
|
||
|
|
auto* rowL = new QHBoxLayout;
|
||
|
|
rowL->addWidget(m_editLeftIntrinsic);
|
||
|
|
rowL->addWidget(btnLoadL);
|
||
|
|
connect(btnLoadL, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onLoadLeftIntrinsic);
|
||
|
|
topForm->addRow(QString::fromUtf8("左相机预内参(可选):"), rowL);
|
||
|
|
|
||
|
|
// 预标定内参 - 右
|
||
|
|
m_editRightIntrinsic = new QLineEdit(
|
||
|
|
AppSettings::instance().get(AppSettings::K_RightCal, ""));
|
||
|
|
m_editRightIntrinsic->setPlaceholderText(
|
||
|
|
QString::fromUtf8("可选:仅优化外参时使用"));
|
||
|
|
auto* btnLoadR = new QPushButton(QString::fromUtf8("加载..."));
|
||
|
|
btnLoadR->setFixedWidth(80);
|
||
|
|
auto* rowR = new QHBoxLayout;
|
||
|
|
rowR->addWidget(m_editRightIntrinsic);
|
||
|
|
rowR->addWidget(btnLoadR);
|
||
|
|
connect(btnLoadR, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onLoadRightIntrinsic);
|
||
|
|
topForm->addRow(QString::fromUtf8("右相机预内参(可选):"), rowR);
|
||
|
|
|
||
|
|
// 若初始有文件,尝试加载
|
||
|
|
if (!m_editLeftIntrinsic->text().isEmpty() &&
|
||
|
|
QFile::exists(m_editLeftIntrinsic->text())) {
|
||
|
|
try {
|
||
|
|
ReadCamPar(HTuple(m_editLeftIntrinsic->text().toLocal8Bit().constData()),
|
||
|
|
&m_d->preCamParamL);
|
||
|
|
m_d->preLLoaded = true;
|
||
|
|
} catch (...) {}
|
||
|
|
}
|
||
|
|
if (!m_editRightIntrinsic->text().isEmpty() &&
|
||
|
|
QFile::exists(m_editRightIntrinsic->text())) {
|
||
|
|
try {
|
||
|
|
ReadCamPar(HTuple(m_editRightIntrinsic->text().toLocal8Bit().constData()),
|
||
|
|
&m_d->preCamParamR);
|
||
|
|
m_d->preRLoaded = true;
|
||
|
|
} catch (...) {}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── ② 中部: 3 栏 ───────────────────────────────────────────────
|
||
|
|
auto* middleSplit = new QSplitter(Qt::Horizontal);
|
||
|
|
|
||
|
|
// 左: 图像对列表
|
||
|
|
auto* listGroup = new QGroupBox(QString::fromUtf8("图像对列表"));
|
||
|
|
auto* listVlay = new QVBoxLayout(listGroup);
|
||
|
|
|
||
|
|
m_labelPairCount = new QLabel(QString::fromUtf8("已采集: 0 对"));
|
||
|
|
m_labelPairCount->setStyleSheet("color:#00cc66; font-weight:bold;");
|
||
|
|
listVlay->addWidget(m_labelPairCount);
|
||
|
|
|
||
|
|
m_treePairs = new QTreeWidget;
|
||
|
|
m_treePairs->setColumnCount(3);
|
||
|
|
m_treePairs->setHeaderLabels(QStringList()
|
||
|
|
<< QString::fromUtf8("名称")
|
||
|
|
<< QString::fromUtf8("是否有效")
|
||
|
|
<< QString::fromUtf8("重投影误差"));
|
||
|
|
m_treePairs->setFont(QFont("Consolas", 10));
|
||
|
|
m_treePairs->setRootIsDecorated(false);
|
||
|
|
m_treePairs->setAlternatingRowColors(true);
|
||
|
|
m_treePairs->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||
|
|
m_treePairs->header()->setStretchLastSection(false);
|
||
|
|
m_treePairs->header()->setSectionResizeMode(0, QHeaderView::Stretch);
|
||
|
|
m_treePairs->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
|
||
|
|
m_treePairs->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
|
||
|
|
m_treePairs->setMinimumHeight(200);
|
||
|
|
listVlay->addWidget(m_treePairs);
|
||
|
|
|
||
|
|
auto* listCtrlHlay = new QHBoxLayout;
|
||
|
|
m_btnExclude = new QPushButton(QString::fromUtf8("剔除选中"));
|
||
|
|
m_btnInclude = new QPushButton(QString::fromUtf8("恢复选中"));
|
||
|
|
m_btnExclude->setStyleSheet("QPushButton{background:#c0392b;color:white;padding:4px;}");
|
||
|
|
m_btnInclude->setStyleSheet("QPushButton{background:#2980b9;color:white;padding:4px;}");
|
||
|
|
listCtrlHlay->addWidget(m_btnExclude);
|
||
|
|
listCtrlHlay->addWidget(m_btnInclude);
|
||
|
|
listVlay->addLayout(listCtrlHlay);
|
||
|
|
|
||
|
|
middleSplit->addWidget(listGroup);
|
||
|
|
|
||
|
|
// 中: 控制
|
||
|
|
auto* ctrlWidget = new QWidget;
|
||
|
|
auto* ctrlVlay = new QVBoxLayout(ctrlWidget);
|
||
|
|
|
||
|
|
auto* captureGroup = new QGroupBox(QString::fromUtf8("采集与检测"));
|
||
|
|
auto* captureVlay = new QVBoxLayout(captureGroup);
|
||
|
|
m_btnCapture = new QPushButton(QString::fromUtf8("[CAM] 同步单帧采集"));
|
||
|
|
m_btnValidateAll = new QPushButton(QString::fromUtf8("[CHK] 检测所有图像对"));
|
||
|
|
m_btnCapture->setMinimumHeight(32);
|
||
|
|
m_btnValidateAll->setMinimumHeight(32);
|
||
|
|
captureVlay->addWidget(m_btnCapture);
|
||
|
|
captureVlay->addWidget(m_btnValidateAll);
|
||
|
|
ctrlVlay->addWidget(captureGroup);
|
||
|
|
|
||
|
|
auto* calibGroup = new QGroupBox(QString::fromUtf8("标定控制"));
|
||
|
|
auto* calibVlay = new QVBoxLayout(calibGroup);
|
||
|
|
m_btnCalibrate = new QPushButton(QString::fromUtf8(">> 完整双目标定 (优化内外参)"));
|
||
|
|
m_btnRefineExt = new QPushButton(QString::fromUtf8("[EXT] 仅优化外参 (固定内参)"));
|
||
|
|
m_btnComputeErrors = new QPushButton(QString::fromUtf8("计算各对重投影误差"));
|
||
|
|
m_btnSave = new QPushButton(QString::fromUtf8("保存标定结果"));
|
||
|
|
m_btnCalibrate->setMinimumHeight(36);
|
||
|
|
m_btnRefineExt->setMinimumHeight(32);
|
||
|
|
m_btnComputeErrors->setMinimumHeight(32);
|
||
|
|
m_btnSave->setMinimumHeight(32);
|
||
|
|
m_btnCalibrate->setStyleSheet(
|
||
|
|
"QPushButton{background:#1e6f9f;color:white;font-weight:bold;"
|
||
|
|
"font-size:13px;border-radius:4px;}"
|
||
|
|
"QPushButton:hover{background:#2589c4;}"
|
||
|
|
"QPushButton:disabled{background:#555;color:#999;}");
|
||
|
|
m_btnRefineExt->setStyleSheet(
|
||
|
|
"QPushButton{background:#2e8b57;color:white;font-weight:bold;"
|
||
|
|
"font-size:12px;border-radius:4px;}"
|
||
|
|
"QPushButton:hover{background:#38a56a;}"
|
||
|
|
"QPushButton:disabled{background:#555;color:#999;}");
|
||
|
|
m_btnComputeErrors->setEnabled(false);
|
||
|
|
m_btnSave->setEnabled(false);
|
||
|
|
|
||
|
|
m_labelRMSE = new QLabel(QString::fromUtf8("整体 RMSE: —"));
|
||
|
|
m_labelRMSE->setStyleSheet("font-weight:bold; color:#00cc66; font-size:13px;");
|
||
|
|
|
||
|
|
calibVlay->addWidget(m_btnCalibrate);
|
||
|
|
calibVlay->addWidget(m_btnRefineExt);
|
||
|
|
calibVlay->addWidget(m_btnComputeErrors);
|
||
|
|
calibVlay->addWidget(m_btnSave);
|
||
|
|
calibVlay->addWidget(m_labelRMSE);
|
||
|
|
ctrlVlay->addWidget(calibGroup);
|
||
|
|
|
||
|
|
m_progress = new QProgressBar;
|
||
|
|
m_progress->setValue(0);
|
||
|
|
ctrlVlay->addWidget(m_progress);
|
||
|
|
ctrlVlay->addStretch();
|
||
|
|
|
||
|
|
middleSplit->addWidget(ctrlWidget);
|
||
|
|
|
||
|
|
// 右: 日志
|
||
|
|
auto* logGroup = new QGroupBox(QString::fromUtf8("运行日志"));
|
||
|
|
auto* logVlay = new QVBoxLayout(logGroup);
|
||
|
|
m_textLog = new QTextEdit;
|
||
|
|
m_textLog->setReadOnly(true);
|
||
|
|
m_textLog->setFont(QFont("Consolas", 10));
|
||
|
|
m_textLog->setStyleSheet("background:#0d0d1a; color:#00cc88;");
|
||
|
|
m_textLog->setMinimumHeight(220);
|
||
|
|
logVlay->addWidget(m_textLog);
|
||
|
|
|
||
|
|
middleSplit->addWidget(logGroup);
|
||
|
|
|
||
|
|
middleSplit->setStretchFactor(0, 4);
|
||
|
|
middleSplit->setStretchFactor(1, 2);
|
||
|
|
middleSplit->setStretchFactor(2, 4);
|
||
|
|
|
||
|
|
// ── ③ 底部: 左右相机预览 ───────────────────────────────────────
|
||
|
|
auto* previewHlay = new QHBoxLayout;
|
||
|
|
auto* leftCamGroup = new QGroupBox(QString::fromUtf8("左相机预览"));
|
||
|
|
auto* rightCamGroup = new QGroupBox(QString::fromUtf8("右相机预览"));
|
||
|
|
auto* leftCamVlay = new QVBoxLayout(leftCamGroup);
|
||
|
|
auto* rightCamVlay = new QVBoxLayout(rightCamGroup);
|
||
|
|
|
||
|
|
m_labelLeft = new QLabel(QString::fromUtf8("左相机图像"));
|
||
|
|
m_labelRight = new QLabel(QString::fromUtf8("右相机图像"));
|
||
|
|
for (auto* lbl : {m_labelLeft, m_labelRight}) {
|
||
|
|
lbl->setMinimumSize(560, 260);
|
||
|
|
lbl->setAlignment(Qt::AlignCenter);
|
||
|
|
lbl->setStyleSheet("background:#1a1a2e; color:#666; font-size:13px;");
|
||
|
|
lbl->setFrameShape(QFrame::Box);
|
||
|
|
lbl->setScaledContents(false);
|
||
|
|
lbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||
|
|
}
|
||
|
|
leftCamVlay->addWidget(m_labelLeft);
|
||
|
|
rightCamVlay->addWidget(m_labelRight);
|
||
|
|
previewHlay->addWidget(leftCamGroup);
|
||
|
|
previewHlay->addWidget(rightCamGroup);
|
||
|
|
|
||
|
|
auto* mainVlay = new QVBoxLayout(this);
|
||
|
|
mainVlay->setContentsMargins(8, 8, 8, 8);
|
||
|
|
mainVlay->setSpacing(6);
|
||
|
|
|
||
|
|
mainVlay->addWidget(topGroup, 0);
|
||
|
|
mainVlay->addWidget(middleSplit, 2);
|
||
|
|
mainVlay->addLayout(previewHlay, 3);
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::setupConnections()
|
||
|
|
{
|
||
|
|
connect(m_btnCapture, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onCapturePair);
|
||
|
|
connect(m_btnValidateAll, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onValidateAll);
|
||
|
|
connect(m_treePairs, &QTreeWidget::itemClicked,
|
||
|
|
this, &StereoCalibDialog::onPairClicked);
|
||
|
|
connect(m_btnExclude, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onExcludeSelected);
|
||
|
|
connect(m_btnInclude, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onIncludeSelected);
|
||
|
|
connect(m_btnCalibrate, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onCalibrate);
|
||
|
|
connect(m_btnRefineExt, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onRefineExtrinsics);
|
||
|
|
connect(m_btnComputeErrors, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onComputeErrors);
|
||
|
|
connect(m_btnSave, &QPushButton::clicked,
|
||
|
|
this, &StereoCalibDialog::onSave);
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::log(const QString& msg)
|
||
|
|
{
|
||
|
|
QString ts = QDateTime::currentDateTime().toString("hh:mm:ss");
|
||
|
|
m_textLog->append(QString("[%1] %2").arg(ts, sanitizeUserVisibleLog(msg)));
|
||
|
|
QApplication::processEvents();
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::setProgress(int pct, const QString& msg)
|
||
|
|
{
|
||
|
|
m_progress->setValue(pct);
|
||
|
|
if (!msg.isEmpty()) {
|
||
|
|
log(msg);
|
||
|
|
}
|
||
|
|
QApplication::processEvents();
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::updateBoardParameterUi()
|
||
|
|
{
|
||
|
|
const bool honeycomb = m_comboBoardType &&
|
||
|
|
m_comboBoardType->currentData().toInt() == kHoneycombBoard;
|
||
|
|
if (m_spinFindSigma) m_spinFindSigma->setEnabled(honeycomb);
|
||
|
|
if (m_spinFindAlpha) m_spinFindAlpha->setEnabled(!honeycomb);
|
||
|
|
if (m_spinGapTolerance) m_spinGapTolerance->setEnabled(!honeycomb);
|
||
|
|
if (m_spinMaxDiamMarks) m_spinMaxDiamMarks->setEnabled(!honeycomb);
|
||
|
|
if (m_checkSkipFindCaltab) m_checkSkipFindCaltab->setEnabled(!honeycomb);
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::invalidateDetectionResults(const QString& reason)
|
||
|
|
{
|
||
|
|
bool changed = m_hasResult;
|
||
|
|
for (auto& pair : m_pairs) {
|
||
|
|
changed = changed || pair.validated;
|
||
|
|
pair.validated = false;
|
||
|
|
pair.validLeft = false;
|
||
|
|
pair.validRight = false;
|
||
|
|
pair.reproj = -1.0;
|
||
|
|
}
|
||
|
|
m_hasResult = false;
|
||
|
|
m_overallRMSE = -1.0;
|
||
|
|
m_d->clearHandle();
|
||
|
|
if (m_btnComputeErrors) m_btnComputeErrors->setEnabled(false);
|
||
|
|
if (m_btnSave) m_btnSave->setEnabled(false);
|
||
|
|
if (m_labelRMSE) {
|
||
|
|
m_labelRMSE->setText(QString::fromUtf8("整体 RMSE: —"));
|
||
|
|
m_labelRMSE->setStyleSheet("font-weight:bold;color:#888;font-size:13px;");
|
||
|
|
}
|
||
|
|
if (changed && !reason.isEmpty()) log(reason + QString::fromUtf8(",请重新检测图像。"));
|
||
|
|
refreshList();
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::displayImageOnLabel(QLabel* lbl, const QImage& img)
|
||
|
|
{
|
||
|
|
if (img.isNull()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
lbl->setPixmap(QPixmap::fromImage(img).scaled(
|
||
|
|
lbl->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::refreshList()
|
||
|
|
{
|
||
|
|
m_treePairs->clear();
|
||
|
|
for (int i = 0; i < m_pairs.size(); ++i) {
|
||
|
|
const auto& p = m_pairs[i];
|
||
|
|
auto* item = new QTreeWidgetItem(m_treePairs);
|
||
|
|
item->setData(0, Qt::UserRole, i);
|
||
|
|
|
||
|
|
item->setText(0, p.displayName());
|
||
|
|
item->setText(1, p.validStr());
|
||
|
|
item->setTextAlignment(1, Qt::AlignCenter);
|
||
|
|
item->setText(2, p.reprojStr());
|
||
|
|
item->setTextAlignment(2, Qt::AlignRight | Qt::AlignVCenter);
|
||
|
|
|
||
|
|
QColor c(Qt::black);
|
||
|
|
if (p.excluded) {
|
||
|
|
c = Qt::darkGray;
|
||
|
|
} else if (p.validated && p.validLeft && p.validRight) {
|
||
|
|
if (p.reproj >= 0 && p.reproj < 0.5) {
|
||
|
|
c = QColor("#00cc66");
|
||
|
|
} else if (p.reproj >= 0.5 && p.reproj < 1.0) {
|
||
|
|
c = QColor("#f39c12");
|
||
|
|
} else if (p.reproj >= 1.0) {
|
||
|
|
c = QColor("#e74c3c");
|
||
|
|
} else {
|
||
|
|
c = QColor("#00cc66");
|
||
|
|
}
|
||
|
|
} else if (p.validated) {
|
||
|
|
c = QColor("#e74c3c");
|
||
|
|
} else {
|
||
|
|
c = Qt::gray;
|
||
|
|
}
|
||
|
|
for (int col = 0; col < 3; ++col) {
|
||
|
|
item->setForeground(col, c);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
m_labelPairCount->setText(QString::fromUtf8("已采集: %1 对").arg(m_pairs.size()));
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::displayPair(int idx)
|
||
|
|
{
|
||
|
|
if (idx < 0 || idx >= m_pairs.size()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
QImage imgL(m_pairs[idx].leftPath), imgR(m_pairs[idx].rightPath);
|
||
|
|
if (!imgL.isNull()) {
|
||
|
|
displayImageOnLabel(m_labelLeft, imgL);
|
||
|
|
}
|
||
|
|
if (!imgR.isNull()) {
|
||
|
|
displayImageOnLabel(m_labelRight, imgR);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onBrowseCaltab()
|
||
|
|
{
|
||
|
|
QString p = AppSettings::instance().openFile(
|
||
|
|
this, AppSettings::K_Caltab,
|
||
|
|
QString::fromUtf8("选择标定板描述文件"),
|
||
|
|
QString::fromUtf8("视觉引导标定板 (*.cpd *.descr);;所有文件 (*)"),
|
||
|
|
m_editCaltab->text());
|
||
|
|
if (!p.isEmpty()) {
|
||
|
|
const bool changed = p != m_editCaltab->text();
|
||
|
|
m_editCaltab->setText(p);
|
||
|
|
if (changed) invalidateDetectionResults(QString::fromUtf8("标定板文件已改变"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onBrowseSaveDir()
|
||
|
|
{
|
||
|
|
QString d = AppSettings::instance().openDir(
|
||
|
|
this, AppSettings::K_CalibSaveDir,
|
||
|
|
QString::fromUtf8("选择标定结果保存目录"),
|
||
|
|
m_editSaveDir->text());
|
||
|
|
if (!d.isEmpty()) {
|
||
|
|
m_saveDir = d;
|
||
|
|
m_editSaveDir->setText(d);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onLoadLeftIntrinsic()
|
||
|
|
{
|
||
|
|
QString p = AppSettings::instance().openFile(
|
||
|
|
this, AppSettings::K_LeftCal,
|
||
|
|
QString::fromUtf8("加载左相机内参.cal"),
|
||
|
|
QString::fromUtf8("VisionGuide Cal (.cal);;All ()"));
|
||
|
|
if (p.isEmpty()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
ReadCamPar(HTuple(p.toLocal8Bit().constData()), &m_d->preCamParamL);
|
||
|
|
m_d->preLLoaded = true;
|
||
|
|
m_editLeftIntrinsic->setText(p);
|
||
|
|
invalidateDetectionResults(QString::fromUtf8("左相机预内参已改变"));
|
||
|
|
log(QString::fromUtf8("✓ 左相机预内参加载: ") + QFileInfo(p).fileName());
|
||
|
|
} catch (HException& e) {
|
||
|
|
m_d->preLLoaded = false;
|
||
|
|
log(QString::fromUtf8("[错误] 左内参加载失败: ") +
|
||
|
|
sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onLoadRightIntrinsic()
|
||
|
|
{
|
||
|
|
QString p = AppSettings::instance().openFile(
|
||
|
|
this, AppSettings::K_RightCal,
|
||
|
|
QString::fromUtf8("加载右相机内参.cal"),
|
||
|
|
QString::fromUtf8("VisionGuide Cal (.cal);;All ()"));
|
||
|
|
if (p.isEmpty()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
ReadCamPar(HTuple(p.toLocal8Bit().constData()), &m_d->preCamParamR);
|
||
|
|
m_d->preRLoaded = true;
|
||
|
|
m_editRightIntrinsic->setText(p);
|
||
|
|
invalidateDetectionResults(QString::fromUtf8("右相机预内参已改变"));
|
||
|
|
log(QString::fromUtf8("✓ 右相机预内参加载: ") + QFileInfo(p).fileName());
|
||
|
|
} catch (HException& e) {
|
||
|
|
m_d->preRLoaded = false;
|
||
|
|
log(QString::fromUtf8("[错误] 右内参加载失败: ") +
|
||
|
|
sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onCapturePair()
|
||
|
|
{
|
||
|
|
if (!m_camMgr->isConnected(CameraManager::LEFT) ||
|
||
|
|
!m_camMgr->isConnected(CameraManager::RIGHT)) {
|
||
|
|
QMessageBox::warning(this,
|
||
|
|
QString::fromUtf8("相机未连接"),
|
||
|
|
QString::fromUtf8("请先连接左右两台相机!"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (m_sessionImageDir.isEmpty()) {
|
||
|
|
const QString sessionName = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss_zzz");
|
||
|
|
m_sessionImageDir = QDir(QCoreApplication::applicationDirPath())
|
||
|
|
.filePath("calib_images/" + sessionName);
|
||
|
|
}
|
||
|
|
QDir().mkpath(m_sessionImageDir);
|
||
|
|
|
||
|
|
int idx = m_pairs.size() + 1;
|
||
|
|
QString leftPath = QString("%1/left_%2.png")
|
||
|
|
.arg(m_sessionImageDir).arg(idx, 3, 10, QChar('0'));
|
||
|
|
QString rightPath = QString("%1/right_%2.png")
|
||
|
|
.arg(m_sessionImageDir).arg(idx, 3, 10, QChar('0'));
|
||
|
|
|
||
|
|
log(QString::fromUtf8("正在采集第 %1 对图像...").arg(idx));
|
||
|
|
setProgress(10);
|
||
|
|
|
||
|
|
QImage imgL = m_camMgr->captureSingleFrame(CameraManager::LEFT);
|
||
|
|
setProgress(40, QString::fromUtf8("左相机采集完成"));
|
||
|
|
|
||
|
|
QImage imgR = m_camMgr->captureSingleFrame(CameraManager::RIGHT);
|
||
|
|
setProgress(70, QString::fromUtf8("右相机采集完成"));
|
||
|
|
|
||
|
|
if (imgL.isNull() || imgR.isNull()) {
|
||
|
|
log(QString::fromUtf8("[错误] 采集失败!"));
|
||
|
|
setProgress(0);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!imgL.save(leftPath, "PNG") || !imgR.save(rightPath, "PNG")) {
|
||
|
|
log(QString::fromUtf8("[错误] 无损标定图像保存失败: ") + m_sessionImageDir);
|
||
|
|
setProgress(0);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
CalibImagePair pair;
|
||
|
|
pair.index = idx;
|
||
|
|
pair.leftPath = leftPath;
|
||
|
|
pair.rightPath = rightPath;
|
||
|
|
m_pairs.append(pair);
|
||
|
|
|
||
|
|
refreshList();
|
||
|
|
displayImageOnLabel(m_labelLeft, imgL);
|
||
|
|
displayImageOnLabel(m_labelRight, imgR);
|
||
|
|
|
||
|
|
if (m_treePairs->topLevelItemCount() > 0) {
|
||
|
|
m_treePairs->setCurrentItem(
|
||
|
|
m_treePairs->topLevelItem(m_treePairs->topLevelItemCount() - 1));
|
||
|
|
}
|
||
|
|
setProgress(100, QString::fromUtf8("第 %1 对图像采集成功").arg(idx));
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onValidateAll()
|
||
|
|
{
|
||
|
|
if (m_pairs.isEmpty()) {
|
||
|
|
log(QString::fromUtf8("没有图像对可以检测"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
log(QString::fromUtf8("开始检测所有未验证的图像对..."));
|
||
|
|
int skipped = 0;
|
||
|
|
for (int i = 0; i < m_pairs.size(); ++i) {
|
||
|
|
// ✓ 跳过已验证的,提升重复操作速度
|
||
|
|
if (m_pairs[i].validated) {
|
||
|
|
++skipped;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
setProgress(
|
||
|
|
(i + 1) * 100 / m_pairs.size(),
|
||
|
|
QString::fromUtf8("检测 %1/%2...").arg(i + 1).arg(m_pairs.size()));
|
||
|
|
validateSinglePair(i);
|
||
|
|
}
|
||
|
|
refreshList();
|
||
|
|
setProgress(100, QString::fromUtf8("验证完成 (跳过已验证: %1 对)").arg(skipped));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool StereoCalibDialog::validateSinglePair(int idx)
|
||
|
|
{
|
||
|
|
auto& p = m_pairs[idx];
|
||
|
|
QString caltabFile = m_editCaltab->text();
|
||
|
|
if (!QFile::exists(caltabFile)) {
|
||
|
|
log(QString::fromUtf8("[错误] 找不到标定板文件: ") + caltabFile);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
HTuple tmpHandle;
|
||
|
|
CreateCalibData("calibration_object", 2, 1, &tmpHandle);
|
||
|
|
HTuple initParamL = m_d->preLLoaded
|
||
|
|
? m_d->preCamParamL : Impl::initialCamParam();
|
||
|
|
HTuple initParamR = m_d->preRLoaded
|
||
|
|
? m_d->preCamParamR : Impl::initialCamParam();
|
||
|
|
SetCalibDataCamParam(tmpHandle, 0, HTuple(), initParamL);
|
||
|
|
SetCalibDataCamParam(tmpHandle, 1, HTuple(), initParamR);
|
||
|
|
SetCalibDataCalibObject(tmpHandle, 0,
|
||
|
|
HTuple(caltabFile.toLocal8Bit().constData()));
|
||
|
|
|
||
|
|
QImage gL = QImage(p.leftPath)
|
||
|
|
.convertToFormat(QImage::Format_Grayscale8).copy();
|
||
|
|
QImage gR = QImage(p.rightPath)
|
||
|
|
.convertToFormat(QImage::Format_Grayscale8).copy();
|
||
|
|
|
||
|
|
HObject hL, hR;
|
||
|
|
GenImage1(&hL, "byte", gL.width(), gL.height(), (Hlong)gL.constBits());
|
||
|
|
GenImage1(&hR, "byte", gR.width(), gR.height(), (Hlong)gR.constBits());
|
||
|
|
|
||
|
|
p.validLeft = p.validRight = false;
|
||
|
|
try {
|
||
|
|
Impl::findCalibObject(hL, tmpHandle, 0, 0, 0,
|
||
|
|
m_comboBoardType->currentData().toInt(),
|
||
|
|
m_spinFindSigma->value(), m_spinFindAlpha->value(),
|
||
|
|
m_spinGapTolerance->value(), m_spinMaxDiamMarks->value(),
|
||
|
|
m_checkSkipFindCaltab->isChecked());
|
||
|
|
p.validLeft = true;
|
||
|
|
} catch (HException& e) {
|
||
|
|
log(p.displayName() + QString::fromUtf8(" 左检测失败: ")
|
||
|
|
+ sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
} catch (...) {
|
||
|
|
log(p.displayName() + QString::fromUtf8(" 左检测失败: 未知视觉算法异常"));
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
Impl::findCalibObject(hR, tmpHandle, 1, 0, 0,
|
||
|
|
m_comboBoardType->currentData().toInt(),
|
||
|
|
m_spinFindSigma->value(), m_spinFindAlpha->value(),
|
||
|
|
m_spinGapTolerance->value(), m_spinMaxDiamMarks->value(),
|
||
|
|
m_checkSkipFindCaltab->isChecked());
|
||
|
|
p.validRight = true;
|
||
|
|
} catch (HException& e) {
|
||
|
|
log(p.displayName() + QString::fromUtf8(" 右检测失败: ")
|
||
|
|
+ sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
} catch (...) {
|
||
|
|
log(p.displayName() + QString::fromUtf8(" 右检测失败: 未知视觉算法异常"));
|
||
|
|
}
|
||
|
|
|
||
|
|
p.validated = true;
|
||
|
|
try {
|
||
|
|
ClearCalibData(tmpHandle);
|
||
|
|
} catch (...) {}
|
||
|
|
|
||
|
|
const QString detectorInfo = m_comboBoardType->currentData().toInt() == kHoneycombBoard
|
||
|
|
? QString::fromUtf8("sigma=%1").arg(m_spinFindSigma->value(), 0, 'f', 2)
|
||
|
|
: QString::fromUtf8("alpha=%1, gap=%2").arg(m_spinFindAlpha->value(), 0, 'f', 2)
|
||
|
|
.arg(m_spinGapTolerance->value(), 0, 'f', 2);
|
||
|
|
log(p.displayName() + QString::fromUtf8(": L=") + (p.validLeft? "OK" : "NG") +
|
||
|
|
QString::fromUtf8(" R=") + (p.validRight? "OK" : "NG") +
|
||
|
|
" (" + detectorInfo + ")");
|
||
|
|
return p.validLeft && p.validRight;
|
||
|
|
|
||
|
|
} catch (HException& e) {
|
||
|
|
log("[VisionGuide] " + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
p.validated = true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onPairClicked(QTreeWidgetItem* item, int /*col*/)
|
||
|
|
{
|
||
|
|
if (!item) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
displayPair(item->data(0, Qt::UserRole).toInt());
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onExcludeSelected()
|
||
|
|
{
|
||
|
|
for (auto* it : m_treePairs->selectedItems()) {
|
||
|
|
m_pairs[it->data(0, Qt::UserRole).toInt()].excluded = true;
|
||
|
|
}
|
||
|
|
refreshList();
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onIncludeSelected()
|
||
|
|
{
|
||
|
|
for (auto* it : m_treePairs->selectedItems()) {
|
||
|
|
m_pairs[it->data(0, Qt::UserRole).toInt()].excluded = false;
|
||
|
|
}
|
||
|
|
refreshList();
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onCalibrate()
|
||
|
|
{
|
||
|
|
QList<int> usedIdx;
|
||
|
|
for (int i = 0; i < m_pairs.size(); ++i) {
|
||
|
|
const auto& p = m_pairs[i];
|
||
|
|
if (!p.excluded && p.validated && p.validLeft && p.validRight) {
|
||
|
|
usedIdx.append(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (usedIdx.size() < kMinimumStereoPairs) {
|
||
|
|
QMessageBox::warning(this,
|
||
|
|
QString::fromUtf8("图像对不足"),
|
||
|
|
QString::fromUtf8("至少需要 %1 对有效图像才能标定,建议采集 15–20 对并覆盖视野四周。\n请先运行 [CHK] 检测。")
|
||
|
|
.arg(kMinimumStereoPairs));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
log(QString::fromUtf8("开始双目标定(完整模式),使用 %1 对...").arg(usedIdx.size()));
|
||
|
|
setProgress(5);
|
||
|
|
if (runCalibration(usedIdx, /*refineExtrinsicsOnly=*/false)) {
|
||
|
|
m_btnComputeErrors->setEnabled(true);
|
||
|
|
m_btnSave->setEnabled(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onRefineExtrinsics()
|
||
|
|
{
|
||
|
|
if (!m_d->preLLoaded ||!m_d->preRLoaded) {
|
||
|
|
QMessageBox::warning(this,
|
||
|
|
QString::fromUtf8("内参未加载"),
|
||
|
|
QString::fromUtf8(
|
||
|
|
"[EXT] 仅优化外参功能要求:\n"
|
||
|
|
" 1. 加载左相机预内参 (.cal)\n"
|
||
|
|
" 2. 加载右相机预内参 (.cal)\n\n"
|
||
|
|
"请在顶部\"预内参\"栏中加载单目标定得到的.cal 文件。"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
QList<int> usedIdx;
|
||
|
|
for (int i = 0; i < m_pairs.size(); ++i) {
|
||
|
|
const auto& p = m_pairs[i];
|
||
|
|
if (!p.excluded && p.validated && p.validLeft && p.validRight) {
|
||
|
|
usedIdx.append(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (usedIdx.size() < kMinimumStereoPairs) {
|
||
|
|
QMessageBox::warning(this,
|
||
|
|
QString::fromUtf8("图像对不足"),
|
||
|
|
QString::fromUtf8("至少需要 %1 对有效图像,建议采集 15–20 对。\n请先运行 [CHK] 检测。")
|
||
|
|
.arg(kMinimumStereoPairs));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
log(QString::fromUtf8("═══ 仅优化外参模式 (内参固定) ═══"));
|
||
|
|
log(QString::fromUtf8(" 使用 %1 对有效图像").arg(usedIdx.size()));
|
||
|
|
setProgress(5);
|
||
|
|
if (runCalibration(usedIdx, /*refineExtrinsicsOnly=*/true)) {
|
||
|
|
m_btnComputeErrors->setEnabled(true);
|
||
|
|
m_btnSave->setEnabled(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
||
|
|
// 核心标定 - 支持"完整" 和 "仅外参" 两种模式
|
||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
||
|
|
bool StereoCalibDialog::runCalibration(const QList<int>& usedIndices,
|
||
|
|
bool refineExtrinsicsOnly)
|
||
|
|
{
|
||
|
|
QString caltabFile = m_editCaltab->text();
|
||
|
|
if (!QFile::exists(caltabFile)) {
|
||
|
|
log(QString::fromUtf8("[错误] 标定板文件不存在: ") + caltabFile);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
m_d->clearHandle();
|
||
|
|
CreateCalibData("calibration_object", 2, 1, &m_d->calibHandle);
|
||
|
|
m_d->handleValid = true;
|
||
|
|
|
||
|
|
// 内参初值
|
||
|
|
HTuple initParamL, initParamR;
|
||
|
|
if (refineExtrinsicsOnly) {
|
||
|
|
initParamL = m_d->preCamParamL;
|
||
|
|
initParamR = m_d->preCamParamR;
|
||
|
|
log(QString::fromUtf8(" 使用加载的预标定内参"));
|
||
|
|
} else {
|
||
|
|
// 完整模式仍允许优化内参,但如果用户已经加载了单目标定
|
||
|
|
// 结果,应以它们作为优化初值,而不是回到粗略的 0.035 m。
|
||
|
|
initParamL = m_d->preLLoaded
|
||
|
|
? m_d->preCamParamL : Impl::initialCamParam();
|
||
|
|
initParamR = m_d->preRLoaded
|
||
|
|
? m_d->preCamParamR : Impl::initialCamParam();
|
||
|
|
if (m_d->preLLoaded || m_d->preRLoaded)
|
||
|
|
log(QString::fromUtf8(" 使用已加载单目标定参数作为完整标定初值"));
|
||
|
|
else
|
||
|
|
log(QString::fromUtf8(" 未加载单目标定参数,使用默认初值"));
|
||
|
|
}
|
||
|
|
|
||
|
|
SetCalibDataCamParam(m_d->calibHandle, 0, HTuple(), initParamL);
|
||
|
|
SetCalibDataCamParam(m_d->calibHandle, 1, HTuple(), initParamR);
|
||
|
|
SetCalibDataCalibObject(m_d->calibHandle, 0,
|
||
|
|
HTuple(caltabFile.toLocal8Bit().constData()));
|
||
|
|
|
||
|
|
// 已知像元尺寸来自相机规格,固定 sx/sy 可避免它们与焦距互相补偿。
|
||
|
|
// 仅外参模式则固定全部内部参数,但保留相机相对位姿参与优化。
|
||
|
|
if (refineExtrinsicsOnly) {
|
||
|
|
SetCalibData(m_d->calibHandle, "camera", "general",
|
||
|
|
"excluded_settings", "params");
|
||
|
|
log(QString::fromUtf8(" ✓ 已固定两台相机全部内参,仅优化双目外参"));
|
||
|
|
} else {
|
||
|
|
HTuple fixedCellSize;
|
||
|
|
fixedCellSize.Append("sx");
|
||
|
|
fixedCellSize.Append("sy");
|
||
|
|
SetCalibData(m_d->calibHandle, "camera", "general",
|
||
|
|
"excluded_settings", fixedCellSize);
|
||
|
|
log(QString::fromUtf8(" ✓ 完整模式固定已知像元尺寸 sx/sy"));
|
||
|
|
}
|
||
|
|
|
||
|
|
log(QString::fromUtf8("正在提取标定点..."));
|
||
|
|
int n = usedIndices.size();
|
||
|
|
for (int k = 0; k < n; ++k) {
|
||
|
|
int i = usedIndices[k];
|
||
|
|
setProgress(10 + k * 50 / n,
|
||
|
|
QString::fromUtf8("提取 %1/%2: %3")
|
||
|
|
.arg(k + 1).arg(n).arg(m_pairs[i].displayName()));
|
||
|
|
|
||
|
|
QImage gL = QImage(m_pairs[i].leftPath)
|
||
|
|
.convertToFormat(QImage::Format_Grayscale8).copy();
|
||
|
|
QImage gR = QImage(m_pairs[i].rightPath)
|
||
|
|
.convertToFormat(QImage::Format_Grayscale8).copy();
|
||
|
|
|
||
|
|
HObject hL, hR;
|
||
|
|
GenImage1(&hL, "byte", gL.width(), gL.height(), (Hlong)gL.constBits());
|
||
|
|
GenImage1(&hR, "byte", gR.width(), gR.height(), (Hlong)gR.constBits());
|
||
|
|
|
||
|
|
Impl::findCalibObject(hL, m_d->calibHandle, 0, 0, k,
|
||
|
|
m_comboBoardType->currentData().toInt(),
|
||
|
|
m_spinFindSigma->value(), m_spinFindAlpha->value(),
|
||
|
|
m_spinGapTolerance->value(), m_spinMaxDiamMarks->value(),
|
||
|
|
m_checkSkipFindCaltab->isChecked());
|
||
|
|
Impl::findCalibObject(hR, m_d->calibHandle, 1, 0, k,
|
||
|
|
m_comboBoardType->currentData().toInt(),
|
||
|
|
m_spinFindSigma->value(), m_spinFindAlpha->value(),
|
||
|
|
m_spinGapTolerance->value(), m_spinMaxDiamMarks->value(),
|
||
|
|
m_checkSkipFindCaltab->isChecked());
|
||
|
|
}
|
||
|
|
|
||
|
|
setProgress(65, QString::fromUtf8("正在计算标定结果,请稍候..."));
|
||
|
|
HTuple errors;
|
||
|
|
CalibrateCameras(m_d->calibHandle, &errors);
|
||
|
|
m_overallRMSE = errors[0].D();
|
||
|
|
|
||
|
|
setProgress(80, QString::fromUtf8("标定完成! 整体 RMSE = %1 px")
|
||
|
|
.arg(m_overallRMSE, 0, 'f', 4));
|
||
|
|
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 0, "params", &m_d->camParamL);
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 1, "params", &m_d->camParamR);
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 1, "pose", &m_d->relPose);
|
||
|
|
|
||
|
|
QString quality, style;
|
||
|
|
if (m_overallRMSE < 0.5) {
|
||
|
|
quality = QString::fromUtf8("优秀");
|
||
|
|
style = "font-weight:bold;color:#00cc66;font-size:13px;";
|
||
|
|
} else if (m_overallRMSE < 1.0) {
|
||
|
|
quality = QString::fromUtf8("良好");
|
||
|
|
style = "font-weight:bold;color:#f39c12;font-size:13px;";
|
||
|
|
} else {
|
||
|
|
quality = QString::fromUtf8("偏高,建议剔除误差大的图像对");
|
||
|
|
style = "font-weight:bold;color:#e74c3c;font-size:13px;";
|
||
|
|
}
|
||
|
|
QString mode = refineExtrinsicsOnly
|
||
|
|
? QString::fromUtf8("仅外参") : QString::fromUtf8("完整");
|
||
|
|
m_labelRMSE->setText(
|
||
|
|
QString::fromUtf8("[%1] 整体 RMSE: %2 px (%3)")
|
||
|
|
.arg(mode).arg(m_overallRMSE, 0, 'f', 4).arg(quality));
|
||
|
|
m_labelRMSE->setStyleSheet(style);
|
||
|
|
|
||
|
|
setProgress(100, QString::fromUtf8("标定完成"));
|
||
|
|
m_hasResult = true;
|
||
|
|
|
||
|
|
for (int i : usedIndices) {
|
||
|
|
m_pairs[i].reproj = -2.0;
|
||
|
|
}
|
||
|
|
refreshList();
|
||
|
|
return true;
|
||
|
|
|
||
|
|
} catch (HException& e) {
|
||
|
|
QString msg = sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA()));
|
||
|
|
log("[VisionGuide] " + QString::fromUtf8("标定失败: ") + msg);
|
||
|
|
setProgress(0);
|
||
|
|
QMessageBox::critical(this,
|
||
|
|
QString::fromUtf8("标定失败"), "VisionGuide: " + msg);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onComputeErrors()
|
||
|
|
{
|
||
|
|
if (!m_d->handleValid ||!m_hasResult) {
|
||
|
|
log(QString::fromUtf8("请先完成标定"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
QList<int> usedIdx;
|
||
|
|
for (int i = 0; i < m_pairs.size(); ++i) {
|
||
|
|
const auto& p = m_pairs[i];
|
||
|
|
if (!p.excluded && p.validated && p.validLeft && p.validRight) {
|
||
|
|
usedIdx.append(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
computePerImageErrors(usedIdx);
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::computePerImageErrors(const QList<int>& usedIndices)
|
||
|
|
{
|
||
|
|
log(QString::fromUtf8("正在计算各对重投影误差..."));
|
||
|
|
int n = usedIndices.size();
|
||
|
|
try {
|
||
|
|
HTuple camParamL, camParamR;
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 0, "params", &camParamL);
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 1, "params", &camParamR);
|
||
|
|
|
||
|
|
HTuple objX, objY, objZ;
|
||
|
|
GetCalibData(m_d->calibHandle, "calib_obj", 0, "x", &objX);
|
||
|
|
GetCalibData(m_d->calibHandle, "calib_obj", 0, "y", &objY);
|
||
|
|
GetCalibData(m_d->calibHandle, "calib_obj", 0, "z", &objZ);
|
||
|
|
|
||
|
|
double maxErr = 0;
|
||
|
|
int maxIdx = -1;
|
||
|
|
|
||
|
|
for (int k = 0; k < n; ++k) {
|
||
|
|
int i = usedIndices[k];
|
||
|
|
double sumSq = 0;
|
||
|
|
int totalPts = 0;
|
||
|
|
|
||
|
|
HTuple poseItem;
|
||
|
|
poseItem.Append(0);
|
||
|
|
poseItem.Append(k);
|
||
|
|
HTuple objectPoseInLeft, objectInLeft;
|
||
|
|
GetCalibData(m_d->calibHandle, "calib_obj_pose", poseItem,
|
||
|
|
"pose", &objectPoseInLeft);
|
||
|
|
PoseToHomMat3d(objectPoseInLeft, &objectInLeft);
|
||
|
|
|
||
|
|
for (int cam = 0; cam < 2; ++cam) {
|
||
|
|
HTuple camParam = (cam == 0)? camParamL : camParamR;
|
||
|
|
|
||
|
|
try {
|
||
|
|
HTuple rowObs, colObs, idxObs, poseObs;
|
||
|
|
GetCalibDataObservPoints(m_d->calibHandle, cam, 0, k,
|
||
|
|
&rowObs, &colObs, &idxObs, &poseObs);
|
||
|
|
|
||
|
|
HTuple objectInCamera = objectInLeft;
|
||
|
|
if (cam == 1) {
|
||
|
|
HTuple rightPoseInLeft, rightInLeft, leftInRight;
|
||
|
|
GetCalibData(m_d->calibHandle, "camera", 1,
|
||
|
|
"pose", &rightPoseInLeft);
|
||
|
|
PoseToHomMat3d(rightPoseInLeft, &rightInLeft);
|
||
|
|
HomMat3dInvert(rightInLeft, &leftInRight);
|
||
|
|
HomMat3dCompose(leftInRight, objectInLeft, &objectInCamera);
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int p = 0; p < rowObs.Length(); ++p) {
|
||
|
|
int ptIdx = idxObs[p].I();
|
||
|
|
if (ptIdx < 0 || ptIdx >= objX.Length()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
HTuple Xw = objX[ptIdx], Yw = objY[ptIdx], Zw = objZ[ptIdx];
|
||
|
|
HTuple Xc, Yc, Zc;
|
||
|
|
AffineTransPoint3d(objectInCamera, Xw, Yw, Zw, &Xc, &Yc, &Zc);
|
||
|
|
HTuple rProj, cProj;
|
||
|
|
Project3dPoint(Xc, Yc, Zc, camParam, &rProj, &cProj);
|
||
|
|
|
||
|
|
double dr = rProj.D() - rowObs[p].D();
|
||
|
|
double dc = cProj.D() - colObs[p].D();
|
||
|
|
sumSq += dr * dr + dc * dc;
|
||
|
|
totalPts++;
|
||
|
|
}
|
||
|
|
} catch (HException& e) {
|
||
|
|
log(QString::fromUtf8(" %1 cam%2: %3")
|
||
|
|
.arg(m_pairs[i].displayName()).arg(cam)
|
||
|
|
.arg(sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA()))));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
double pairRmse = (totalPts > 0)
|
||
|
|
? std::sqrt(sumSq / totalPts)
|
||
|
|
: -1.0;
|
||
|
|
m_pairs[i].reproj = pairRmse;
|
||
|
|
|
||
|
|
if (pairRmse > maxErr) {
|
||
|
|
maxErr = pairRmse;
|
||
|
|
maxIdx = i;
|
||
|
|
}
|
||
|
|
|
||
|
|
log(QString::fromUtf8(" %1: RMSE = %2 px (总点数=%3)")
|
||
|
|
.arg(m_pairs[i].displayName())
|
||
|
|
.arg(pairRmse >= 0? QString::number(pairRmse, 'f', 4) : "N/A")
|
||
|
|
.arg(totalPts));
|
||
|
|
setProgress((k + 1) * 100 / n);
|
||
|
|
}
|
||
|
|
|
||
|
|
refreshList();
|
||
|
|
setProgress(100, QString::fromUtf8("各对误差计算完成"));
|
||
|
|
|
||
|
|
if (maxIdx >= 0) {
|
||
|
|
log(QString::fromUtf8("最高误差: %1 = %2 px (建议考虑剔除)")
|
||
|
|
.arg(m_pairs[maxIdx].displayName())
|
||
|
|
.arg(maxErr, 0, 'f', 4));
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (HException& e) {
|
||
|
|
log("[VisionGuide] " + QString::fromUtf8("计算异常: ") +
|
||
|
|
sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void StereoCalibDialog::onSave()
|
||
|
|
{
|
||
|
|
if (!m_hasResult ||!m_d->handleValid) {
|
||
|
|
log(QString::fromUtf8("无标定结果可保存"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
m_saveDir = m_editSaveDir->text();
|
||
|
|
QDir().mkpath(m_saveDir);
|
||
|
|
m_leftCalFile = m_saveDir + "/cam_left.cal";
|
||
|
|
m_rightCalFile = m_saveDir + "/cam_right.cal";
|
||
|
|
m_relPoseFile = m_saveDir + "/pos_right2left.dat";
|
||
|
|
|
||
|
|
try {
|
||
|
|
WriteCamPar(m_d->camParamL,
|
||
|
|
HTuple(m_leftCalFile.toLocal8Bit().constData()));
|
||
|
|
WriteCamPar(m_d->camParamR,
|
||
|
|
HTuple(m_rightCalFile.toLocal8Bit().constData()));
|
||
|
|
WritePose(m_d->relPose,
|
||
|
|
HTuple(m_relPoseFile.toLocal8Bit().constData()));
|
||
|
|
|
||
|
|
// 把刚保存的三个路径写进 AppSettings
|
||
|
|
auto& s = AppSettings::instance();
|
||
|
|
s.set(AppSettings::K_LeftCal, m_leftCalFile);
|
||
|
|
s.set(AppSettings::K_RightCal, m_rightCalFile);
|
||
|
|
s.set(AppSettings::K_StereoCalDir, m_relPoseFile);
|
||
|
|
s.set(AppSettings::K_CalibSaveDir, m_saveDir);
|
||
|
|
s.set(AppSettings::K_Caltab, m_editCaltab->text());
|
||
|
|
|
||
|
|
log(QString::fromUtf8("标定结果已保存:"));
|
||
|
|
log(" " + m_leftCalFile);
|
||
|
|
log(" " + m_rightCalFile);
|
||
|
|
log(" " + m_relPoseFile);
|
||
|
|
|
||
|
|
QMessageBox::information(this,
|
||
|
|
QString::fromUtf8("保存成功"),
|
||
|
|
QString::fromUtf8("标定结果已保存至:\n")
|
||
|
|
+ m_leftCalFile + "\n" + m_rightCalFile + "\n" + m_relPoseFile);
|
||
|
|
|
||
|
|
accept();
|
||
|
|
|
||
|
|
} catch (HException& e) {
|
||
|
|
QString msg = sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA()));
|
||
|
|
log(QString::fromUtf8("[错误] 保存失败: ") + msg);
|
||
|
|
QMessageBox::critical(this,
|
||
|
|
QString::fromUtf8("保存失败"), msg);
|
||
|
|
}
|
||
|
|
}
|