#include "MainForm.h"
#include "ui_MainForm.h"
#include <QDebug>
#include <opencv2/imgproc.hpp>
MainForm::MainForm(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainForm)
{
m_ImageWidget = nullptr;
p_CalibMethod = new Calibration();
ui->setupUi(this);
// 循环连接 valueChanged() 信号到槽函数
for (QSpinBox* spinBox : findChildren<QSpinBox*>())
{
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainForm::SpinBoxValueChanged);
}
ui->imageBox->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->imageBox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->spinBox_CB_Cols->setValue(9);
ui->spinBox_CB_Rows->setValue(6);
QRegExpValidator* validator = new QRegExpValidator(QRegExp("[0-9]*"), ui->lineEdit_SquareSize);
ui->lineEdit_SquareSize->setValidator(validator);
ui->lineEdit_SquareSize->setText("1.0");
m_ResultTable = new QStandardItemModel(4, 10);//总计8列数据,2行间隔
m_ResultTable->setData(m_ResultTable->index(0, 0), QStringLiteral("相机内参:"));
m_ResultTable->setData(m_ResultTable->index(0, 4), QStringLiteral("畸变系数:"));
m_ResultTable->setData(m_ResultTable->index(0, 8), QStringLiteral("相机位姿:"));
// 隐藏列头
QHeaderView* headerView = ui->tableView_Results->horizontalHeader();
headerView->setVisible(false);
// 隐藏行头
QHeaderView* verticalHeaderView = ui->tableView_Results->verticalHeader();
verticalHeaderView->setVisible(false);
ui->tableView_Results->setModel(m_ResultTable);
ui->tableView_Results->show();
}
MainForm::~MainForm()
{
delete ui;
}
void MainForm::RecvShowImageSignal(QImage image)
{
if (image == QImage())
{
return;
}
QPixmap ConvertPixmap = QPixmap::fromImage(image);
QGraphicsScene* qgraphicsScene = new QGraphicsScene;
m_ImageWidget = new ImageWidget(&ConvertPixmap);
m_ImageWidget->setQGraphicsViewWH(ui->imageBox->width(), ui->imageBox->height());
qgraphicsScene->addItem(m_ImageWidget);
ui->imageBox->setSceneRect(QRectF(-(ui->imageBox->width() / 2), -(ui->imageBox->height() / 2), ui->imageBox->width(), ui->imageBox->height()));
ui->imageBox->setScene(qgraphicsScene);
ui->imageBox->setFocus();
}
void MainForm::on_browseButton_clicked()
{
QString directory = QFileDialog::getExistingDirectory(this, tr("Select Image Directory"));
if (!directory.isEmpty())
{
try
{
QDir dir(directory);
ui->imagePathLineEdit->setText(directory);
QStringList imageFilter;
imageFilter << "*.png" << "*.jpg" << "*.jpeg" << "*.bmp";
QStringList files = dir.entryList(imageFilter, QDir::Files);
m_ImageMatList.clear();
m_ImageNameList.clear();
foreach(QString file, files)
{
QString imagePath = directory + "/" + file;
cv::Mat image = cv::imread(imagePath.toStdString());
m_ImageNameList.push_back(file);
m_ImageMatList.push_back(image);
}
m_CvImage = m_ImageMatList[0].clone();
ShowImage(m_CvImage);
UpdateImageListWidget();
}
catch (const std::exception& Err)
{
QMessageBox::information(this, QStringLiteral("错误"), Err.what());
return;
}
}
}
void MainForm::ShowImage(const cv::Mat& image)
{
//三通道RGB
if (image.type() == CV_8UC3)
m_QImage = QImage((const unsigned char*)image.data, image.cols, image.rows, QImage::Format::Format_RGB888);
//四通道RGBA
else if (image.type() == CV_8UC4)
m_QImage = QImage((const unsigned char*)image.data, image.cols, image.rows, QImage::Format::Format_RGBA8888_Premultiplied);
//单通道Gray
else if (image.type() == CV_8UC1)
m_QImage = QImage((const unsigned char*)image.data, image.cols, image.rows, QImage::Format::Format_Grayscale8);
else
return;
RecvShowImageSignal(m_QImage);
//更新显示
ui->imageBox->show();
}
void MainForm::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);
ShowImage(m_CvImage);
}
void MainForm::SpinBoxValueChanged(int value)
{
// 使用 QObject::sender() 获取发送信号的对象
QSpinBox* spinBox = qobject_cast<QSpinBox*>(sender());
if (spinBox != nullptr)
{
// 根据 QSpinBox 进行相应的操作
QString spinBoxObjectName = spinBox->objectName();
if (spinBoxObjectName == "spinBox_CB_Rows")
p_CalibMethod->setCornersRows(spinBox->value());
else if (spinBoxObjectName == "spinBox_CB_Cols")
p_CalibMethod->setCornersCols(spinBox->value());
}
}
void MainForm::on_lineEdit_SquareSize_textChanged(const QString& text)
{
p_CalibMethod->setSquareSize(text.toFloat());
}
void MainForm::UpdateImageListWidget()
{
ui->imageListWidget->clear();
for (size_t i = 0; i < m_ImageNameList.size(); i++)
{
cv::Mat image = m_ImageMatList[i];
QString imageName = m_ImageNameList[i];
QListWidgetItem* item = new QListWidgetItem(QIcon(QPixmap::fromImage(QImage(image.data, image.cols, image.rows, image.step, QImage::Format_RGB888).rgbSwapped())), imageName);
ui->imageListWidget->addItem(item);
}
}
void MainForm::on_imageListWidget_itemClicked(QListWidgetItem* item)
{
int currentItemIndex = ui->imageListWidget->currentRow();
if (currentItemIndex >= 0 && currentItemIndex < m_ImageMatList.size())
{
m_CvImage = m_ImageMatList[currentItemIndex].clone();
ui->imageBox->resetMatrix();
if (ui->checkBox_isDrawCorners->isChecked())
{
p_CalibMethod->drawCorners(m_CvImage);
}
else if (ui->checkBox_isUndistort->isChecked())
{
cv::Mat image = p_CalibMethod->undistortImage(m_CvImage.clone());
}
ShowImage(m_CvImage);
ShowCameraPoseResult(m_CvImage);
}
}
void MainForm::on_Button_Execute_clicked()
{
if (!m_ImageMatList.size())
{
return;
}
bool isCalibrated = p_CalibMethod->calibrate(m_ImageMatList);
if (isCalibrated)
{
ShowCameraParamResult();
}
}
void MainForm::on_Button_Export_clicked()
{
if (p_CalibMethod->calibrated)
{
try
{
ExportTableView(ui->tableView_Results, ui->imagePathLineEdit->text() + "/Results.csv");
}
catch (const std::exception& Err)
{
QString strErr = Err.what();
}
}
}
void MainForm::on_checkBox_isDrawCorners_stateChanged(int state)
{
if (state == Qt::Checked)
{
ui->imageBox->resetMatrix();
cv::Mat image = m_CvImage.clone();
p_CalibMethod->drawCorners(image);
ShowImage(image);
}
else if (state == Qt::Unchecked)
{
ShowImage(m_CvImage);
}
}
void MainForm::on_checkBox_isUndistort_stateChanged(int state)
{
if (state == Qt::Checked)
{
ui->imageBox->resetMatrix();
cv::Mat image = p_CalibMethod->undistortImage(m_CvImage.clone());
ShowImage(image);
}
else if (state == Qt::Unchecked)
{
ShowImage(m_CvImage);
}
}
void MainForm::ShowCameraParamResult()
{
m_CameraMatrix = p_CalibMethod->getCameraMatrix();
m_DistortCoeffs = p_CalibMethod->getDistortionCoefficients();
//m_ResultTable->clear();
for (int row = 0; row < m_CameraMatrix.rows; ++row)
{
for (int col = 0; col < m_CameraMatrix.cols; ++col)
{
m_ResultTable->setData(m_ResultTable->index(row + 1, col), QString::number(m_CameraMatrix.at<double>(row, col)));
}
}
int currentIndex = 0;
for (int row = 0; row < 3; ++row)
{
for (int column = 0; column < 3; ++column)
{
QString data = "";
if (currentIndex < m_DistortCoeffs.rows)
{
data = QString::number(m_DistortCoeffs.at<double>(currentIndex, 0), 'f', 5);
// 更新当前向量索引
++currentIndex;
}
m_ResultTable->setData(m_ResultTable->index(row + 1, column + 4), data);
}
}
for (int row = 0; row < m_DistortCoeffs.rows; ++row)
{
for (int col = 0; col < m_DistortCoeffs.cols; ++col)
{
m_ResultTable->setData(m_ResultTable->index(row + 1, col + 4), QString::number(m_DistortCoeffs.at<double>(row, col)));
}
}
//ui->tableView_Results->resizeColumnsToContents();
//ui->tableView_Results->resizeRowsToContents();
ui->tableView_Results->update();
//ui->tableView_Results->show();
}
void MainForm::ShowCameraPoseResult(cv::Mat I
没有合适的资源?快使用搜索试试~ 我知道了~
【Opencv+QT】棋盘格标定助手
共818个文件
hpp:496个
h:62个
jpg:53个
4 下载量 149 浏览量
2023-08-19
16:36:30
上传
评论
收藏 153.96MB ZIP 举报
温馨提示
基于Opencv4.7.0开发的棋盘格标定助手 资源名称:Opencv棋盘格标定助手 版本信息:Visual Studio 2022、QT5.9.9 主要功能:针对使用棋盘格标定板的相机内参标定。 技术要求:Window10/11 x64,Opencv 使用说明:导入棋盘格图像和设置相应的角点和格子尺寸就可以直接标定相机内参了,畸变系数默认使用了8个,可以在代码中自行设置,附带了结果导出。 其他说明:标定误差结果估计的部分暂未添加进去,有需要的自行开发。
资源推荐
资源详情
资源评论
收起资源包目录
【Opencv+QT】棋盘格标定助手 (818个子文件)
moc_predefs.h.cbt 55B
moc_predefs.h.cbt 55B
MainForm.cpp 10KB
Calibration.cpp 7KB
moc_MainForm.cpp 6KB
moc_MainForm.cpp 6KB
moc_qcameracalibrate.cpp 5KB
moc_qchessboardelab.cpp 5KB
qrc_MainForm.cpp 4KB
qrc_MainForm.cpp 4KB
ImageWidget.cpp 3KB
main.cpp 260B
Results.csv 167B
opencv_world470d.dll 103.9MB
opencv_world470d.dll 103.9MB
opencv_world470.dll 43.54MB
opencv_world470.dll 43.54MB
opencv_videoio_ffmpeg470_64.dll 25.02MB
opengl32sw.dll 19.95MB
opengl32sw.dll 19.95MB
Qt5Guid.dll 13.24MB
Qt5Cored.dll 12.66MB
Qt5Widgetsd.dll 11.21MB
libGLESV2d.dll 11.06MB
Qt5Gui.dll 5.78MB
Qt5Core.dll 5.57MB
Qt5Widgets.dll 5.34MB
D3Dcompiler_47.dll 3.98MB
D3Dcompiler_47.dll 3.98MB
qwindowsd.dll 3.64MB
libGLESV2.dll 2.4MB
qwindows.dll 1.29MB
qwebpd.dll 1.14MB
qtiffd.dll 797KB
Qt5Svgd.dll 728KB
qjpegd.dll 527KB
qwebp.dll 485KB
opencv_img_hash470d.dll 460KB
qtiff.dll 376KB
Qt5Svg.dll 327KB
qjpeg.dll 235KB
opencv_img_hash470.dll 163KB
qicnsd.dll 134KB
qsvgicond.dll 116KB
qgifd.dll 110KB
qicod.dll 110KB
qtgad.dll 89KB
qsvgd.dll 87KB
qwbmpd.dll 85KB
libEGLd.dll 66KB
qicns.dll 46KB
qsvgicon.dll 40KB
qico.dll 37KB
qgif.dll 37KB
qtga.dll 30KB
qsvg.dll 30KB
qwbmp.dll 29KB
libEGL.dll 21KB
opencv_interactive-calibrationd.exe 488KB
OpencvCalibration.exe 366KB
opencv_visualisationd.exe 196KB
opencv_annotationd.exe 175KB
opencv_interactive-calibration.exe 134KB
opencv_versiond.exe 120KB
opencv_version_win32d.exe 118KB
OpencvCalibration.exe 117KB
opencv_model_diagnosticsd.exe 96KB
opencv_visualisation.exe 57KB
opencv_annotation.exe 41KB
opencv_version.exe 36KB
opencv_version_win32.exe 35KB
opencv_model_diagnostics.exe 21KB
OpencvCalibration.vcxproj.filters 2KB
qtvars.vcxproj.filters 493B
qtvars.vcxproj.filters 493B
core_c.h 126KB
msa_macros.h 81KB
types_c.h 70KB
kmeans_index.h 66KB
imgproc_c.h 50KB
dist.h 40KB
ui_CalibGUI.h 39KB
cvdef.h 36KB
constants_c.h 30KB
cv_cpu_helper.h 29KB
hierarchical_clustering_index.h 26KB
autotuned_index.h 21KB
kdtree_single_index.h 20KB
kdtree_index.h 20KB
lsh_table.h 18KB
types_c.h 18KB
lsh_index.h 15KB
result_set.h 15KB
index_testing.h 11KB
highgui_c.h 10KB
ui_MainForm.h 9KB
ui_MainForm.h 9KB
any.h 8KB
cv_cpu_dispatch.h 8KB
hdf5.h 7KB
共 818 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
多巴胺耐受
- 粉丝: 94
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功