踩坑,C++ Opencv中Mat对象作消息队列参数时候出现队列中数据完全一样的情况

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QFileDialog>
#include <stdio.h>
#include <string>
#include <queue>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <qaction.h>

void test()
{
    queue<Mat> videoframes;
    Mat frame;
    VideoCapture capture;

    capture.open("./data/output.mp4");


    if (!capture.isOpened())
    {
        return;
    }
    videoframes = queue<Mat>();
    while (capture.read(frame))
    {
        qDebug("add one frame");
        videoframes.push(frame.clone());

		videoframes.push(frame.clone()); //方法1
        //videoframes.push(move(frame)); //方法2

    }
    capture.release();
    qDebug("getFrameOk");

    while (!videoframes.empty())
    {

        Mat frame = videoframes.front();
        videoframes.pop();

        namedWindow("Display");
        //显示图像
        imshow("Display", frame);

        //暂停,等待按键结束
        waitKey(10);

        qDebug("Detect frame tid:%x", this_thread::get_id());

    }
}

问题:

这串代码会显示完全一样的图片

原因:

opencv的Mat对象的拷贝构造和赋值都是浅拷贝,也就是说队列中的对象最终都指向最后输入的图像

解决方法:

使用clone()方法,进行深拷贝
调用move函数也可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值