31 #include <opencv2/video/tracking.hpp>
33 #ifdef HAVE_OPENCV_CUDAOPTFLOW
34 #include <opencv2/cudaoptflow.hpp>
41 bool opticalFlow = Parameters::defaultStereoOpticalFlow();
49 return new Stereo(parameters);
54 winWidth_(
Parameters::defaultStereoWinWidth()),
55 winHeight_(
Parameters::defaultStereoWinHeight()),
56 iterations_(
Parameters::defaultStereoIterations()),
57 maxLevel_(
Parameters::defaultStereoMaxLevel()),
58 minDisparity_(
Parameters::defaultStereoMinDisparity()),
59 maxDisparity_(
Parameters::defaultStereoMaxDisparity()),
77 const cv::Mat & leftImage,
78 const cv::Mat & rightImage,
79 const std::vector<cv::Point2f> & leftCorners,
80 std::vector<unsigned char> & status)
const
82 std::vector<cv::Point2f> rightCorners;
83 UDEBUG(
"util2d::calcStereoCorrespondences() begin");
95 UDEBUG(
"util2d::calcStereoCorrespondences() end");
99 #ifdef HAVE_OPENCV_CUDEV
101 const cv::cuda::GpuMat & leftImage,
102 const cv::cuda::GpuMat & rightImage,
103 const std::vector<cv::Point2f> & leftCorners,
104 std::vector<unsigned char> & status)
const
106 UERROR(
"GPU support for this approach is not implemented!");
107 return std::vector<cv::Point2f>();
124 #ifndef HAVE_OPENCV_CUDAOPTFLOW
127 UERROR(
"%s is enabled but RTAB-Map is not built with OpenCV CUDA, disabling it.", Parameters::kStereoGpu().
c_str());
135 #ifdef HAVE_OPENCV_CUDAOPTFLOW
143 const cv::Mat & leftImage,
144 const cv::Mat & rightImage,
145 const std::vector<cv::Point2f> & leftCorners,
146 std::vector<unsigned char> & status)
const
148 std::vector<cv::Point2f> rightCorners;
149 std::vector<float> err;
150 #ifdef HAVE_OPENCV_CUDAOPTFLOW
153 cv::cuda::GpuMat d_leftImage(leftImage);
154 cv::cuda::GpuMat d_rightImage(rightImage);
160 UDEBUG(
"util2d::calcOpticalFlowPyrLKStereo() begin");
170 cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, this->
iterations(),
epsilon_),
171 cv::OPTFLOW_LK_GET_MIN_EIGENVALS, 1
e-4);
172 UDEBUG(
"util2d::calcOpticalFlowPyrLKStereo() end");
178 #ifdef HAVE_OPENCV_CUDEV
180 const cv::cuda::GpuMat & leftImage,
181 const cv::cuda::GpuMat & rightImage,
182 const std::vector<cv::Point2f> & leftCorners,
183 std::vector<unsigned char> & status)
const
185 std::vector<cv::Point2f> rightCorners;
186 #ifdef HAVE_OPENCV_CUDAOPTFLOW
187 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow transfer host to device begin");
188 cv::cuda::GpuMat d_leftImage(leftImage);
189 cv::cuda::GpuMat d_rightImage(rightImage);
190 cv::cuda::GpuMat d_leftCorners(leftCorners);
191 cv::cuda::GpuMat d_rightCorners;
192 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow transfer host to device end");
193 cv::cuda::GpuMat d_status;
194 cv::Ptr<cv::cuda::SparsePyrLKOpticalFlow> d_pyrLK_sparse = cv::cuda::SparsePyrLKOpticalFlow::create(
197 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow calc begin");
198 d_pyrLK_sparse->calc(d_leftImage, d_rightImage, d_leftCorners, d_rightCorners, d_status);
199 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow calc end");
201 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow transfer device to host begin");
203 rightCorners = std::vector<cv::Point2f>(d_rightCorners.cols);
204 cv::Mat matRightCorners(1, d_rightCorners.cols, CV_32FC2, (
void*)&rightCorners[0]);
205 d_rightCorners.download(matRightCorners);
207 status = std::vector<unsigned char>(d_status.cols);
208 cv::Mat matStatus(1, d_status.cols, CV_8UC1, (
void*)&status[0]);
209 d_status.download(matStatus);
210 UDEBUG(
"cv::cuda::SparsePyrLKOpticalFlow transfer device to host end");
215 UERROR(
"GPU support for this approach is not implemented!");
222 const std::vector<cv::Point2f> & leftCorners,
223 const std::vector<cv::Point2f> & rightCorners,
224 std::vector<unsigned char> & status)
const
226 UASSERT(leftCorners.size() == rightCorners.size() && status.size() == leftCorners.size());
227 int countFlowRejected = 0;
228 int countDisparityRejected = 0;
229 for(
unsigned int i=0;
i<status.size(); ++
i)
233 float disparity = leftCorners[
i].x - rightCorners[
i].x;
237 ++countDisparityRejected;
245 UDEBUG(
"total=%d countFlowRejected=%d countDisparityRejected=%d", (
int)status.size(), countFlowRejected, countDisparityRejected);