30 #include <opencv2/highgui/highgui.hpp>
38 #undef OSVR_ENABLE_RECORDING
42 static const std::string windowNameAndInstructions(
43 "OSVR tracking camera preview | q or esc to quit");
45 static const auto VIDEO_FILENAME =
"capture.avi";
47 static const auto FPS_MEASUREMENT_PERIOD = std::chrono::seconds(3);
53 auto now = clock::now();
56 std::chrono::duration_cast<std::chrono::duration<double>>(
58 std::cout << m_frames /
duration.count() <<
" FPS read from camera"
65 m_begin = clock::now();
66 m_end = m_begin + FPS_MEASUREMENT_PERIOD;
71 using clock = std::chrono::system_clock;
72 using time_point = std::chrono::time_point<clock>;
75 std::size_t m_frames = 0;
78 int main(
int argc,
char *argv[]) {
81 auto cam = osvr::vbtracker::openHDKCameraDirectShow();
83 std::cerr <<
"Warning: Just using OpenCV to open Camera #0, which may not "
84 "be the tracker camera."
86 auto cam = osvr::vbtracker::openOpenCVCamera(0);
88 if (!cam || !cam->ok()) {
89 std::cerr <<
"Couldn't find, open, or read from the OSVR HDK tracking "
91 <<
"Press enter to exit." << std::endl;
95 auto FRAME_DISPLAY_STRIDE = 3u;
99 if (ss >> FRAME_DISPLAY_STRIDE) {
100 std::cout <<
"Custom display stride passed: "
101 << FRAME_DISPLAY_STRIDE << std::endl;
103 std::cout <<
"Could not parse first command-line argument as a "
105 << argv[1] <<
"' (will use default)" << std::endl;
111 std::cout <<
"Will display 1 out of every " << FRAME_DISPLAY_STRIDE
112 <<
" frames captured." << std::endl;
113 std::cout <<
"\nPress q or esc to quit, c to capture a frame to file.\n"
116 auto frame = cv::Mat{};
117 auto grayFrame = cv::Mat{};
119 auto savedFrame =
false;
120 static const auto FILENAME =
"capture.png";
121 static const auto FILENAME_STEM =
"image";
122 static const auto EXTENSION =
".png";
123 auto captures = std::size_t{0};
126 #ifdef OSVR_ENABLE_RECORDING
127 std::cout <<
"\nThis build is also video capture enabled: continuously "
129 << VIDEO_FILENAME << std::endl;
130 cv::VideoWriter outputVideo{
"capture.avi",
131 CV_FOURCC_MACRO(
'M',
'J',
'P',
'G'), 100,
132 cv::Size(640, 480),
true};
133 if (!outputVideo.isOpened()) {
134 std::cerr <<
"Could not open the output video for writing!"
140 cv::namedWindow(windowNameAndInstructions);
141 auto frameCount = std::size_t{0};
143 cam->retrieve(frame, grayFrame);
145 #ifdef OSVR_ENABLE_RECORDING
146 outputVideo.write(frame);
151 if (frameCount % FRAME_DISPLAY_STRIDE == 0) {
153 cv::imshow(windowNameAndInstructions, frame);
156 cv::imwrite(FILENAME, frame);
157 std::cout <<
"Wrote a captured frame to " << FILENAME
160 char key =
static_cast<char>(cv::waitKey(1));
161 if (
'q' == key ||
'Q' == key || 27 == key) {
163 }
else if (
'c' == key) {
165 std::ostringstream os;
166 os << FILENAME_STEM << captures << EXTENSION;
167 cv::imwrite(os.str(), frame);
168 std::cout <<
"Captured frame to " << os.str() << std::endl;
172 }
while (cam->grab());
double duration(TimeValue const &a, TimeValue const &b)
Get a double containing seconds between the time points.