Skip to content

Commit

Permalink
Adding FOURCC usb camera support
Browse files Browse the repository at this point in the history
  • Loading branch information
matlabbe committed Oct 26, 2024
1 parent e3fde4e commit 9b1ac02
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 48 deletions.
2 changes: 2 additions & 0 deletions corelib/include/rtabmap/core/camera/CameraStereoVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class RTABMAP_CORE_EXPORT CameraStereoVideo :
virtual std::string getSerial() const;

void setResolution(int width, int height) {_width=width, _height=height;}
void setFOURCC(const std::string & fourcc) { _fourcc = fourcc; }

protected:
virtual SensorData captureImage(SensorCaptureInfo * info = 0);
Expand All @@ -86,6 +87,7 @@ class RTABMAP_CORE_EXPORT CameraStereoVideo :
int usbDevice2_;
int _width;
int _height;
std::string _fourcc;
};

} // namespace rtabmap
2 changes: 2 additions & 0 deletions corelib/include/rtabmap/core/camera/CameraVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class RTABMAP_CORE_EXPORT CameraVideo :
* has been loaded, thus resolution from calibration is used.
* */
void setResolution(int width, int height) {_width=width, _height=height;}
void setFOURCC(const std::string & fourcc) { _fourcc = fourcc; }

protected:
virtual SensorData captureImage(SensorCaptureInfo * info = 0);
Expand All @@ -79,6 +80,7 @@ class RTABMAP_CORE_EXPORT CameraVideo :
std::string _guid;
int _width;
int _height;
std::string _fourcc;

CameraModel _model;
};
Expand Down
39 changes: 39 additions & 0 deletions corelib/src/camera/CameraStereoVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,45 @@ bool CameraStereoVideo::init(const std::string & calibrationFolder, const std::s
}
}
}

// Set FPS
if (this->getFrameRate() > 0)
{
bool fpsSupported = false;
fpsSupported = capture_.set(CV_CAP_PROP_FPS, this->getFrameRate());
if (capture2_.isOpened())
{
fpsSupported = fpsSupported && capture2_.set(CV_CAP_PROP_FPS, this->getFrameRate());
}
if(fpsSupported)
{
this->setFrameRate(0);
}
}

// Set FOURCC
if (!_fourcc.empty())
{
if(_fourcc.size() == 4)
{
std::string fourccUpperCase = uToUpperCase(_fourcc);
int fourcc = cv::VideoWriter::fourcc(fourccUpperCase.at(0), fourccUpperCase.at(1), fourccUpperCase.at(2), fourccUpperCase.at(3));
bool fourccSupported = false;
fourccSupported = capture_.set(CV_CAP_PROP_FOURCC, fourcc);
if (capture2_.isOpened())
{
fourccSupported = fourccSupported && capture2_.set(CV_CAP_PROP_FOURCC, fourcc);
}
if(!fourccSupported)
{
UWARN("Camera doesn't support provided FOURCC \"%s\"", fourccUpperCase.c_str());
}
}
else
{
UERROR("FOURCC parameter should be 4 characters, current value is \"%s\"", _fourcc.c_str());
}
}
}

if(rectifyImages_ && !stereoModel_.isValidForRectification())
Expand Down
24 changes: 24 additions & 0 deletions corelib/src/camera/CameraVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ bool CameraVideo::init(const std::string & calibrationFolder, const std::string
_capture.set(CV_CAP_PROP_FRAME_WIDTH, _width);
_capture.set(CV_CAP_PROP_FRAME_HEIGHT, _height);
}

// Set FPS
if (this->getFrameRate() > 0 && _capture.set(CV_CAP_PROP_FPS, this->getFrameRate()))
{
this->setFrameRate(0);
}

