-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuicide_Detect_Trains.cpp
94 lines (81 loc) · 3.55 KB
/
Suicide_Detect_Trains.cpp
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
#include <opencv2/opencv.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main ()
{
Mat frame;//consistant of Track ROI
Mat back;//background of ROI
Mat fore;//foreground of ROI
Mat input;//takes video stream from camera
int flag = 0;
VideoCapture cap1("model.mp4");/*to capture from pre-designed video*/
//Background Subtraction Part_____________________________________________________________
BackgroundSubtractorMOG2 bg;
bg.set ("nmixtures", 10);
bool bShadowDetection = false;
vector < vector < Point > >contours;
double ti=getTickCount()/getTickFrequency();
namedWindow("Frame");
cvMoveWindow("Frame",0,0);//to relocate the "Frame" window to desired coordinates
namedWindow( "Output");
namedWindow("Proc");
cvMoveWindow("Output",730,70);
cvMoveWindow("Proc",520,70);
namedWindow("Input");
cvMoveWindow("Input",70,70);//from top left
for (;;)
{
double t1= getTickCount();
cap1 >> input;
Rect r(120,0,185,input.rows);//ROI for model.wmv
frame=input(r).clone();//change this region to get calibrated ROI
bg.operator()(frame, fore);
bg.getBackgroundImage (back);
erode (fore, fore, cv::Mat ());//to remove image noise
erode (fore, fore, cv::Mat ());//to remove image noise more, but reduces contour size
dilate (fore, fore, cv::Mat ());//
dilate (fore, fore, cv::Mat ());//
dilate (fore, fore, cv::Mat ());// sharpen contour without noise
findContours (fore, contours, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//CHAIN_APPROX_NONE ensures that straight lines of contour are also included
drawContours (frame, contours, -1, Scalar (255, 255, 255), 1);
imshow("Input",input);
if (cv::waitKey(15)>=10)
break;
//Rectangles Part________________________________________________
Mat drawing = Mat::zeros( fore.size(), CV_8UC3 );//to initialize drawing to a blank matrix
Scalar color = Scalar( 100, 100, 100);
int a=0;
vector<Rect> boundRect( contours.size() );
double t2;
for( int i = 0; i < contours.size(); i++ )
{
boundRect[i] = boundingRect( contours[i] );
//color coding part____________________________________________________________________________________
if(boundRect[i].width>=20 && boundRect[i].height>=40)//eliminates small boxes
{
if(boundRect[i].x+boundRect[i].width>drawing.cols/4 && boundRect[i].x<drawing.cols/1.333 && boundRect[i].height>120 && (getTickCount()/getTickFrequency()-ti)>2)//ensures a 2 second gap to eliminate noise and that blob is central
{
color=Scalar(0,0,255);//red
putText(drawing,"Suicide Detected",Point(5,30),FONT_HERSHEY_TRIPLEX,0.6,Scalar(0,255,255),1);
putText(frame,"Suicide Detected",Point(5,30),FONT_HERSHEY_TRIPLEX,0.6,Scalar(0,255,255),1);
t2= getTickCount();
int n= (t2-t1)/getTickFrequency();
}
drawContours( drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 1,0);//0 decides that the rectangle superimposes contour
rectangle( frame, boundRect[i].tl(), boundRect[i].br(), color, 1,0);//0 decides that the rectangle superimposes contour
}
}
imshow( "Output", drawing );
imshow("Proc",frame);
//char name[10];
//sprintf(name, "Frame%d.jpeg ", j+1);//to print a variable text on screen.
//imwrite(name,frame);
}
return 1;
}