Skip to content

Commit 1ae02c0

Browse files
committed
Minor refactoring in several C++ samples:
- bgfg_segm - peopledetect - opencv_version - dnn/colorization - tapi/opencl_custom_kernel - tapi/dense_optical_flow (renamed tvl1_optical_flow)
1 parent dc1d9ae commit 1ae02c0

File tree

7 files changed

+301
-423
lines changed

7 files changed

+301
-423
lines changed

samples/cpp/bgfg_segm.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
15
#include "opencv2/core.hpp"
26
#include "opencv2/imgproc.hpp"
37
#include "opencv2/video.hpp"
@@ -10,18 +14,18 @@ using namespace cv;
1014

1115
int main(int argc, const char** argv)
1216
{
13-
const String keys = "{c camera||use video stream from camera (default is NO)}"
14-
"{fn file_name|../data/tree.avi|video file}"
15-
"{m method|mog2|method: background subtraction algorithm ('knn', 'mog2')}"
16-
"{h help||show help message}";
17+
const String keys = "{c camera | 0 | use video stream from camera (device index starting from 0) }"
18+
"{fn file_name | | use video file as input }"
19+
"{m method | mog2 | method: background subtraction algorithm ('knn', 'mog2')}"
20+
"{h help | | show help message}";
1721
CommandLineParser parser(argc, argv, keys);
1822
parser.about("This sample demonstrates background segmentation.");
1923
if (parser.has("help"))
2024
{
2125
parser.printMessage();
2226
return 0;
2327
}
24-
bool useCamera = parser.has("camera");
28+
int camera = parser.get<int>("camera");
2529
String file = parser.get<String>("file_name");
2630
String method = parser.get<String>("method");
2731
if (!parser.check())
@@ -31,13 +35,13 @@ int main(int argc, const char** argv)
3135
}
3236

3337
VideoCapture cap;
34-
if (useCamera)
35-
cap.open(0);
38+
if (file.empty())
39+
cap.open(camera);
3640
else
3741
cap.open(file.c_str());
3842
if (!cap.isOpened())
3943
{
40-
cout << "Can not open video stream: '" << (useCamera ? "<camera 0>" : file) << "'" << endl;
44+
cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
4145
return 2;
4246
}
4347

samples/cpp/opencv_version.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
15
#include <opencv2/core/utility.hpp>
26
#include <iostream>
37

4-
const char* keys =
5-
{
6-
"{ b build | | print complete build info }"
7-
"{ h help | | print this help }"
8-
};
8+
static const std::string keys = "{ b build | | print complete build info }"
9+
"{ h help | | print this help }";
910

1011
int main(int argc, const char* argv[])
1112
{
1213
cv::CommandLineParser parser(argc, argv, keys);
13-
14+
parser.about("This sample outputs OpenCV version and build configuration.");
1415
if (parser.has("help"))
1516
{
1617
parser.printMessage();
@@ -27,6 +28,5 @@ int main(int argc, const char* argv[])
2728
{
2829
std::cout << "Welcome to OpenCV " << CV_VERSION << std::endl;
2930
}
30-
3131
return 0;
3232
}

samples/cpp/peopledetect.cpp

Lines changed: 80 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,126 @@
1-
#include <iostream>
2-
#include <stdexcept>
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
35
#include <opencv2/objdetect.hpp>
46
#include <opencv2/highgui.hpp>
57
#include <opencv2/imgproc.hpp>
6-
#include <opencv2/imgcodecs.hpp>
7-
#include <opencv2/video.hpp>
88
#include <opencv2/videoio.hpp>
9+
#include <iostream>
10+
#include <iomanip>
911

1012
using namespace cv;
1113
using namespace std;
1214

13-
14-
const char* keys =
15+
class Detector
1516
{
16-
"{ help h | | print help message }"
17-
"{ image i | | specify input image}"
18-
"{ camera c | | enable camera capturing }"
19-
"{ video v | ../data/vtest.avi | use video as input }"
20-
"{ directory d | | images directory}"
21-
};
22-
23-
static void detectAndDraw(const HOGDescriptor &hog, Mat &img)
24-
{
25-
vector<Rect> found, found_filtered;
26-
double t = (double) getTickCount();
27-
// Run the detector with default parameters. to get a higher hit-rate
28-
// (and more false alarms, respectively), decrease the hitThreshold and
29-
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
30-
hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
31-
t = (double) getTickCount() - t;
32-
cout << "detection time = " << (t*1000./cv::getTickFrequency()) << " ms" << endl;
33-
34-
for(size_t i = 0; i < found.size(); i++ )
17+
enum Mode { Default, Daimler } m;
18+
HOGDescriptor hog, hog_d;
19+
public:
20+
Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9)
3521
{
36-
Rect r = found[i];
37-
38-
size_t j;
39-
// Do not add small detections inside a bigger detection.
40-
for ( j = 0; j < found.size(); j++ )
41-
if ( j != i && (r & found[j]) == r )
42-
break;
43-
44-
if ( j == found.size() )
45-
found_filtered.push_back(r);
22+
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
23+
hog_d.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());
4624
}
47-
48-
for (size_t i = 0; i < found_filtered.size(); i++)
25+
void toggleMode() { m = (m == Default ? Daimler : Default); }
26+
string modeName() const { return (m == Default ? "Default" : "Daimler"); }
27+
vector<Rect> detect(InputArray img)
28+
{
29+
// Run the detector with default parameters. to get a higher hit-rate
30+
// (and more false alarms, respectively), decrease the hitThreshold and
31+
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
32+
vector<Rect> found;
33+
if (m == Default)
34+
hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2, false);
35+
else if (m == Daimler)
36+
hog_d.detectMultiScale(img, found, 0.5, Size(8,8), Size(32,32), 1.05, 2, true);
37+
return found;
38+
}
39+
void adjustRect(Rect & r) const
4940
{
50-
Rect r = found_filtered[i];
51-
5241
// The HOG detector returns slightly larger rectangles than the real objects,
5342
// so we slightly shrink the rectangles to get a nicer output.
5443
r.x += cvRound(r.width*0.1);
5544
r.width = cvRound(r.width*0.8);
5645
r.y += cvRound(r.height*0.07);
5746
r.height = cvRound(r.height*0.8);
58-
rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
5947
}
60-
}
48+
};
49+
50+
static const string keys = "{ help h | | print help message }"
51+
"{ camera c | 0 | capture video from camera (device index starting from 0) }"
52+
"{ video v | | use video as input }";
6153