// Set FOURCC
if (!_fourcc.empty())
{
if(_fourcc.size() == 4)
{
std::string fourccUpperCase = uToUpperCase(_fourcc);
int fourcc = cv::VideoWriter::fourcc(fourccUpperCase.at(0), fourccUpperCase.at(1), fourccUpperCase.at(2), fourccUpperCase.at(3));
if(!_capture.set(CV_CAP_PROP_FOURCC, fourcc))
{
UWARN("Camera doesn't support provided FOURCC \"%s\"", fourccUpperCase.c_str());
}
}
else
{
UERROR("FOURCC parameter should be 4 characters, current value is \"%s\"", _fourcc.c_str());
}
}
}
if(_rectifyImages && !_model.isValidForRectification())
{
Expand Down
12 changes: 12 additions & 0 deletions guilib/src/PreferencesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,7 @@ void PreferencesDialog::readCameraSettings(const QString & filePath)
_ui->spinBox_stereo_right_device->setValue(settings.value("device2", _ui->spinBox_stereo_right_device->value()).toInt());
_ui->spinBox_stereousbcam_streamWidth->setValue(settings.value("width", _ui->spinBox_stereousbcam_streamWidth->value()).toInt());
_ui->spinBox_stereousbcam_streamHeight->setValue(settings.value("height", _ui->spinBox_stereousbcam_streamHeight->value()).toInt());
_ui->lineEdit_stereousbcam_fourcc->setText(settings.value("fourcc", _ui->lineEdit_stereousbcam_fourcc->text()).toString());
settings.endGroup(); // StereoVideo

settings.beginGroup("StereoZed");
Expand Down Expand Up @@ -2809,6 +2810,7 @@ void PreferencesDialog::readCameraSettings(const QString & filePath)
settings.beginGroup("UsbCam");
_ui->spinBox_usbcam_streamWidth->setValue(settings.value("width", _ui->spinBox_usbcam_streamWidth->value()).toInt());
_ui->spinBox_usbcam_streamHeight->setValue(settings.value("height", _ui->spinBox_usbcam_streamHeight->value()).toInt());
_ui->lineEdit_usbcam_fourcc->setText(settings.value("fourcc", _ui->lineEdit_usbcam_fourcc->text()).toString());
settings.endGroup(); // UsbCam

settings.beginGroup("Video");
Expand Down Expand Up @@ -3332,6 +3334,7 @@ void PreferencesDialog::writeCameraSettings(const QString & filePath) const
settings.setValue("device2", _ui->spinBox_stereo_right_device->value());
settings.setValue("width", _ui->spinBox_stereousbcam_streamWidth->value());
settings.setValue("height", _ui->spinBox_stereousbcam_streamHeight->value());
settings.setValue("fourcc", _ui->lineEdit_stereousbcam_fourcc->text());
settings.endGroup(); // StereoVideo

settings.beginGroup("StereoZed");
Expand Down Expand Up @@ -3407,6 +3410,7 @@ void PreferencesDialog::writeCameraSettings(const QString & filePath) const
settings.beginGroup("UsbCam");
settings.setValue("width", _ui->spinBox_usbcam_streamWidth->value());
settings.setValue("height", _ui->spinBox_usbcam_streamHeight->value());
settings.setValue("fourcc", _ui->lineEdit_usbcam_fourcc->text());
settings.endGroup(); // UsbCam

settings.beginGroup("Video");
Expand Down Expand Up @@ -6738,6 +6742,10 @@ Camera * PreferencesDialog::createCamera(
this->getSourceLocalTransform());
}
((CameraStereoVideo*)camera)->setResolution(_ui->spinBox_stereousbcam_streamWidth->value(), _ui->spinBox_stereousbcam_streamHeight->value());
if(!_ui->lineEdit_stereousbcam_fourcc->text().isEmpty())
{
((CameraStereoVideo*)camera)->setFOURCC(_ui->lineEdit_stereousbcam_fourcc->text().toStdString());
}
}
else if(driver == kSrcStereoVideo)
{
Expand Down Expand Up @@ -6866,6 +6874,10 @@ Camera * PreferencesDialog::createCamera(
this->getGeneralInputRate(),
this->getSourceLocalTransform());
((CameraVideo*)camera)->setResolution(_ui->spinBox_usbcam_streamWidth->value(), _ui->spinBox_usbcam_streamHeight->value());
if(!_ui->lineEdit_usbcam_fourcc->text().isEmpty())
{
((CameraVideo*)camera)->setFOURCC(_ui->lineEdit_usbcam_fourcc->text().toStdString());
}
}
else if(driver == kSrcVideo)
{
Expand Down
Loading

0 comments on commit 9b1ac02

Please sign in to comment.