Skip to content
Snippets Groups Projects
Commit 95d51f0b authored by Sebastian Hahta's avatar Sebastian Hahta
Browse files

more refactoring: disparity class use new frame class

parent 82e194d5
No related branches found
No related tags found
2 merge requests!105CUDA optical flow smoothing,!103feature/frame class
Pipeline #13343 passed
This commit is part of merge request !105. Comments created here will be created in the context of that merge request.
...@@ -4,36 +4,13 @@ ...@@ -4,36 +4,13 @@
#include <Eigen/Eigen> #include <Eigen/Eigen>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <ftl/rgbd/camera.hpp> #include <ftl/rgbd/camera.hpp>
#include <ftl/rgbd/frame.hpp>
namespace ftl{ namespace ftl{
namespace rgbd { namespace rgbd {
class Source; class Source;
// TODO: frame.hpp has to know the exact amount of channels
// (remove hardcoded value by providing exact value elsewhere).
// Possibly move channels to frame.hpp?
typedef unsigned int channel_t;
static const channel_t kChanNone = 0;
static const channel_t kChanLeft = 0x0001;
static const channel_t kChanDepth = 0x0002;
static const channel_t kChanRight = 0x0004;
static const channel_t kChanDisparity = 0x0008;
static const channel_t kChanDeviation = 0x0010;
static const channel_t kChanNormals = 0x0020;
static const channel_t kChanConfidence = 0x0040;
static const channel_t kChanFlow = 0x0080;
static const channel_t kChanEnergy = 0x0100;
static const channel_t kChanOverlay1 = 0x1000;
inline bool isFloatChannel(ftl::rgbd::channel_t chan) {
return (chan == ftl::rgbd::kChanDepth || chan == ftl::rgbd::kChanEnergy);
}
typedef unsigned int capability_t; typedef unsigned int capability_t;
static const capability_t kCapMovable = 0x0001; // A movable virtual cam static const capability_t kCapMovable = 0x0001; // A movable virtual cam
......
#pragma once #pragma once
#ifndef _FTL_RGBD_FRAME_HPP_
#define _FTL_RGBD_FRAME_HPP_
#include <ftl/rgbd/detail/source.hpp>
#include <ftl/configuration.hpp> #include <ftl/configuration.hpp>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include <opencv2/core/cuda.hpp>
namespace ftl { namespace ftl {
namespace rgbd { namespace rgbd {
typedef unsigned int channel_t;
static const channel_t kChanNone = 0;
static const channel_t kChanLeft = 0x0001;
static const channel_t kChanDepth = 0x0002;
static const channel_t kChanRight = 0x0004;
static const channel_t kChanDisparity = 0x0008;
static const channel_t kChanDeviation = 0x0010;
static const channel_t kChanNormals = 0x0020;
static const channel_t kChanConfidence = 0x0040;
static const channel_t kChanFlow = 0x0080;
static const channel_t kChanEnergy = 0x0100;
static const channel_t kChanOverlay1 = 0x1000;
// maximum number of available channels
static const unsigned int n_channels = 11;
inline bool isFloatChannel(ftl::rgbd::channel_t chan) {
return (chan == ftl::rgbd::kChanDepth || chan == ftl::rgbd::kChanEnergy);
}
class Frame;
class Frame { class Frame {
public: public:
/* @todo TODO: REMOVE HARDCODED CHANNEL COUNT! Frame() : channels_host_(n_channels),
*/ channels_gpu_(n_channels),
Frame() : channels_host_(11), channels_gpu_(11), available_(11, 0) {} available_(n_channels, 0)
{}
/* @brief Reset all channels without releasing memory. /* @brief Reset all channels without releasing memory.
*/ */
...@@ -27,16 +54,27 @@ public: ...@@ -27,16 +54,27 @@ public:
return available_[_channelIdx(channel)]; return available_[_channelIdx(channel)];
} }
/* @brief get reference to the channel contents /* @brief Method to get reference to the channel content
* @param channel type * @param Channel type
* @returns const reference to channel data * @returns Const reference to channel data
* *
* Methods automatically copy between host/gpu if the data is only available * Result is valid only if hasChannel() is true. Host/Gpu transfer is
* in the other. Results are cached. Result is valid only if hasChannel() is * performed, if necessary, but only once unless channel contents is
* true. * changed by calling setChannel(). Return value valid only if
* hasChannel(channel) is true.
*/ */
template <typename T> const T& getChannel(const ftl::rgbd::channel_t& channel); template <typename T> const T& getChannel(const ftl::rgbd::channel_t& channel);
/* @brief Method to set/modify channel content
* @param Channel type
* @returns Reference to channel data
*
* Returns non-const reference to channel data. Invalidates other copies of
* the data (host/gpu) for the specified channel, next time getChannel() is
* called a memory transfer may occur.
*/
template <typename T> T& setChannel(const ftl::rgbd::channel_t& channel); template <typename T> T& setChannel(const ftl::rgbd::channel_t& channel);
private: private:
...@@ -70,5 +108,12 @@ private: ...@@ -70,5 +108,12 @@ private:
std::vector<uint> available_; std::vector<uint> available_;
}; };
template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel);
template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel);
template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel);
template<> cv::cuda::GpuMat& Frame::setChannel(const ftl::rgbd::channel_t& channel);
} }
} }
#endif // _FTL_RGBD_FRAME_HPP_
\ No newline at end of file
...@@ -64,9 +64,17 @@ void FixstarsSGM::init(const cv::Size size) { ...@@ -64,9 +64,17 @@ void FixstarsSGM::init(const cv::Size size) {
); );
} }
void FixstarsSGM::compute(const cv::cuda::GpuMat &l, const cv::cuda::GpuMat &r, void FixstarsSGM::compute(ftl::rgbd::Frame &frame, cv::cuda::Stream &stream)
cv::cuda::GpuMat &disp, cv::cuda::Stream &stream)
{ {
const auto &l = frame.getChannel<GpuMat>(ftl::rgbd::kChanLeft);
const auto &r = frame.getChannel<GpuMat>(ftl::rgbd::kChanRight);
auto &disp = frame.setChannel<GpuMat>(ftl::rgbd::kChanDisparity);
if (disp.size() != l.size())
{
disp = GpuMat(l.size(), CV_32FC1);
}
if (l.size() != size_) { if (l.size() != size_) {
// re-use same buffer for l/r // re-use same buffer for l/r
cv::cuda::resize(r, l_downscaled_, size_, 0.0, 0.0, cv::INTER_CUBIC, stream); cv::cuda::resize(r, l_downscaled_, size_, 0.0, 0.0, cv::INTER_CUBIC, stream);
......
...@@ -30,7 +30,7 @@ namespace ftl { ...@@ -30,7 +30,7 @@ namespace ftl {
public: public:
explicit FixstarsSGM(nlohmann::json &config); explicit FixstarsSGM(nlohmann::json &config);
void compute(const cv::cuda::GpuMat &l, const cv::cuda::GpuMat &r, cv::cuda::GpuMat &disp, cv::cuda::Stream &stream) override; void compute(ftl::rgbd::Frame &frame, cv::cuda::Stream &stream) override;
void setMask(cv::Mat &mask) override; void setMask(cv::Mat &mask) override;
/* Factory creator */ /* Factory creator */
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <ftl/configurable.hpp> #include <ftl/configurable.hpp>
#include <ftl/rgbd/frame.hpp>
namespace ftl { namespace ftl {
namespace rgbd { namespace rgbd {
...@@ -32,7 +33,15 @@ class Disparity : public ftl::Configurable { ...@@ -32,7 +33,15 @@ class Disparity : public ftl::Configurable {
* Pure virtual function representing the actual computation of * Pure virtual function representing the actual computation of
* disparity from left and right images to be implemented. * disparity from left and right images to be implemented.
*/ */
virtual void compute(const cv::cuda::GpuMat &l, const cv::cuda::GpuMat &r, cv::cuda::GpuMat &disp, cv::cuda::Stream &stream)=0; virtual void compute(Frame &frame, cv::cuda::Stream &stream)=0;
virtual void compute(cv::cuda::GpuMat &l, cv::cuda::GpuMat &r, cv::cuda::GpuMat &disp, cv::cuda::Stream &stream)
{
ftl::rgbd::Frame frame;
frame.setChannel<cv::cuda::GpuMat>(kChanLeft) = l;
frame.setChannel<cv::cuda::GpuMat>(kChanRight) = r;
frame.setChannel<cv::cuda::GpuMat>(kChanDisparity) = disp;
}
/** /**
* Factory registration class. * Factory registration class.
......
...@@ -6,7 +6,7 @@ namespace rgbd { ...@@ -6,7 +6,7 @@ namespace rgbd {
template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel) template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); size_t idx = _channelIdx(channel);
if (!(available_[idx] & mask_host)) if (!(available_[idx] & mask_host))
{ {
if (available_[idx] & mask_gpu) if (available_[idx] & mask_gpu)
...@@ -21,14 +21,14 @@ template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel) ...@@ -21,14 +21,14 @@ template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel) template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); size_t idx = _channelIdx(channel);
available_[idx] = mask_host; available_[idx] = mask_host;
return channels_host_[idx]; return channels_host_[idx];
} }
template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel) template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); size_t idx = _channelIdx(channel);
if (!(available_[idx] & mask_gpu)) if (!(available_[idx] & mask_gpu))
{ {
if (available_[idx] & mask_host) if (available_[idx] & mask_host)
...@@ -43,7 +43,7 @@ template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& ...@@ -43,7 +43,7 @@ template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t&
template<> cv::cuda::GpuMat& Frame::setChannel(const ftl::rgbd::channel_t& channel) template<> cv::cuda::GpuMat& Frame::setChannel(const ftl::rgbd::channel_t& channel)
{ {
auto idx = _channelIdx(channel); size_t idx = _channelIdx(channel);
available_[idx] = mask_gpu; available_[idx] = mask_gpu;
return channels_gpu_[idx]; return channels_gpu_[idx];
} }
......
...@@ -217,16 +217,16 @@ bool StereoVideoSource::compute(int n, int b) { ...@@ -217,16 +217,16 @@ bool StereoVideoSource::compute(int n, int b) {
if (left.empty() || right.empty()) return false; if (left.empty() || right.empty()) return false;
if (chan == ftl::rgbd::kChanDepth) { if (chan == ftl::rgbd::kChanDepth) {
auto &depth = frame.setChannel<cv::cuda::GpuMat>(ftl::rgbd::kChanDepth); disp_->compute(frame, stream_);
auto &disp = frame.setChannel<cv::cuda::GpuMat>(ftl::rgbd::kChanDisparity);
auto &disp = frame.getChannel<cv::cuda::GpuMat>(ftl::rgbd::kChanDisparity);
auto &depth = frame.setChannel<cv::cuda::GpuMat>(ftl::rgbd::kChanDepth);
if (depth.empty()) depth = cv::cuda::GpuMat(left.size(), CV_32FC1); if (depth.empty()) depth = cv::cuda::GpuMat(left.size(), CV_32FC1);
if (disp.empty()) disp = cv::cuda::GpuMat(left.size(), CV_32FC1);
disp_->compute(left, right, disp, stream_);
ftl::cuda::disparity_to_depth(disp, depth, params_, stream_); ftl::cuda::disparity_to_depth(disp, depth, params_, stream_);
left.download(rgb_, stream_); left.download(rgb_, stream_);
depth.download(depth_, stream_); depth.download(depth_, stream_);
stream_.waitForCompletion(); // TODO:(Nick) Move to getFrames stream_.waitForCompletion(); // TODO:(Nick) Move to getFrames
} else if (chan == ftl::rgbd::kChanRight) { } else if (chan == ftl::rgbd::kChanRight) {
left.download(rgb_, stream_); left.download(rgb_, stream_);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment