31 #include <opencv2/highgui/highgui.hpp>
41 static const std::string windowNameAndInstructions(
42 "OSVR tracking camera preview | q or esc to quit");
44 static const auto FPS_MEASUREMENT_PERIOD = std::chrono::seconds(3);
50 auto now = clock::now();
53 std::chrono::duration_cast<std::chrono::duration<double>>(
55 std::cout << m_frames /
duration.count() <<
" FPS read from camera"
62 m_begin = clock::now();
63 m_end = m_begin + FPS_MEASUREMENT_PERIOD;
68 using clock = std::chrono::system_clock;
69 using time_point = std::chrono::time_point<clock>;
72 std::size_t m_frames = 0;
75 int main(
int argc,
char *argv[]) {
76 auto cam = getDirectShowHDKCamera();
77 if (!cam || !cam->read_image_to_memory()) {
78 std::cerr <<
"Couldn't find, open, or read from the OSVR HDK tracking "
80 <<
"Press enter to exit." << std::endl;
84 auto FRAME_DISPLAY_STRIDE = 3u;
86 auto is = std::istringstream{argv[1]};
87 if (is >> FRAME_DISPLAY_STRIDE) {
88 std::cout <<
"Custom display stride passed: "
89 << FRAME_DISPLAY_STRIDE << std::endl;
91 std::cout <<
"Could not parse first command-line argument as a "
93 << argv[1] <<
"' (will use default)" << std::endl;
96 std::cout <<
"Will display 1 out of every " << FRAME_DISPLAY_STRIDE
97 <<
" frames captured." << std::endl;
98 auto frame = cv::Mat{};
100 auto savedFrame =
false;
101 static const auto FILENAME =
"capture.png";
102 static const auto FILENAME_STEM =
"image";
103 static const auto EXTENSION =
".png";
104 auto captures = std::size_t{0};
106 cv::namedWindow(windowNameAndInstructions);
107 auto frameCount = std::size_t{0};
109 frame = retrieve(*cam);
112 if (frameCount % FRAME_DISPLAY_STRIDE == 0) {
114 cv::imshow(windowNameAndInstructions, frame);
117 cv::imwrite(FILENAME, frame);
118 std::cout <<
"Wrote a captured frame to " << FILENAME
121 char key =
static_cast<char>(cv::waitKey(1));
122 if (
'q' == key ||
'Q' == key || 27 == key) {
124 }
else if (
'c' == key) {
126 std::ostringstream os;
127 os << FILENAME_STEM << captures << EXTENSION;
128 cv::imwrite(os.str(), frame);
129 std::cout <<
"Captured frame to " << os.str() << std::endl;
133 }
while (cam->read_image_to_memory());
double duration(TimeValue const &a, TimeValue const &b)
Get a double containing seconds between the time points.