c++ - 提取特征。一个类中有很多 getter/setter 是否正常?

标签 c++ image-processing opencv getter-setter

我有一堆不同的 Feature 类来计算图像特征。
我必须从这些类中提取“关键特征”,这些特征将被打包在一起作为搜索键。
我还将存储部分 Feature 类。我无法存储整个要素类,因为那样效率非常低。

现在,我想到的是编写一个 Stored_features 类,将“关键功能”组合在一起。
我的布局是:

   Facial_features
   |      |       |
   |      V       |
   | .---Feature1 V   <|-- Abstract_feature
   | | .---Feature2   <|---'
   V V V
  Stored_features

我的问题是,这样的Stored_features类会有很多getter和setter,据我所知,getters和setter表明设计不好。是否有一种易于维护的方法来避免这里出现过多的 getter 和 setter?

重点是我看到我的代码与此布局紧密结合:(

编辑:

我的代码按要求摘录。

#include <opencv2/core/core.hpp>

class Abstract_feature{
public:
  virtual void calculate()=0;
  virtual void draw(cv::Mat& canvas)=0;
  /// to put values into Stored_features
  virtual void registrate_key_values(Stored_features&) const=0;
};

class Facial_features : Abstract_feature{
public:
  virtual void calculate()
  { 
    es.calculate; sc.calculate; 
    /*etc but iterating over a list of Abstract_feature's*/
  }
  Stored_features get_stored_features() const
  {
    return sf.clone();
  }
private:
  Stored_features sf;
  Head_size es;
  Skin_color sc;
};

class Head_size : public Abstract_feature{
  //you can guess the impl.
};    

class Stored_features{
public:
  typedef enum{SKIN_COLOR=0, HEAD_SIZE_WIDTH,HEAD_SIZE_HEIGHT} Name;
public:
  void set_key_feature(Name, double value);
  cv::Mat get_feature_vector() const {return key_values;}
private:
  cv::Mat key_values;
  // and here would come other features eg.
  cv::Rect head_roi; // I don't search based on these. (So I should not use getters/setters?)
};

添加了opencv,因为无论如何它都是一个基于opencv的项目。

最佳答案

许多 getter 和 setter 本身并不表示设计不好。然而,它们可以暗示你的类(class)可以分解为更小的类(class)。以“客户”为例,如果您将所有名称、地址、送货地址字段放在一个类中,那么这将是一个糟糕的设计,您应该使用“地址”类对其进行拆分。

我不确定你的设计是关于什么的,重点是你是否需要 100 个 setter/getter ,因为你有 100 个不相关的字段使用 100 个 setter/getter

关于c++ - 提取特征。一个类中有很多 getter/setter 是否正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349633/

相关文章:

c++ - 需要指针的私有(private)类变量?

c++ - LLVM Clang 在 OS X 上产生极慢的输入/输出

android - 如何在没有 API 的情况下在 Android 中执行图像处理?

c++ - 无法在 RHEL 7 上使用 CUDA 构建 OpenCV

opencv - 将 Libraw 格式转换为 cv::Mat

c++ - 与 Python 相比,在 Pytorch C++ API 中无法获得相似的 DL 预测结果

c++ - 如何在包含指向元素的指针的集合中找到元素?

java - 如何将图像分解为子图像?

c++ - YUV 图像中的像素

c++ - 在 OpenCv 中计算 cv::Mat 的外(张量)积