// 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;
}