Skip to content

Commit

Permalink
Merge pull request fw4spl-org#2 from maciejmatuszak/remove_static_var
Browse files Browse the repository at this point in the history
removing static variables to be thread-safe
  • Loading branch information
yunchih committed May 18, 2017
2 parents cd9c88d + b75ba71 commit c37a8cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 4 additions & 0 deletions include/ORBextractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <list>
#include <opencv/cv.h>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudafilters.hpp>
#include <cuda/Fast.hpp>
#include <cuda/Orb.hpp>

Expand Down Expand Up @@ -90,6 +91,7 @@ class ORBextractor
std::vector<cv::cuda::GpuMat> mvImagePyramid;
std::vector<cv::cuda::GpuMat> mvImagePyramidBorder;


protected:

void ComputePyramid(cv::Mat image);
Expand All @@ -99,6 +101,8 @@ class ORBextractor

void ComputeKeyPointsOld(std::vector<std::vector<cv::KeyPoint> >& allKeypoints);
std::vector<cv::Point> pattern;
cv::Ptr<cv::cuda::Filter> mpGaussianFilter;
cuda::Stream mcsStream;

int nfeatures;
double scaleFactor;
Expand Down
17 changes: 8 additions & 9 deletions src/ORBextractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include <opencv2/cudafeatures2d.hpp>
#include <opencv2/cudawarping.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudafilters.hpp>
#include <ORBextractor.h>
#include <cuda/Allocator.hpp>
#include <cuda/Fast.hpp>
Expand Down Expand Up @@ -693,7 +692,6 @@ void ORBextractor::ComputeKeyPointsOctTree(vector<vector<KeyPoint>>& allKeypoint
{
allKeypoints.resize(nlevels);

static Ptr<cv::cuda::Filter> gaussianFilter = cv::cuda::createGaussianFilter(mvImagePyramid[0].type(), mvImagePyramid[0].type(), Size(7, 7), 2, 2, BORDER_REFLECT_101);
const int minBorderX = EDGE_THRESHOLD-3;
const int minBorderY = minBorderX;
for (int level = 0; level < nlevels; ++level)
Expand All @@ -718,7 +716,7 @@ void ORBextractor::ComputeKeyPointsOctTree(vector<vector<KeyPoint>>& allKeypoint
if (level != 0) {
ic_angle.launch_async(mvImagePyramid[level-1], allKeypoints[level-1].data(), allKeypoints[level-1].size(), HALF_PATCH_SIZE, minBorderX, minBorderY, level-1, PATCH_SIZE * mvScaleFactor[level-1]);
cv::cuda::GpuMat &gMat = mvImagePyramid[level-1];
gaussianFilter->apply(gMat, gMat, ic_angle.cvStream());
mpGaussianFilter->apply(gMat, gMat, ic_angle.cvStream());
}

vector<KeyPoint> & keypoints = allKeypoints[level];
Expand All @@ -741,7 +739,7 @@ void ORBextractor::ComputeKeyPointsOctTree(vector<vector<KeyPoint>>& allKeypoint
// compute orientations
cv::cuda::GpuMat &gMat = mvImagePyramid[nlevels-1];
ic_angle.launch_async(gMat, allKeypoints[nlevels-1].data(), allKeypoints[nlevels-1].size(), HALF_PATCH_SIZE, minBorderX, minBorderY, nlevels-1, PATCH_SIZE * mvScaleFactor[nlevels-1]);
gaussianFilter->apply(gMat, gMat, ic_angle.cvStream());
mpGaussianFilter->apply(gMat, gMat, ic_angle.cvStream());
ic_angle.join(allKeypoints[nlevels-1].data(), allKeypoints[nlevels-1].size());
}

Expand Down Expand Up @@ -834,25 +832,26 @@ void ORBextractor::ComputePyramid(Mat image) {
}
mvImagePyramidBorder.resize(nlevels);
mvImagePyramid.resize(nlevels);
mpGaussianFilter = cv::cuda::createGaussianFilter(mvImagePyramid[0].type(), mvImagePyramid[0].type(), Size(7, 7), 2, 2, BORDER_REFLECT_101);
mvImagePyramidAllocatedFlag = true;
}
static cuda::Stream cvStream;

for (int level = 0; level < nlevels; ++level) {
float scale = mvInvScaleFactor[level];
Size sz(cvRound((float)image.cols*scale), cvRound((float)image.rows*scale));
cuda::GpuMat target(mvImagePyramidBorder[level]);
// Compute the resized image
if (level != 0) {
cuda::resize(mvImagePyramid[level-1], mvImagePyramid[level], sz, 0, 0, INTER_LINEAR, cvStream);
cuda::resize(mvImagePyramid[level-1], mvImagePyramid[level], sz, 0, 0, INTER_LINEAR, mcsStream);
cuda::copyMakeBorder(mvImagePyramid[level], target, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD,
BORDER_REFLECT_101, cv::Scalar(), cvStream);
BORDER_REFLECT_101, cv::Scalar(), mcsStream);
} else {
cuda::GpuMat gpuImg(image);
cuda::copyMakeBorder(gpuImg, target, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD,
BORDER_REFLECT_101, cv::Scalar(), cvStream);
BORDER_REFLECT_101, cv::Scalar(), mcsStream);
}
}
cvStream.waitForCompletion();
mcsStream.waitForCompletion();
}

} //namespace ORB_SLAM

0 comments on commit c37a8cb

Please sign in to comment.