-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (155 loc) · 6.03 KB
/
main.cpp
File metadata and controls
194 lines (155 loc) · 6.03 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include <opencv2/video/background_segm.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
//init
Ptr<BackgroundSubtractor>bg_model=createBackgroundSubtractorMOG2().dynamicCast<BackgroundSubtractor>();
Mat fgMask, bgImg, fgImg;
//setup Video Capture device and link it to the first capture device
VideoCapture captureDevice;
captureDevice.open(1);
//setup image files used in the capture process
Mat captureFrame;
Mat grayscaleFrame;
Mat thresholdFrame;
//create a window to present the results
namedWindow("Kamera", 1);
//namedWindow("BG", 1);
//namedWindow("TH", 1);
//create a loop to capture and find faces
while(true)
{
//capture a new image frame
captureDevice>>captureFrame;
//resize frame
cv::Mat resized;
cv::resize(captureFrame, resized, cv::Size(800, 600));
//draw rectangle
cv::rectangle(
resized,
cv::Point(480, 20),
cv::Point(780, 320),
cv::Scalar(255, 255, 255)
);
//crop image
Mat ROI(resized, Rect(480,20,300,300));
Mat croppedImage;
// Copy the data into new matrix
ROI.copyTo(croppedImage);
//convert captured image to grayscale and equalize
cvtColor(croppedImage, grayscaleFrame, CV_BGR2GRAY);
equalizeHist(grayscaleFrame, grayscaleFrame);
GaussianBlur(grayscaleFrame,grayscaleFrame,Size(19,19),0.0,0);
threshold(grayscaleFrame, thresholdFrame,0,255,THRESH_BINARY_INV+THRESH_OTSU);
if(fgMask.empty()){
fgMask.create(croppedImage.size(), croppedImage.type());
}
bg_model->apply(croppedImage, fgMask, true ? -1:0);
GaussianBlur(fgMask, fgMask, Size(11,11), 3.5,3.5);
// threshold mask to saturate at black and white values
threshold(fgMask, fgMask, 10,255,THRESH_BINARY);
// create black foreground image
fgImg = Scalar::all(0);
// Copy source image to foreground image only in area with white mask
croppedImage.copyTo(fgImg, fgMask);
Mat coba, coba2;
erode(fgMask,coba, 0, Point(-1, -1), 2, 1, 1);
dilate(coba,coba2, 0, Point(-1, -1), 10, 1, 1);
//Get background image
bg_model->getBackgroundImage(bgImg);
// Show the results
imshow("coba", coba2);
Mat Temp, img;
coba2.convertTo(Temp, CV_8UC1);
bilateralFilter(Temp, img, 5, 20, 20);
vector<vector<Point> > contours;
vector<Vec4i> hier;
cv::findContours(Temp, contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
cv::Mat contourImage(Temp.size(), CV_8UC3, cv::Scalar(0,0,0));
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, Scalar(255, 255, 255), 2, 8, hier);
//Rect box = boundingRect(contours[idx]);
//rectangle(resized, box, Scalar(0,255,0));
}
//convex hull
double largest_area = 0.0;
int largest_contour_index = 0;
if (!contours.empty())
{
// Find largest contour
for (size_t i = 0; i < contours.size(); i++)
{
double a = contourArea(contours[i], false);
if (a > largest_area)
{
largest_area = a;
largest_contour_index = i;
}
}
// Draw largest contors
Mat3b contour(coba2.rows, coba2.cols, Vec3b(0, 0, 0));
drawContours(contour, contours, largest_contour_index, Scalar(255, 255, 255), 2, 8, hier);
// Find convex hull of largest contour
vector<Point>hulll;
convexHull(contours[largest_contour_index], hulll, CV_CLOCKWISE, true);
// Draw the convex hull
vector<vector<Point> > tmp;
tmp.push_back(hulll);
//drawContours(contour, tmp, 0, Scalar(0, 0, 255), 3);
//give point
//convex hulls
vector<vector<Point> >hull(contours.size());
vector<vector<int> > hullsI(contours.size());
vector<vector<Vec4i> > defects(contours.size());
for (int i = 0; i < contours.size(); i++)
{
convexHull(contours[i], hull[i], false);
convexHull(contours[i], hullsI[i], false);
if(hullsI[i].size() > 3 )
{
convexityDefects(contours[i], hullsI[i], defects[i]);
}
}
//REQUIRED contour is detected,then convex hell is found and also convexity defects are found and stored in defects
if (largest_area>100){
drawContours(contour, hull, largest_contour_index, Scalar(255, 0, 255), 3, 8, vector<Vec4i>(), 0, Point());
int count=0;
/// Draw convexityDefects
for(int j=0; j<defects[largest_contour_index].size(); ++j)
{
const Vec4i& v = defects[largest_contour_index][j];
float depth = v[3] / 256;
if (depth > 10 && depth < 100) // filter defects by depth
{
int startidx = v[0]; Point ptStart(contours[largest_contour_index][startidx]);
int endidx = v[1]; Point ptEnd(contours[largest_contour_index][endidx]);
int faridx = v[2]; Point ptFar(contours[largest_contour_index][faridx]);
line(contour, ptStart, ptEnd, Scalar(0, 255, 0), 1);
line(contour, ptStart, ptFar, Scalar(0, 255, 0), 1);
line(contour, ptEnd, ptFar, Scalar(0, 255, 0), 1);
circle(contour, ptFar, 4, Scalar(0, 0, 255), 2);
count++;
}
}
printf("ada %d jari \n", count);
}
imshow("Contour", contour);
}
//print output
imshow("Kamera", resized);
//ximshow("foreground mask", fgMask);
//imshow("TH", thresholdFrame);
//imshow("Contour", contourImage);
//pause for 33ms
waitKey(33);
}
return 0;
}