1414using namespace std ;
1515
1616#include " opencv.hpp"
17-
1817#include " camerainstruction.hpp"
18+ #include " globals.hpp"
1919#include " ../utils/logger.hpp"
2020
2121constexpr int OK_KEY = 121 ;
2222constexpr int NOK_KEY = 110 ;
2323
24+ /* *
25+ * @brief Determine if the camera is in grayscale.
26+ *
27+ * @throw CameraException if unable to open the camera device
28+ *
29+ * @return true if so, otheriwse false.
30+ */
31+ bool Camera::isGrayscale ()
32+ {
33+ unique_ptr<cv::Mat> frame = read1 ();
34+
35+ if (frame->channels () != 3 )
36+ return false ;
37+
38+ for (int r = 0 ; r < frame->rows ; ++r)
39+ for (int c = 0 ; c < frame->cols ; ++c)
40+ {
41+ const cv::Vec3b &pixel = frame->at <cv::Vec3b>(r, c);
42+ if (pixel[0 ] != pixel[1 ] || pixel[0 ] != pixel[2 ])
43+ return false ;
44+ }
45+
46+ return true ;
47+ }
48+
49+ /* *
50+ * @brief Find a grayscale camera.
51+ *
52+ * @return path to the graycale device,
53+ * nullptr if unable to find such device
54+ */
55+ shared_ptr<Camera> Camera::findGrayscaleCamera ()
56+ {
57+ auto v4lDevices = get_v4l_devices ();
58+ for (auto &device : *v4lDevices)
59+ {
60+ auto camera = make_shared<Camera>(device);
61+ try
62+ {
63+ if (camera->isGrayscale ())
64+ return camera;
65+ }
66+ catch (CameraException &e)
67+ { // ignore
68+ }
69+ }
70+
71+ return nullptr ;
72+ }
73+
2474/* *
2575 * @brief Get the file descriptor previously opened
2676 *
@@ -46,16 +96,14 @@ shared_ptr<cv::VideoCapture> Camera::getCap() const noexcept
4696 *
4797 * @param device path to the camera
4898 *
49- * @throw runtime_error if unable to obtain the /dev/videoX path
50- *
5199 * @return the device id
52100 */
53101int Camera::deviceId (const string &device)
54102{
55103 std::unique_ptr<char [], decltype (&free)> devDevice (realpath (device.c_str (), nullptr ), &free);
56- int id;
104+ int id = 0 ;
57105 if (devDevice == nullptr || sscanf (devDevice.get (), " /dev/video%d" , &id) != 1 )
58- throw runtime_error ( " CRITICAL: Unable to obtain the /dev/videoX path" );
106+ Logger::critical (ExitCode::FAILURE, " Unable to obtain the /dev/videoX path" );
59107 return id;
60108}
61109
@@ -340,7 +388,7 @@ uint16_t Camera::lenUvcQuery(uint8_t unit, uint8_t selector)
340388 return len;
341389}
342390
343- CameraException::CameraException (const string &device) : message(" CRITICAL: Cannot access to " + device) {}
391+ CameraException::CameraException (const string &device) : message(" Cannot access to " + device) {}
344392
345393const char *CameraException::what () const noexcept
346394{
0 commit comments