opencv 释放内存


// opencv_test.cpp : 定义控制台应用程序的入口点。

//

 

#include "stdafx.h"

#include<iostream>

#include "opencv2/imgcodecs.hpp"

#include "opencv2/imgproc.hpp"

#include "opencv2/videoio.hpp"

#include <opencv2/highgui.hpp>

#include <opencv2/video.hpp>

#include "opencv2/video/background_segm.hpp"

#include <thread>

typedef struct _Camera_Data

{

std::string name;

cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2;

}Camera_Data, *LP_Camera_Data;

class LP_pData

{

public:

/*MainWindow * mainwindow;

QString m_sessionId;*/

cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2;

double areaMinSmall, areaMinBig, kSmallMin2Max, kBigMin2Max;

cv::Rect rect1, rect2;

int frame_count, frame_countReal;

double stdDeSum, stdDeMean;

std::string cameraName;

};

std::vector<cv::Mat> findConnectedComponents(std::string m_picPath, void* args) {

std::vector<cv::Mat> rects;

LP_pData *tmp_data = (LP_pData *)args;

cv::Mat frame, mask, mean, convar;

cv::Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method

frame = cv::imread(m_picPath.c_str());

if (frame.empty()) {

//qDebug("May reach the end of image list,or not read properly.");

return rects;

}

tmp_data->frame_count++;

std::cout << 2 << std::endl;

Laplacian(frame, mask, CV_32F);

meanStdDev(mask, mean, convar);

tmp_data->stdDeSum += *convar.ptr<double>(0);

tmp_data->stdDeMean = tmp_data->stdDeSum / tmp_data->frame_count;

 

if (*convar.ptr<double>(0) <= tmp_data->stdDeMean && tmp_data->frame_count >= 10) {

return rects;

}

tmp_data->frame_countReal++;

std::cout << 11111111111111 << std::endl;

tmp_data->pMOG2->apply(frame, fgMaskMOG2, -1);

std::cout << 2222 << std::endl;

//if (tmp_data->frame_count <= 10)

//return rects;

//imshow("Frame", frame);

//cv::waitKey(200);

cv::Rect bounding_box;

cv::Point cPoint;

int box_width, box_height;

float ratio_l2w = 0;

//clean up raw mask

cv::morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_OPEN, cv::Mat(), cv::Point(-1, -1), 1);

cv::morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_CLOSE, cv::Mat(), cv::Point(-1, -1), 1);

// FIND CONTOURS AROUND ONLY BIGGER REGIONS

std::vector<std::vector<cv::Point> > contours_all; // all contours found

cv::findContours(fgMaskMOG2, contours_all, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

double areaLimitLow, areaLimitHigh;

std::cout <<5 << std::endl;

for (std::vector<std::vector<cv::Point>>::iterator c = contours_all.begin(); c != contours_all.end(); ++c) {

bounding_box = boundingRect(*c);

box_width = bounding_box.width;

box_height = bounding_box.height;

auto area_of_contour = bounding_box.width * bounding_box.height;

cPoint.x = bounding_box.x + bounding_box.width / 2;

cPoint.y = bounding_box.y + bounding_box.height / 2;

areaLimitLow = cPoint.y * tmp_data->kSmallMin2Max + tmp_data->areaMinSmall;

areaLimitHigh = cPoint.y * tmp_data->kBigMin2Max + tmp_data->areaMinBig;

if (box_width <= box_height) {

ratio_l2w = box_height / box_width;

}

else {

ratio_l2w = box_width / box_height;

}

if ((areaLimitLow > area_of_contour) || (area_of_contour > areaLimitHigh) || (ratio_l2w > 8) || tmp_data->rect1.contains(cPoint) || tmp_data->rect2.contains(cPoint)) {

continue;

}

cv::Mat newROI;

frame(bounding_box).copyTo(newROI);

rects.push_back(newROI);

std::cout << 8<< std::endl;

}

frame.release();

mask.release();

mean.release();

convar.release();

std::cout << 10 << std::endl;

return rects;

}

void aaa(int i) {

LP_pData* tmp_data = new LP_pData();

 

cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG;

pMOG = cv::createBackgroundSubtractorMOG2(100, 25, false); //MOG2 approach

pMOG->setNMixtures(8);

pMOG->setBackgroundRatio(0.9f);

tmp_data->pMOG2 = pMOG;

 

std::string aa = "D:\\11.jpeg";

for (int i = 0; i < 10; i++) {

findConnectedComponents(aa, (void *)tmp_data);

}

tmp_data->pMOG2.release();

std::cout << "end********************" << i<< std::endl;

delete tmp_data;

}

int main()

{

for (int i = 0; i < 50; i++) {

std::thread task01(aaa,i);

task01.detach();

}

 

getchar();

    return 0;

}

 

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值