6254
int main(int argc, char** argv)
6355
{
6456
CommandLineParser parser(argc, argv, keys);
65-
57+
parser.about("This sample demonstrates the use ot the HoG descriptor.");
6658
if (parser.has("help"))
6759
{
68-
cout << "\nThis program demonstrates the use of the HoG descriptor using\n"
69-
" HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n";
7060
parser.printMessage();
71-
cout << "During execution:\n\tHit q or ESC key to quit.\n"
72-
"\tUsing OpenCV version " << CV_VERSION << "\n"
73-
"Note: camera device number must be different from -1.\n" << endl;
7461
return 0;
7562
}
76-
77-
HOGDescriptor hog;
78-
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
79-
namedWindow("people detector", 1);
80-
81-
string pattern_glob = "";
82-
string video_filename = "../data/vtest.avi";
83-
int camera_id = -1;
84-
if (parser.has("directory"))
63+
int camera = parser.get<int>("camera");
64+
string file = parser.get<string>("video");
65+
if (!parser.check())
8566
{
86-
pattern_glob = parser.get<string>("directory");
67+
parser.printErrors();
68+
return 1;
8769
}
88-
else if (parser.has("image"))
89-
{
90-
pattern_glob = parser.get<string>("image");
91-
}
92-
else if (parser.has("camera"))
93-
{
94-
camera_id = parser.get<int>("camera");
95-
}
96-
else if (parser.has("video"))
70+
71+
VideoCapture cap;
72+
if (file.empty())
73+
cap.open(camera);
74+
else
75+
cap.open(file.c_str());
76+
if (!cap.isOpened())
9777
{
98-
video_filename = parser.get<string>("video");
78+
cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
79+
return 2;
9980
}
10081

101-
if (!pattern_glob.empty() || camera_id != -1 || !video_filename.empty())
82+
cout << "Press 'q' or <ESC> to quit." << endl;
83+
cout << "Press <space> to toggle between Default and Daimler detector" << endl;
84+
Detector detector;
85+
Mat frame;
86+
for (;;)
10287
{
103-
//Read from input image files
104-
vector<String> filenames;
105-
//Read from video file
106-
VideoCapture vc;
107-
Mat frame;
108-
109-
if (!pattern_glob.empty())
88+
cap >> frame;
89+
if (frame.empty())
11090
{
111-
String folder(pattern_glob);
112-
glob(folder, filenames);
91+
cout << "Finished reading: empty frame" << endl;
92+
break;
11393
}
114-
else if (camera_id != -1)
94+
int64 t = getTickCount();
95+
vector<Rect> found = detector.detect(frame);
96+
t = getTickCount() - t;
97+
98+
// show the window
11599
{
116-
vc.open(camera_id);
117-
if (!vc.isOpened())
118-
{
119-
stringstream msg;
120-
msg << "can't open camera: " << camera_id;
121-
throw runtime_error(msg.str());
122-
}
100+
ostringstream buf;
101+
buf << "Mode: " << detector.modeName() << " ||| "
102+
<< "FPS: " << fixed << setprecision(1) << (getTickFrequency() / (double)t);
103+
putText(frame, buf.str(), Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255), 2, LINE_AA);
123104
}
124-
else
105+
for (vector<Rect>::iterator i = found.begin(); i != found.end(); ++i)
125106
{
126-
vc.open(video_filename.c_str());
127-
if (!vc.isOpened())
128-
throw runtime_error(string("can't open video file: " + video_filename));
107+
Rect &r = *i;
108+
detector.adjustRect(r);
109+
rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 2);
129110
}
111+
imshow("People detector", frame);
130112

131-
vector<String>::const_iterator it_image = filenames.begin();
132-
133-
for (;;)
113+
// interact with user
114+
const char key = (char)waitKey(30);
115+
if (key == 27 || key == 'q') // ESC
134116
{
135-
if (!pattern_glob.empty())
136-
{
137-
bool read_image_ok = false;
138-
for (; it_image != filenames.end(); ++it_image)
139-
{
140-
cout << "\nRead: " << *it_image << endl;
141-
// Read current image
142-
frame = imread(*it_image);
143-
144-
if (!frame.empty())
145-
{
146-
++it_image;
147-
read_image_ok = true;
148-
break;
149-
}
150-
}
151-
152-
//No more valid images
153-
if (!read_image_ok)
154-
{
155-
//Release the image in order to exit the while loop
156-
frame.release();
157-
}
158-
}
159-
else
160-
{
161-
vc >> frame;
162-
}
163-
164-
if (frame.empty())
165-
break;
166-
167-
detectAndDraw(hog, frame);
168-
169-
imshow("people detector", frame);
170-
int c = waitKey( vc.isOpened() ? 30 : 0 ) & 255;
171-
if ( c == 'q' || c == 'Q' || c == 27)
172-
break;
117+
cout << "Exit requested" << endl;
118+
break;
119+
}
120+
else if (key == ' ')
121+
{
122+
detector.toggleMode();
173123
}
174124
}
175-
176125
return 0;
177126
}

0 commit comments

Comments
 (0)