-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.cpp
108 lines (88 loc) · 3.52 KB
/
sample.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Program to display a video from attached default camera device and detect colored blobs using H and S thresholding
// Remove noise using opening and closing morphological operations
// Author: Samarth Manoj Brahmbhatt, University of Pennsylvania
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int hs_slider = 0, low_slider = 30, high_slider = 100;
int low_h = 30, low_s = 30, high_h = 100, high_s = 100;
void on_hs_trackbar(int, void *) {
switch(hs_slider) {
case 0:
cv::setTrackbarPos("Low threshold", "Segmentation", low_h);
cv::setTrackbarPos("High threshold", "Segmentation", high_h);
break;
case 1:
cv::setTrackbarPos("Low threshold", "Segmentation", low_s);
cv::setTrackbarPos("High threshold", "Segmentation", high_s);
break;
}
}
void on_low_thresh_trackbar(int, void *) {
switch(hs_slider) {
case 0:
low_h = min(high_slider - 1, low_slider);
cv::setTrackbarPos("Low threshold", "Segmentation", low_h);
break;
case 1:
low_s = min(high_slider - 1, low_slider);
cv::setTrackbarPos("Low threshold", "Segmentation", low_s);
break;
}
}
void on_high_thresh_trackbar(int, void *) {
switch(hs_slider) {
case 0:
high_h = max(low_slider + 1, high_slider);
cv::setTrackbarPos("High threshold", "Segmentation", high_h);
break;
case 1:
high_s = max(low_slider + 1, high_slider);
cv::setTrackbarPos("High threshold", "Segmentation", high_s);
break;
}
}
int main()
{
// Create a VideoCapture object to read from video file
// 0 is the ID of the built-in laptop camera, change if you want to use other camera
VideoCapture cap(1);
//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Capture could not be opened succesfully" << endl;
return -1;
}
cv::namedWindow("Video");
cv::namedWindow("Segmentation");
cv::createTrackbar("0. H\n1. S", "Segmentation", &hs_slider, 1, on_hs_trackbar);
cv::createTrackbar("Low threshold", "Segmentation", &low_slider, 255, on_low_thresh_trackbar);
cv::createTrackbar("High threshold", "Segmentation", &high_slider, 255, on_high_thresh_trackbar);
while(char(waitKey(1)) != 'q' && cap.isOpened())
{
cv::Mat frame, frame_thresholded, frame_hsv;
cap >> frame;
cv::cvtColor(frame, frame_hsv, CV_BGR2HSV);
// Check if the video is over
if(frame.empty())
{
cout << "Video over" << endl;
break;
}
// extract the Hue and Saturation channels
int from_to[] = {0,0, 1,1};
Mat hs(frame.size(), CV_8UC2);
cv::mixChannels(&frame_hsv, 1, &hs, 1, from_to, 2);
// check the image for a specific range of H and S
cv::inRange(hs, Scalar(low_h, low_s), Scalar(high_h, high_s), frame_thresholded);
// open and close to remove noise
cv::Mat str_el = getStructuringElement(MORPH_ELLIPSE, Size(7, 7));
cv::morphologyEx(frame_thresholded, frame_thresholded, MORPH_OPEN, str_el);
cv::morphologyEx(frame_thresholded, frame_thresholded, MORPH_CLOSE, str_el);
cv::imshow("Video", frame);
cv::imshow("Segmentation", frame_thresholded);
}
return 0;
}