-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
55 lines (45 loc) · 1.47 KB
/
process.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
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture stream(0);
vector<vector<Point> > contours;
int GREENHUE_LOWERB = 29; //yellow
int GREENHUE_UPPERB = 64; //green
namedWindow("output");
namedWindow("control");
createTrackbar("Hue Lower", "control", &GREENHUE_LOWERB, 255);
createTrackbar("Hue Upper", "control", &GREENHUE_UPPERB, 255);
while (true) {
Mat cameraFrame;
Mat processedFrame;
Mat outputFrame;
stream.read(cameraFrame);
cvtColor(cameraFrame, processedFrame, COLOR_BGR2HSV);
inRange(processedFrame,
Scalar(GREENHUE_LOWERB, 0, 0), Scalar(GREENHUE_UPPERB, 255, 255),
processedFrame);
findContours(processedFrame, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int maxContourIdx = -1;
int maxContourArea = -1;
for (int i=0; i < contours.size(); ++i) {
Rect boundRect = boundingRect(contours[i]);
if (boundRect.area() > maxContourArea) {
maxContourIdx = i;
maxContourArea = boundRect.area();
}
}
if (maxContourIdx > 0) {
Rect boundRect = boundingRect(contours[maxContourIdx]);
outputFrame = cameraFrame;
rectangle(outputFrame, boundRect.tl(), boundRect.br(), Scalar(GREENHUE_LOWERB, 255, 255));
}
imshow("output", outputFrame);
if (waitKey(30) >= 0)
break;
}
return 0;
}