我试图在opencv和c ++中实现以下链接http://in.mathworks.com/help/vision/examples/motion-based-multiple-object-tracking.html。 我创建了一个说ex:
的课程class assign
{
vector <int> id;
vector <int> area;
vector<Point> centroid;
};
在此之后我创建了一个对象
assign id;
现在我想分配质心值和其他值。我试过的是
id.centroid (p);
其中p是“点”但是我得到了错误。我不知道我哪里出错了。
答案 0 :(得分:3)
centroid
是班级private
的{{1}}成员。如果您想直接访问它,您应该公开
assign
如果您想在class assign
{
public:
vector<Point> centroid;
//...
};
中添加Point
,则应该
centroid
答案 1 :(得分:1)
主要答案已由songyuanyao给出。我想要添加的是一个可能的解决方案,它允许您使用已经尝试过的成员变量。
如果您想使用centroid
获取并设置成员id.centroid(p)
,您可以使用以下类声明:
class Assign
{
public:
vector<Point> centroid();
void centroid(vector<Point> c);
private:
vector<Point> m_centroid;
};
该定义可能如下所示:
// getter
vector<Point> Assign::centroid() {
return m_centroid;
}
// setter
void Assign::centroid(vector<Point> c) {
m_centroid = c;
}
现在,如果使用id.centroid(p)
设置成员,将调用重载的setter并设置变量。如果您调用p = id.centroid()
(空参数列表),将调用重载的getter并返回当前的m_centroid
。
答案 2 :(得分:1)
添加到之前的答案;如果你想扩展你的课程,可以在构建你的对象时为你完成。
class Assign {
private:
std::vector<int> m_vIds;
std::vector<int> m_vAreas;
std::vector<Vec2> m_vCentroids;
public:
Assign(); // Default Constructor Same As What You Have But Not Declared.
Assign( int* pIds, int* pAreas, int* pCentroids ); // Create By Using Pointers
// Create By Passing In Either Pre Filled Vectors Or Even An Empty
// Vectors To Be Filled Out Later. Passes By Reference. This Will
// Also Set The Variables That Are Passed In From The Caller.
Assign( std::vector<int>& vIds, std::vector<int>& vAreas, std::vector<Vec2>& vCentroids );
// Since You Are Using Vectors Within This Class It Is Also Good To
// Have A Destructor To Clear These Out Once The Object Is Done And
// Ready To Be Destroyed Or Removed From Memory
~Assign();
};
// The Destructor Would Look Like This
Assign::~Asign() {
if ( !m_vIds.empty() ) {
m_vIds.clear();
}
if ( !m_vAreas.empty() ) {
m_vAreas.clear();
}
if ( !m_vCentroids.empty() ) {
m_vCentroids.empty();
}
} // ~Assign
// NOTE: I used Vec2 instead of point due to my use of programming
// 2D & 3D Graphics Rendering Engines; Most Graphics APIs and Libraries
// along with Most Math Libraries Will Not Have A Point Class; Most Will
// Use Vec2 or Vec3 - Vector2 or Vector3 & Vector4 Since in terms of
// memory they are exactly the same thing. It is up to you to know which
// objects are points or locations, and which are vectors as in forces,
// velocities, accelerations, directions, normals etc. The only major
// difference between a discrete Point Class or Structure versus a Vector
// Class is that the Vector Class usually has operations defined with it
// to do vector mathematics such as addition, subtraction, multiplication by
// value, multiplication by vector, division by value, division by vector,
// cross & dot product, comparisons, testing if vector is 0, setting it to
// be a normal vector, returning the magnitude or length and a few others.
// The general point class or object is usually just data values or
// simply coordinates without operations.