From 85ad0c55547b178c20f87b129a6ccaa6d798ffc9 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Sun, 27 Oct 2024 18:06:31 -0700 Subject: [PATCH] more checks and warnings --- corelib/src/camera/CameraStereoVideo.cpp | 25 ++++++++++++++++---- corelib/src/camera/CameraVideo.cpp | 30 +++++++++++++++++++----- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/corelib/src/camera/CameraStereoVideo.cpp b/corelib/src/camera/CameraStereoVideo.cpp index c11bc36624..3b1f1c41fb 100644 --- a/corelib/src/camera/CameraStereoVideo.cpp +++ b/corelib/src/camera/CameraStereoVideo.cpp @@ -209,12 +209,29 @@ bool CameraStereoVideo::init(const std::string & calibrationFolder, const std::s if(capture_.isOpened()) { - capture_.set(CV_CAP_PROP_FRAME_WIDTH, stereoModel_.left().imageWidth()*(capture2_.isOpened()?1:2)); - capture_.set(CV_CAP_PROP_FRAME_HEIGHT, stereoModel_.left().imageHeight()); + bool resolutionSet = false; + resolutionSet = capture_.set(CV_CAP_PROP_FRAME_WIDTH, stereoModel_.left().imageWidth()*(capture2_.isOpened()?1:2)); + resolutionSet = resolutionSet && capture_.set(CV_CAP_PROP_FRAME_HEIGHT, stereoModel_.left().imageHeight()); if(capture2_.isOpened()) { - capture2_.set(CV_CAP_PROP_FRAME_WIDTH, stereoModel_.right().imageWidth()); - capture2_.set(CV_CAP_PROP_FRAME_HEIGHT, stereoModel_.right().imageHeight()); + resolutionSet = resolutionSet && capture2_.set(CV_CAP_PROP_FRAME_WIDTH, stereoModel_.right().imageWidth()); + resolutionSet = resolutionSet && capture2_.set(CV_CAP_PROP_FRAME_HEIGHT, stereoModel_.right().imageHeight()); + } + + // Check if the resolution was set successfully + int actualWidth = int(capture_.get(CV_CAP_PROP_FRAME_WIDTH)); + int actualHeight = int(capture_.get(CV_CAP_PROP_FRAME_HEIGHT)); + if(!resolutionSet || + actualWidth != stereoModel_.left().imageWidth()*(capture2_.isOpened()?1:2) || + actualHeight != stereoModel_.left().imageHeight()) + { + UERROR("Calibration resolution (%dx%d) cannot be set to camera driver, " + "actual resolution is %dx%d. You would have to re-calibrate with one " + "supported format by your camera. " + "Do \"v4l2-ctl --list-formats-ext\" to list all supported " + "formats by your camera. For side-by-side format, you should set listed width/2.", + stereoModel_.left().imageWidth(), stereoModel_.left().imageHeight(), + actualWidth/(capture2_.isOpened()?1:2), actualHeight); } } } diff --git a/corelib/src/camera/CameraVideo.cpp b/corelib/src/camera/CameraVideo.cpp index 4bb12db890..9a0a70d0d9 100644 --- a/corelib/src/camera/CameraVideo.cpp +++ b/corelib/src/camera/CameraVideo.cpp @@ -141,19 +141,37 @@ bool CameraVideo::init(const std::string & calibrationFolder, const std::string _width, _height, _model.imageWidth(), _model.imageHeight()); } - - _capture.set(CV_CAP_PROP_FRAME_WIDTH, _model.imageWidth()); - _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _model.imageHeight()); + + bool resolutionSet = false; + resolutionSet = _capture.set(CV_CAP_PROP_FRAME_WIDTH, _model.imageWidth()); + resolutionSet = resolutionSet && _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _model.imageHeight()); + + // Check if the resolution was set successfully + int actualWidth = int(_capture.get(CV_CAP_PROP_FRAME_WIDTH)); + int actualHeight = int(_capture.get(CV_CAP_PROP_FRAME_HEIGHT)); + if(!resolutionSet || + actualWidth != _model.imageWidth() || + actualHeight != _model.imageHeight()) + { + UERROR("Calibration resolution (%dx%d) cannot be set to camera driver, " + "actual resolution is %dx%d. You would have to re-calibrate with one " + "supported format by your camera. " + "Do \"v4l2-ctl --list-formats-ext\" to list all supported " + "formats by your camera.", + _model.imageWidth(), _model.imageHeight(), + actualWidth, actualHeight); + } } else if(_width > 0 && _height > 0) { - _capture.set(CV_CAP_PROP_FRAME_WIDTH, _width); - _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _height); + int resolutionSet = false; + resolutionSet = _capture.set(CV_CAP_PROP_FRAME_WIDTH, _width); + resolutionSet = resolutionSet && _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _height); // Check if the resolution was set successfully int actualWidth = int(_capture.get(CV_CAP_PROP_FRAME_WIDTH)); int actualHeight = int(_capture.get(CV_CAP_PROP_FRAME_HEIGHT)); - if(actualWidth != _width || actualHeight != _height) + if(!resolutionSet || actualWidth != _width || actualHeight != _height) { UWARN("Desired resolution (%dx%d) cannot be set to camera driver, " "actual resolution is %dx%d. "