262 lines
8.9 KiB
C
262 lines
8.9 KiB
C
|
|
#ifndef PARTICLESIZEPRESENTER_H
|
|||
|
|
#define PARTICLESIZEPRESENTER_H
|
|||
|
|
|
|||
|
|
#include <condition_variable>
|
|||
|
|
#include <thread>
|
|||
|
|
#include <map>
|
|||
|
|
#include <mutex>
|
|||
|
|
#include <memory>
|
|||
|
|
|
|||
|
|
#include "BasePresenter.h"
|
|||
|
|
#include "IVrConfig.h"
|
|||
|
|
#include "IVrEyeDevice.h"
|
|||
|
|
#include "ConfigMonitor.h"
|
|||
|
|
#include "TCPServerProtocol.h"
|
|||
|
|
#include "IYParticleSizeStatus.h"
|
|||
|
|
#include "SG_baseDataType.h"
|
|||
|
|
#include "VrConvert.h"
|
|||
|
|
#include "LaserDataLoader.h"
|
|||
|
|
#include "CommonDialogCameraLevel.h" // 引入通用对话框的接口
|
|||
|
|
#include <QImage>
|
|||
|
|
#include <QPainter>
|
|||
|
|
#include <QColor>
|
|||
|
|
#include <QObject>
|
|||
|
|
#include <QTimer>
|
|||
|
|
#include <memory>
|
|||
|
|
|
|||
|
|
// Forward declarations
|
|||
|
|
class DetectPresenter;
|
|||
|
|
|
|||
|
|
// 2D点结构体(替代cv::Point2f)
|
|||
|
|
struct Point2D
|
|||
|
|
{
|
|||
|
|
double x = 0.0;
|
|||
|
|
double y = 0.0;
|
|||
|
|
|
|||
|
|
Point2D() = default;
|
|||
|
|
Point2D(double _x, double _y) : x(_x), y(_y) {}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 3D点结构体(替代cv::Point3f)
|
|||
|
|
struct Point3D
|
|||
|
|
{
|
|||
|
|
double x = 0.0;
|
|||
|
|
double y = 0.0;
|
|||
|
|
double z = 0.0;
|
|||
|
|
|
|||
|
|
Point3D() = default;
|
|||
|
|
Point3D(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 工件检测结果结构
|
|||
|
|
struct ParticleSizeDetectionResult
|
|||
|
|
{
|
|||
|
|
int cameraIndex; // 相机索引
|
|||
|
|
double timestamp; // 检测时间戳
|
|||
|
|
bool success; // 检测是否成功
|
|||
|
|
std::vector<Point2D> corners; // 检测到的角点坐标(图像坐标)
|
|||
|
|
std::vector<Point3D> worldCorners; // 世界坐标系中的角点
|
|||
|
|
QImage detectionImage; // 检测结果图像
|
|||
|
|
QImage originalImage; // 原始图像
|
|||
|
|
std::string errorMessage; // 错误信息
|
|||
|
|
double confidence; // 置信度
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// Note: DetectionResult is defined in IYParticleSizeStatus.h and uses ParticleSizePosition
|
|||
|
|
|
|||
|
|
// 粒度检测算法参数结构
|
|||
|
|
struct ParticleSizeParams
|
|||
|
|
{
|
|||
|
|
double threshold1 = 50.0; // Canny边缘检测阈值1
|
|||
|
|
double threshold2 = 150.0; // Canny边缘检测阈值2
|
|||
|
|
double minLineLength = 50.0; // 最小线段长度
|
|||
|
|
double maxLineGap = 10.0; // 最大线段间隙
|
|||
|
|
double minCornerDistance = 20.0; // 最小角点距离
|
|||
|
|
double areaThreshold = 1000.0; // 面积阈值
|
|||
|
|
bool enablePerspectiveCorrection = true; // 是否启用透视校正
|
|||
|
|
double targetAspectRatio = 1.0; // 目标长宽比
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 当前执行参数结构
|
|||
|
|
struct CurrentExecutionParams
|
|||
|
|
{
|
|||
|
|
int cameraIndex = 1; // 相机序号
|
|||
|
|
ParticleSizeParams cornerParams; // 角点提取参数
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
class ParticleSizePresenter : public BasePresenter, public IConfigCommandHandler,
|
|||
|
|
public IVrConfigChangeNotify, public ICameraLevelCalculator,
|
|||
|
|
public ICameraLevelResultSaver
|
|||
|
|
{
|
|||
|
|
Q_OBJECT
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
explicit ParticleSizePresenter(QObject *parent = nullptr);
|
|||
|
|
~ParticleSizePresenter();
|
|||
|
|
|
|||
|
|
// 初始化
|
|||
|
|
int InitApp() override;
|
|||
|
|
|
|||
|
|
// 获取配置对象
|
|||
|
|
IVrConfig* GetConfig() { return m_vrConfig; }
|
|||
|
|
|
|||
|
|
// 获取配置结果对象
|
|||
|
|
ConfigResult* GetConfigResult() { return &m_configResult; }
|
|||
|
|
|
|||
|
|
// 手眼标定矩阵管理
|
|||
|
|
CalibMatrix GetClibMatrix(int index) const;
|
|||
|
|
|
|||
|
|
// 实现IVrConfigChangeNotify接口
|
|||
|
|
virtual void OnConfigChanged(const ConfigResult& configResult) override;
|
|||
|
|
|
|||
|
|
// ============ 实现 IConfigCommandHandler 接口 ============
|
|||
|
|
bool OnCameraExposeCommand(const CameraConfigParam& param) override;
|
|||
|
|
bool OnCameraGainCommand(const CameraConfigParam& param) override;
|
|||
|
|
bool OnCameraFrameRateCommand(const CameraConfigParam& param) override;
|
|||
|
|
bool OnCameraSwingCommand(const SwingConfigParam& param) override;
|
|||
|
|
bool OnAlgoParamCommand(const AlgoConfigParam& param) override;
|
|||
|
|
bool OnCalibParamCommand(const CalibConfigParam& param) override;
|
|||
|
|
bool OnFullConfigCommand(const FullConfigParam& param) override;
|
|||
|
|
|
|||
|
|
// 获取角点提取参数
|
|||
|
|
ParticleSizeParams GetParticleSizeParams() const { return m_currentExecutionParams.cornerParams; }
|
|||
|
|
|
|||
|
|
// 设置角点提取参数
|
|||
|
|
void SetParticleSizeParams(const ParticleSizeParams& params);
|
|||
|
|
|
|||
|
|
// 发送JSON格式检测结果给客户端
|
|||
|
|
void SendDetectionResultToClient(const ParticleSizeDetectionResult& result);
|
|||
|
|
|
|||
|
|
// ============ 实现 ICameraLevelCalculator 接口 ============
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 计算平面调平参数
|
|||
|
|
*/
|
|||
|
|
bool CalculatePlaneCalibration(
|
|||
|
|
const std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& scanData,
|
|||
|
|
double planeCalib[9],
|
|||
|
|
double& planeHeight,
|
|||
|
|
double invRMatrix[9]) override;
|
|||
|
|
|
|||
|
|
// ============ 实现 ICameraLevelResultSaver 接口 ============
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 保存相机调平结果到配置文件
|
|||
|
|
*/
|
|||
|
|
bool SaveLevelingResults(double planeCalib[9], double planeHeight, double invRMatrix[9],
|
|||
|
|
int cameraIndex, const QString& cameraName) override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 从配置文件加载相机调平结果
|
|||
|
|
*/
|
|||
|
|
bool LoadLevelingResults(int cameraIndex, const QString& cameraName,
|
|||
|
|
double planeCalib[9], double& planeHeight, double invRMatrix[9]) override;
|
|||
|
|
|
|||
|
|
protected:
|
|||
|
|
// ============ 实现 BasePresenter 纯虚函数 ============
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 初始化算法参数(实现纯虚函数)
|
|||
|
|
*/
|
|||
|
|
int InitAlgoParams() override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 执行算法检测(实现纯虚函数)
|
|||
|
|
* @param detectionDataCache 检测数据缓存的引用
|
|||
|
|
*/
|
|||
|
|
int ProcessAlgoDetection(std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& detectionDataCache) override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取检测数据类型(实现纯虚函数)
|
|||
|
|
* 粒度检测项目使用Position点云数据
|
|||
|
|
*/
|
|||
|
|
EVzResultDataType GetDetectionDataType() override {
|
|||
|
|
return keResultDataType_Position;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 相机状态变化通知(实现纯虚函数)
|
|||
|
|
*/
|
|||
|
|
void OnCameraStatusChanged(int cameraIndex, bool isConnected) override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 工作状态变化通知(重写虚函数)
|
|||
|
|
*
|
|||
|
|
* 当BasePresenter的工作状态改变时,此方法会被调用
|
|||
|
|
* 在此调用UI回调接口通知状态变化
|
|||
|
|
*/
|
|||
|
|
void OnWorkStatusChanged(WorkStatus status) override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 相机数量变化通知(重写虚函数)
|
|||
|
|
*
|
|||
|
|
* 当相机初始化时,此方法会被调用
|
|||
|
|
* 在此调用UI回调接口通知相机数量
|
|||
|
|
*/
|
|||
|
|
void OnCameraCountChanged(int count) override;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 状态文字更新通知(重写虚函数)
|
|||
|
|
*
|
|||
|
|
* 当需要更新状态文字时,此方法会被调用
|
|||
|
|
* 在此调用UI回调接口通知状态消息
|
|||
|
|
*/
|
|||
|
|
void OnStatusUpdate(const std::string& statusMessage) override;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
|
|||
|
|
// 连接状态检查和更新
|
|||
|
|
void CheckAndUpdateWorkStatus();
|
|||
|
|
|
|||
|
|
// 根据相机索引获取调平参数
|
|||
|
|
SSG_planeCalibPara _GetCameraCalibParam(int cameraIndex);
|
|||
|
|
|
|||
|
|
// 更新当前执行参数
|
|||
|
|
void _UpdateCurrentExecutionParams();
|
|||
|
|
|
|||
|
|
// 粒度检测算法实现
|
|||
|
|
bool _DetectParticleSize(const QImage& inputImage, std::vector<Point2D>& corners,
|
|||
|
|
std::vector<Point3D>& worldCorners, QImage& resultImage,
|
|||
|
|
std::string& errorMessage);
|
|||
|
|
|
|||
|
|
// 透视校正
|
|||
|
|
bool _PerspectiveCorrection(const std::vector<Point2D>& srcCorners,
|
|||
|
|
const std::vector<Point2D>& dstCorners,
|
|||
|
|
QImage& correctedImage);
|
|||
|
|
|
|||
|
|
// TCP服务器相关方法
|
|||
|
|
int InitTCPServer(); // 初始化TCP服务器协议(使用配置端口)
|
|||
|
|
void OnTCPConnectionChanged(bool connected); // TCP连接状态改变回调
|
|||
|
|
bool OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp); // TCP检测触发回调
|
|||
|
|
void _SendDetectionResultToTCP(const DetectionResult& detectionResult, int cameraIndex);
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
// ParticleSizePresenter 特有的成员变量
|
|||
|
|
IVrConfig* m_vrConfig = nullptr;
|
|||
|
|
ConfigResult m_configResult; // 配置结果
|
|||
|
|
|
|||
|
|
// TCP服务器相关
|
|||
|
|
TCPServerProtocol* m_pTCPServer = nullptr; // TCP服务器协议实例
|
|||
|
|
bool m_bTCPConnected = false; // TCP客户端连接状态
|
|||
|
|
|
|||
|
|
// 配置监控器
|
|||
|
|
ConfigMonitor m_configMonitor;
|
|||
|
|
|
|||
|
|
// 当前执行参数
|
|||
|
|
CurrentExecutionParams m_currentExecutionParams; // 当前执行参数
|
|||
|
|
|
|||
|
|
// 检测处理器
|
|||
|
|
DetectPresenter* m_pDetectPresenter = nullptr;
|
|||
|
|
|
|||
|
|
// 手眼标定矩阵列表
|
|||
|
|
std::vector<CalibMatrix> m_clibMatrixList;
|
|||
|
|
|
|||
|
|
// 调试参数
|
|||
|
|
VrDebugParam m_debugParam;
|
|||
|
|
|
|||
|
|
// 算法参数
|
|||
|
|
VrAlgorithmParams m_algorithmParams;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif // PARTICLESIZEPRESENTER_H
|