c++ - 矩阵在任何运算符后变得相同

标签 c++ opencv

我有一个名为 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::Matclone()操作。

此外,您还需要将 BasePoint 的 = 运算符重载为 previousBasePoint = currentBasePoint; 上的默认运算符,这也会对内部 invariant 进行浅拷贝>.

关于c++ - 矩阵在任何运算符后变得相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45655974/

相关文章:

c++ - 没有类(class)类型

c++ - opencv 中轨迹栏名称的大小/长度

opencv - 定向梯度直方图中的最终向量

c++ - createprocess 没有那个文件或目录

c++ - 有纯右值的 std::forward 用例吗?

c++ - Ncurses - 如何将某些内容添加到输入缓冲区中

c++ - 使用不包含在另一个 vector 中的键从 map 中删除元素

python - 让 haar 级联工作

python - OpenCV 错误:参数 '%s' 需要 Ptr<cv::UMat>

android - Features2D + Homography OpenCV 链接错误