-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (72 loc) · 2.67 KB
/
main.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
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect.hpp"
int main(int argc, char ** argv) {
std::string windowName = "DiceRecognizer";
cv::namedWindow(windowName, CV_WINDOW_AUTOSIZE);
cv::VideoCapture cap(1);
cv::Mat backgroundFrame;
cap >> backgroundFrame;
cvtColor(backgroundFrame, backgroundFrame, CV_BGR2GRAY);
cv::Mat originalFrame;
while (1) {
cap >> originalFrame;
std::ostringstream rectangleText;
cv::Mat processedFrame;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cvtColor(originalFrame,
processedFrame,
CV_BGR2GRAY);
cv::absdiff(processedFrame,
backgroundFrame,
processedFrame);
cv::threshold(processedFrame,
processedFrame,
140, 255,
cv::THRESH_OTSU || cv::THRESH_BINARY);
cv::Canny(processedFrame,
processedFrame,
2,
4,
3,
false);
cv::findContours(processedFrame,
contours,
hierarchy,
cv::RETR_EXTERNAL,
cv::CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
std::vector<cv::Point> cnt = contours[i];
double area = cv::contourArea(cnt);
if (area > 10000) {
cv::Rect boundRectangle = cv::boundingRect(cv::Mat(cnt));
rectangleText << "val: " << abs(boundRectangle.height * boundRectangle.width);
cv::putText(originalFrame, rectangleText.str(),
cv::Point(boundRectangle.x,
boundRectangle.y + boundRectangle.height + 10),
cv::FONT_HERSHEY_SIMPLEX,
0.9,
cv::Scalar(255, 255, 255),
1,
8);
cv::rectangle(originalFrame,
boundRectangle.tl(),
boundRectangle.br(),
cv::Scalar(255, 0, 0),
2,
8,
0);
}
}
cv::imshow(windowName, originalFrame);
if (!cv::waitKey(1)) {
std::cerr << "Program interrupted" << std::endl;
break;
}
}
cap.release();
cv::destroyAllWindows();
return 0;
}