![](/img/trans.png)
[英]Does using the detectMultiScale of the CascadeClassifier in opencv leak memory?
[英]Memory leak using opencv : VideoCapture
我使用Qt Creator 2.4.1(Qt 4.8.4)和OpenCV 2.4.2开发了一个应用程序,该应用程序从文件夹中读取图像并显示它们。
它使用cv :: VideoCapture和QGraphicsScene / QGraphicsView。 它运行良好,但是我遇到了内存泄漏:如果我在任务管理器中查看消耗的内存,则每次读取新映像时内存都会增加,并最终导致崩溃。
我的主窗口是使用Qt Designer创建的,它是一个继承QMainWindow的类。 上面有一个QGraphicsView view_src
和一个按钮: buttonStart
这是代码示例:类声明:
using namespace std;
using namespace cv;
namespace Ui {
class FenetrePrinc;
}
class FenetrePrinc : public QMainWindow {
Q_OBJECT
public:
explicit FenetrePrinc(QWidget *parent = 0);
~FenetrePrinc();
public slots:
virtual void start();
virtual void tick();
virtual void stop_timer();
private:
Ui::FenetrePrinc *ui;
QString filename;
QGraphicsScene *scene_src;
QGraphicsItem *img_src;
VideoCapture sequence;
Mat src;
};
类定义:
FenetrePrinc::FenetrePrinc(QWidget *parent) : QMainWindow(parent), ui(new Ui::FenetrePrinc){
ui->setupUi(this);
scene_src = new QGraphicsScene();
timer = new QTimer(this);
img_src = scene_src->addPixmap(QPixmap("vide.jpg"));
ui->view_src->setScene(scene_src);
connect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(start()));
}
FenetrePrinc::~FenetrePrinc(){
delete scene_src;
delete img_src;
delete ui;
}
void FenetrePrinc::start(){
if(src.empty())
sequence.open(filename.toStdString());
connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
timer->start(1000/24); //24 frames per second
disconnect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(start()));
connect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(stop_timer()));
}
void FenetrePrinc::tick(){
sequence >> src;
if(src.empty())
{
sequence.release();
stop_timer();
return;
}
scene_src->removeItem(img_src);
img_src = scene_src->addPixmap(convert16uc1(src));
src.release();
}
void FenetrePrinc::stop_timer(){
timer->stop();
disconnect(timer, SIGNAL(timeout()), this, SLOT(tick()));
disconnect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(stop_timer()));
connect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(start()));
}
我不明白为什么每次读取图像时内存使用量会不断增加,为什么每次读取时都会释放图像,并且一旦完成则释放顺序。 但是也许我错过了什么?
编辑:函数QPixmap convert16uc1(Mat img)
是内存泄漏的原因。 我必须使用此功能,因为我正在处理Qt无法读取的16位灰度图像。 我打开图像并使用OpenCV执行图像处理,然后使用Qt显示图像。
该函数的代码如下:
QPixmap FenetrePrinc::convert16uc1(const cv::Mat& source)
{
quint16* pSource = (quint16*) source.data;
int pixelCounts = source.cols * source.rows;
QImage dest(source.cols, source.rows, QImage::Format_RGB32);
char* pDest = (char*) dest.bits();
for (int i = 0; i < pixelCounts; i++)
{
quint8 value = (quint8) ((*(pSource)) >> 8);
*(pDest++) = value; // B
*(pDest++) = value; // G
*(pDest++) = value; // R
*(pDest++) = 0; // Alpha
pSource++;
}
return QPixmap::fromImage(dest);
}
最有可能是convert16uc1
。
如果您不能在此处发布convert16uc1,请尝试使用imwrite将图像临时保存在opencv中并将图像加载到Qt中。 如果memleak消失。 分析convert16uc1
。
或者不调用convert16uc1(src),而是使用先前在Qt中加载的其他常量图像来调用addPixmap
。
通过阅读此线程 ,我找到了导致该问题的原因以及如何解决该问题。
从Qt文档中:
无效的QGraphicsScene :: removeItem(QGraphicsItem * item)
从场景中删除该项目及其所有子项。 项目的所有权会传递给调用方(即QGraphicsScene在销毁时将不再删除项目)。
另请参见addItem()。
一旦QGraphicsScene :: removeItem(QGraphicsItem * item)`被调用,QGraphicsScene将不再被销毁。
修复:在函数FenetrePrinc::tick()
removeItem(img_src)
之后调用delete img_src
:
void FenetrePrinc::tick(){
sequence >> src;
if(src.empty())
{
sequence.release();
stop_timer();
return;
}
scene_src->removeItem(img_src);
delete img_src;
img_src = scene_src->addPixmap(convert16uc1(src));
src.release();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.