algorithmbase.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef ALGORITHMBASE_H
  2. #define ALGORITHMBASE_H
  3. #include <opencv2/opencv.hpp>
  4. #include <QString>
  5. #include <QVariant>
  6. #include <QMap>
  7. #include <QList>
  8. #include <typeindex>
  9. // 1. Parameter type enum (for OpenCV algorithm common parameter types)
  10. enum class ParamType {
  11. Int, // Integer (e.g., kernel size, threshold)
  12. Float, // Float (e.g., sigma, dp)
  13. Bool, // Boolean (e.g., L2gradient)
  14. Enum // Enum (e.g., threshold type THRESH_BINARY)
  15. };
  16. // 2. Single parameter metadata
  17. struct ParamMeta {
  18. QString name; // Parameter name (e.g., threshold1)
  19. QString displayName; // Display name (e.g., "Threshold 1")
  20. ParamType type; // Parameter type
  21. double minVal; // Minimum value (int/float)
  22. double maxVal; // Maximum value (int/float)
  23. double defaultValue; // Default value
  24. QMap<int, QString> enumItems; // Enum items (key=enum value, value=display name)
  25. QVariant value; // Current value
  26. // Constructor (simplify initialization)
  27. ParamMeta(QString n, QString dn, ParamType t, double min, double max, double def)
  28. : name(n), displayName(dn), type(t), minVal(min), maxVal(max), defaultValue(def), value(def) {}
  29. };
  30. // 3. Algorithm metadata (includes algorithm type + all parameter metadata)
  31. struct AlgorithmMeta {
  32. QString type; // Algorithm type (e.g., canny/threshold)
  33. QString name; // Algorithm name (e.g., "Canny Edge Detection")
  34. QList<ParamMeta> params; // Parameter list
  35. // Add parameter method
  36. void addParam(const QString& displayName, const QString& type, double value, double min, double max) {
  37. ParamType paramType = ParamType::Int;
  38. if (type == "float") {
  39. paramType = ParamType::Float;
  40. } else if (type == "bool") {
  41. paramType = ParamType::Bool;
  42. }
  43. params << ParamMeta(displayName, displayName, paramType, min, max, value);
  44. }
  45. };
  46. struct RuntimeParamsBase {
  47. virtual ~RuntimeParamsBase() = default;
  48. std::type_index type() const {
  49. return std::type_index(typeid(*this));
  50. }
  51. };
  52. class AlgorithmBase {
  53. public:
  54. virtual ~AlgorithmBase() = default;
  55. virtual void process(cv::Mat image) = 0;
  56. virtual AlgorithmMeta getParams() = 0;
  57. virtual void setParams(const AlgorithmMeta& params) = 0;
  58. // Get runtime parameters (returns nullptr if not implemented)
  59. virtual RuntimeParamsBase* getRuntimeParams() {
  60. return nullptr;
  61. }
  62. std::unique_ptr<RuntimeParamsBase> m_params;
  63. };
  64. #endif // ALGORITHMBASE_H