From 7e0f99fc0094cacd942200111e403b25b6281e42 Mon Sep 17 00:00:00 2001 From: HipsterSloth Date: Sat, 25 Jun 2016 22:19:06 -0700 Subject: [PATCH] Updating the test_camera app to support multiple cameras (up to 3). --- src/tests/test_camera.cpp | 179 +++++++++++++++++++++++++------------- 1 file changed, 118 insertions(+), 61 deletions(-) diff --git a/src/tests/test_camera.cpp b/src/tests/test_camera.cpp index ae47a04a..0404e76c 100644 --- a/src/tests/test_camera.cpp +++ b/src/tests/test_camera.cpp @@ -1,73 +1,112 @@ #include "PSEyeVideoCapture.h" #include "opencv2/opencv.hpp" #include +#include const std::vector known_keys = {113, 97, 119, 115, 101, 100, 114, 102, 116, 103}; // q, a, w, s, e, d, r, f, t, g +struct camera_state +{ + PSEyeVideoCapture *camera; + std::string identifier; +}; + int main(int, char**) { - PSEyeVideoCapture cap(0); // open the default camera - if(!cap.isOpened()) // check if we succeeded - return -1; + std::vector camera_states; + + // Open all available cameras (up to 3 max) + for (int camera_index = 0; camera_index < 3; ++camera_index) + { + PSEyeVideoCapture *camera = new PSEyeVideoCapture(camera_index); // open the default camera + + if (camera->isOpened()) + { + std::string identifier = camera->getUniqueIndentifier(); + + camera_states.push_back({ camera, identifier }); + } + else + { + delete camera; + } + } - cv::namedWindow("result",1); - for(;;) + // Create a window for each opened camera + std::for_each( + camera_states.begin(), + camera_states.end(), + [&camera_states](camera_state &state) { + cv::namedWindow(state.identifier.c_str(), 1); + } + ); + + bool bKeepRunning = camera_states.size() > 0; + while (bKeepRunning) { - cv::Mat frame; - cap >> frame; // get a new frame from camera - int wk = -1; - if (!frame.empty()) - { - imshow("result", frame); - wk = cv::waitKey(30); - } - + // Render each camera frame in it's own window + std::for_each( + camera_states.begin(), + camera_states.end(), + [&camera_states](camera_state &state) { + cv::Mat frame; + (*state.camera) >> frame; // get a new frame from camera + + if (!frame.empty()) + { + imshow(state.identifier.c_str(), frame); + } + } + ); + + int wk = cv::waitKey(10); + if (wk == 27) // Escape - { - break; - } - else if (std::find(known_keys.begin(), known_keys.end(), wk) != known_keys.end()) - { - int cap_prop = CV_CAP_PROP_EXPOSURE; - double val_diff = 0; - std::string prop_str("CV_CAP_PROP_EXPOSURE"); - - // q/a for +/- exposure + { + bKeepRunning = false; + } + else if (std::find(known_keys.begin(), known_keys.end(), wk) != known_keys.end()) + { + int cap_prop = CV_CAP_PROP_EXPOSURE; + double val_diff = 0; + std::string prop_str("CV_CAP_PROP_EXPOSURE"); + + // q/a for +/- exposure // CL Eye [0 255] - if ((wk == 113) || (wk == 97)) - { - cap_prop = CV_CAP_PROP_EXPOSURE; - prop_str = "CV_CAP_PROP_EXPOSURE"; - val_diff = (wk == 113) ? 10 : -10; - } - - // w/s for +/- contrast + if ((wk == 113) || (wk == 97)) + { + cap_prop = CV_CAP_PROP_EXPOSURE; + prop_str = "CV_CAP_PROP_EXPOSURE"; + val_diff = (wk == 113) ? 10 : -10; + } + + // w/s for +/- contrast // Note that, for CL EYE at least, changing contrast ALSO changes exposure // CL Eye [0 255] - if ((wk == 119) || (wk == 115)) - { - cap_prop = CV_CAP_PROP_CONTRAST; - prop_str = "CV_CAP_PROP_CONTRAST"; - val_diff = (wk == 119) ? 1 : -1; - } - - // e/d for +/- gain + if ((wk == 119) || (wk == 115)) + { + cap_prop = CV_CAP_PROP_CONTRAST; + prop_str = "CV_CAP_PROP_CONTRAST"; + val_diff = (wk == 119) ? 1 : -1; + } + + // e/d for +/- gain // For CL Eye, gain seems to be changing colour balance. - if ((wk == 101) || (wk == 100)) - { - cap_prop = CV_CAP_PROP_GAIN; - prop_str = "CV_CAP_PROP_GAIN"; - val_diff = (wk == 101) ? 4 : -4; - } - - // r/f for +/- hue - if ((wk == 114) || (wk == 102)) - { - cap_prop = CV_CAP_PROP_HUE; - prop_str = "CV_CAP_PROP_HUE"; - val_diff = (wk == 114) ? 1 : -1; - } + if ((wk == 101) || (wk == 100)) + { + cap_prop = CV_CAP_PROP_GAIN; + prop_str = "CV_CAP_PROP_GAIN"; + val_diff = (wk == 101) ? 4 : -4; + } + + // r/f for +/- hue + if ((wk == 114) || (wk == 102)) + { + cap_prop = CV_CAP_PROP_HUE; + prop_str = "CV_CAP_PROP_HUE"; + val_diff = (wk == 114) ? 1 : -1; + } // t/g for +/- sharpness // For CL_Eye, 1 - sharpness @@ -78,20 +117,38 @@ int main(int, char**) val_diff = (wk == 116) ? 1 : -1; } - double val = cap.get(cap_prop); - std::cout << "Value of " << prop_str << " was " << val << std::endl; + std::for_each( + camera_states.begin(), + camera_states.end(), + [&camera_states, &cap_prop, &prop_str, &val_diff](camera_state &state) { + + double val = state.camera->get(cap_prop); + std::cout << state.identifier << ": Value of " << prop_str << " was " << val << std::endl; - val += val_diff; - cap.set(cap_prop, val); + val += val_diff; + state.camera->set(cap_prop, val); - val = cap.get(cap_prop); - std::cout << "Value of " << prop_str << " changed by " << val_diff << " and is now " << val << std::endl; - } + val = state.camera->get(cap_prop); + std::cout << state.identifier << ": Value of " << prop_str << " changed by " << val_diff << " and is now " << val << std::endl; + } + ); + } else if (wk > 0) { std::cout << "Unknown key has code " << wk << std::endl; } } + + // Disconnect all active cameras + std::for_each( + camera_states.begin(), + camera_states.end(), + [&camera_states](camera_state &state) { + delete state.camera; + } + ); + camera_states.empty(); + // the camera will be deinitialized automatically in VideoCapture destructor return 0; } \ No newline at end of file