我有一个名为 BasePoint 的类
BasePoint
{
public:
BasePoint();
//BasePoint(const BasePoint& bp);
uint id;
float avgVal;
float varVal;
float avgLas;
float varLas;
cv::Mat invariants;// Type : 32FC1
int status;
};
并且有这个类的 2 个对象:
BasePoint previousBasePoint;
BasePoint currentBasePoint;
在我执行的每一次迭代中
const float norm_factor = 1.0;
currentBasePoint.invariants = totalInvariants / norm_factor; // Problem is here
currentBasePoint.id = image_counter;
if(previousBasePoint.id == 0)
{
previousBasePoint = currentBasePoint;
currentPlace->members.push_back(currentBasePoint);
wholebasepoints.push_back(currentBasePoint);
}
else
{
//some code here
}
代码的问题是,当我执行 totalInvariants/norm_factor;
而不是使用 totalInvariants
时,previousBasePoint
变得与 相同当前基点
。但是,如果我不划分它,一切正常。这可能是什么问题?
编辑:
const float norm_factor = 1.0;
currentBasePoint.invariants = totalInvariants;
currentBasePoint.invariants = currenBasePoint.invariants / norm_factor
也可以,但我还是想知道除法有什么问题
最佳答案
您正在 cv::Mat
上使用 =
opeartor,它使用浅拷贝操作。所以它们都会有相同的内存地址,你需要使用cv::Mat
的clone()
操作。
此外,您还需要将 BasePoint
的 = 运算符重载为 previousBasePoint = currentBasePoint;
上的默认运算符,这也会对内部 invariant
进行浅拷贝>.
关于c++ - 矩阵在任何运算符后变得相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45655974/