Java调用opencv内存泄漏_内存泄漏使用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)是内存泄漏的原因。我必须使用这个功能,因为我正在使用16位灰度图像,Qt无法读取。我用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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。要使用Java OpenCV调用摄像头并动态识别人脸,您需要遵循以下步骤: 1. 安装OpenCV库 您需要在本地计算机上安装OpenCV库,以便能够在Java调用OpenCV。您可以从OpenCV官方网站下载并安装OpenCV库。 2. 导入OpenCV库 在Java项目中,您需要导入OpenCV库。您可以使用Maven和Gradle等构建工具来导入OpenCV库。 3. 调用摄像头 您可以使用Java OpenCV中的VideoCapture调用摄像头。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.videoio.VideoCapture; public class CameraCapture { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); if(!camera.isOpened()){ System.out.println("Error"); } else { Mat frame = new Mat(); while(true){ if (camera.read(frame)){ System.out.println("Frame Obtained"); System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height()); Core.flip(frame, frame, 1); break; } } } camera.release(); } } ``` 此代码段将获取从摄像头获取的帧并将其翻转。 4. 识别人脸 要识别人脸,您可以使用Java OpenCV中的CascadeClassifier类。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import org.opencv.videoio.VideoCapture; public class FaceDetection { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_alt.xml"); VideoCapture camera = new VideoCapture(0); Mat frame = new Mat(); while (true){ if (camera.read(frame)){ MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(frame, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); for (Rect rect : faceDetections.toArray()){ Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } HighGui.imshow("Face Detection", frame); HighGui.waitKey(1); } } } } ``` 此代码段将检测从摄像头获取的帧中的面部,并将其框定。您需要下载名为“haarcascade_frontalface_alt.xml”的人脸检测器文件并将其放在代码中的相应位置。 希望这可以帮助您开始使用Java OpenCV调用摄像头并动态识别人脸。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值