Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
Updating the test_camera app to support multiple cameras (up to 3).
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterSloth committed Jun 26, 2016
1 parent cb6d510 commit 7e0f99f
Showing 1 changed file with 118 additions and 61 deletions.
179 changes: 118 additions & 61 deletions src/tests/test_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,112 @@
#include "PSEyeVideoCapture.h"
#include "opencv2/opencv.hpp"
#include <algorithm>
#include <vector>

const std::vector<int> 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_state> 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
Expand All @@ -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;
}

0 comments on commit 7e0f99f

Please sign in to comment.