-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_detector.cpp
More file actions
executable file
·131 lines (104 loc) · 3.91 KB
/
Copy pathobject_detector.cpp
File metadata and controls
executable file
·131 lines (104 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/bgsegm.hpp>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib") //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif
using namespace cv;
using namespace std;
using namespace bgsegm;
int main(int argc, char* argv[])
{
/* Check for the input parameter correctness. */
if (argc != 2) {
cerr <<"Incorrect input : Video Filename required" << endl;
cerr <<"exiting..." << endl;
return 1;
}
//global variables
Mat frame; //current frame
Mat resize_blur_Img;
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Mat binaryImg, morphPartial;
//Mat TestImg;
Mat ContourImg; //fg mask fg mask generated by MOG2 method
Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
pMOG2 = createBackgroundSubtractorMOG2(300,32,true);//(300,32,true);//300,0.0);
//char fileName[100] = "datasample1.mov"; //video\\mm2.avi"; //mm2.avi"; //cctv 2.mov"; //mm2.avi"; //";//_p1.avi";
VideoCapture stream1(argv[1]); //0 is the id of video device.0 if you have only one camera
//morphology element
Mat element = getStructuringElement(MORPH_RECT, Size(7, 7), Point(3,3) );
//unconditional loop
while (true) {
Mat cameraFrame;
if(!(stream1.read(frame))) //get one frame form video
break;
//Resize
resize(frame, resize_blur_Img, Size(frame.size().width/3, frame.size().height/3) );
//Blur
blur(resize_blur_Img, resize_blur_Img, Size(2, 2) );
//Background subtraction
pMOG2->apply(resize_blur_Img, fgMaskMOG2, -0.5);//,-0.5);
///////////////////////////////////////////////////////////////////
//pre procesing
//1 point delete
//morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_ERODE, element);
morphologyEx(fgMaskMOG2, morphPartial, CV_MOP_CLOSE, element);
//morphologyEx(fgMaskMOG2, testImg, CV_MOP_OPEN, element);
//Shadow delete
//Binary
threshold(morphPartial, binaryImg, 128, 255, CV_THRESH_BINARY);
//Find contour
ContourImg = binaryImg.clone();
//less blob delete
vector< vector< Point> > contours;
findContours(ContourImg,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours
vector< Rect > output;
vector< vector< Point> >::iterator itc= contours.begin();
while (itc!=contours.end()) {
//Create bounding rect of object
//rect draw on origin image
Rect mr= boundingRect(Mat(*itc));
if(mr.area() > 15000)
rectangle(resize_blur_Img, mr, CV_RGB(255,0,0));
++itc;
}
///////////////////////////////////////////////////////////////////
//Display
//namedWindow("cv_Shadow_Removed", WINDOW_NORMAL);
namedWindow("cv_Blur_Resize", WINDOW_OPENGL);
//namedWindow("cv_MOG2", WINDOW_NORMAL);
// imshow("cv_Shadow_Removed", binaryImg);
imshow("cv_Blur_Resize", resize_blur_Img);
//imshow("cv_MOG2", fgMaskMOG2);
if (waitKey(2) == 'q' )
break;
}
}