refactored cornerHarris and cornerMinEigenVal

* converted it into Algorithm
pull/1042/head
Vladislav Vinogradov 12 years ago
parent ad4d6bed9d
commit d7ff3ad0cf
  1. 36
      modules/gpuimgproc/include/opencv2/gpuimgproc.hpp
  2. 14
      modules/gpuimgproc/perf/perf_corners.cpp
  3. 153
      modules/gpuimgproc/src/corners.cpp
  4. 10
      modules/gpuimgproc/src/gftt.cpp
  5. 8
      modules/gpuimgproc/test/test_corners.cpp
  6. 6
      samples/gpu/performance/tests.cpp

@ -362,17 +362,37 @@ public:
////////////////////////// Corners Detection ///////////////////////////
class CV_EXPORTS CornernessCriteria : public Algorithm
{
public:
virtual void compute(InputArray src, OutputArray dst, Stream& stream = Stream::Null()) = 0;
};
//! computes Harris cornerness criteria at each image pixel
CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101);
CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101);
CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize, double k,
int borderType = BORDER_REFLECT101, Stream& stream = Stream::Null());
CV_EXPORTS Ptr<CornernessCriteria> createHarrisCorner(int srcType, int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101);
//! computes minimum eigen value of 2x2 derivative covariation matrix at each pixel - the cornerness criteria
CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, int borderType=BORDER_REFLECT101);
CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, int borderType=BORDER_REFLECT101);
CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize,
int borderType=BORDER_REFLECT101, Stream& stream = Stream::Null());
CV_EXPORTS Ptr<CornernessCriteria> createMinEigenValCorner(int srcType, int blockSize, int ksize, int borderType = BORDER_REFLECT101);
// obsolete
__OPENCV_GPUIMGPROC_DEPR_BEFORE__ void cornerHarris(InputArray src, OutputArray dst,
int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101,
Stream& stream = Stream::Null()) __OPENCV_GPUIMGPROC_DEPR_AFTER__;
inline void cornerHarris(InputArray src, OutputArray dst, int blockSize, int ksize, double k, int borderType, Stream& stream)
{
gpu::createHarrisCorner(src.type(), blockSize, ksize, k, borderType)->compute(src, dst, stream);
}
__OPENCV_GPUIMGPROC_DEPR_BEFORE__ void cornerMinEigenVal(InputArray src, OutputArray dst,
int blockSize, int ksize, int borderType = BORDER_REFLECT101,
Stream& stream = Stream::Null()) __OPENCV_GPUIMGPROC_DEPR_AFTER__;
inline void cornerMinEigenVal(InputArray src, OutputArray dst, int blockSize, int ksize, int borderType, Stream& stream)
{
gpu::createMinEigenValCorner(src.type(), blockSize, ksize, borderType)->compute(src, dst, stream);
}
////////////////////////// Feature Detection ///////////////////////////

@ -75,11 +75,10 @@ PERF_TEST_P(Image_Type_Border_BlockSz_ApertureSz, CornerHarris,
{
const cv::gpu::GpuMat d_img(img);
cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_Dx;
cv::gpu::GpuMat d_Dy;
cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::cornerHarris(d_img, dst, d_Dx, d_Dy, d_buf, blockSize, apertureSize, k, borderMode);
cv::Ptr<cv::gpu::CornernessCriteria> harris = cv::gpu::createHarrisCorner(img.type(), blockSize, apertureSize, k, borderMode);
TEST_CYCLE() harris->compute(d_img, dst);
GPU_SANITY_CHECK(dst, 1e-4);
}
@ -118,11 +117,10 @@ PERF_TEST_P(Image_Type_Border_BlockSz_ApertureSz, CornerMinEigenVal,
{
const cv::gpu::GpuMat d_img(img);
cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_Dx;
cv::gpu::GpuMat d_Dy;
cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::cornerMinEigenVal(d_img, dst, d_Dx, d_Dy, d_buf, blockSize, apertureSize, borderMode);
cv::Ptr<cv::gpu::CornernessCriteria> minEigenVal = cv::gpu::createMinEigenValCorner(img.type(), blockSize, apertureSize, borderMode);
TEST_CYCLE() minEigenVal->compute(d_img, dst);
GPU_SANITY_CHECK(dst, 1e-4);
}

@ -47,13 +47,8 @@ using namespace cv::gpu;
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
void cv::gpu::cornerHarris(const GpuMat&, GpuMat&, int, int, double, int) { throw_no_cuda(); }
void cv::gpu::cornerHarris(const GpuMat&, GpuMat&, GpuMat&, GpuMat&, int, int, double, int) { throw_no_cuda(); }
void cv::gpu::cornerHarris(const GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, int, int, double, int, Stream&) { throw_no_cuda(); }
void cv::gpu::cornerMinEigenVal(const GpuMat&, GpuMat&, int, int, int) { throw_no_cuda(); }
void cv::gpu::cornerMinEigenVal(const GpuMat&, GpuMat&, GpuMat&, GpuMat&, int, int, int) { throw_no_cuda(); }
void cv::gpu::cornerMinEigenVal(const GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, int, int, int, Stream&) { throw_no_cuda(); }
Ptr<gpu::CornernessCriteria> cv::gpu::createHarrisCorner(int, int, int, double, int) { throw_no_cuda(); return Ptr<gpu::CornernessCriteria>(); }
Ptr<gpu::CornernessCriteria> cv::gpu::createMinEigenValCorner(int, int, int, int) { throw_no_cuda(); return Ptr<gpu::CornernessCriteria>(); }
#else /* !defined (HAVE_CUDA) */
@ -68,89 +63,127 @@ namespace cv { namespace gpu { namespace cudev
namespace
{
void extractCovData(const GpuMat& src, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize, int borderType, Stream& stream)
class CornerBase : public CornernessCriteria
{
protected:
CornerBase(int srcType, int blockSize, int ksize, int borderType);
void extractCovData(const GpuMat& src, Stream& stream);
int srcType_;
int blockSize_;
int ksize_;
int borderType_;
GpuMat Dx_, Dy_;
private:
Ptr<gpu::Filter> filterDx_, filterDy_;
};
CornerBase::CornerBase(int srcType, int blockSize, int ksize, int borderType) :
srcType_(srcType), blockSize_(blockSize), ksize_(ksize), borderType_(borderType)
{
(void) buf;
CV_Assert( borderType_ == BORDER_REFLECT101 || borderType_ == BORDER_REPLICATE || borderType_ == BORDER_REFLECT );
double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
const int sdepth = CV_MAT_DEPTH(srcType_);
const int cn = CV_MAT_CN(srcType_);
if (ksize < 0)
CV_Assert( cn == 1 );
double scale = static_cast<double>(1 << ((ksize_ > 0 ? ksize_ : 3) - 1)) * blockSize_;
if (ksize_ < 0)
scale *= 2.;
if (src.depth() == CV_8U)
if (sdepth == CV_8U)
scale *= 255.;
scale = 1./scale;
Dx.create(src.size(), CV_32F);
Dy.create(src.size(), CV_32F);
Ptr<gpu::Filter> filterDx, filterDy;
if (ksize > 0)
if (ksize_ > 0)
{
filterDx = gpu::createSobelFilter(src.type(), CV_32F, 1, 0, ksize, scale, borderType);
filterDy = gpu::createSobelFilter(src.type(), CV_32F, 0, 1, ksize, scale, borderType);
filterDx_ = gpu::createSobelFilter(srcType, CV_32F, 1, 0, ksize_, scale, borderType_);
filterDy_ = gpu::createSobelFilter(srcType, CV_32F, 0, 1, ksize_, scale, borderType_);
}
else
{
filterDx = gpu::createScharrFilter(src.type(), CV_32F, 1, 0, scale, borderType);
filterDy = gpu::createScharrFilter(src.type(), CV_32F, 0, 1, scale, borderType);
filterDx_ = gpu::createScharrFilter(srcType, CV_32F, 1, 0, scale, borderType_);
filterDy_ = gpu::createScharrFilter(srcType, CV_32F, 0, 1, scale, borderType_);
}
}
filterDx->apply(src, Dx);
filterDy->apply(src, Dy);
void CornerBase::extractCovData(const GpuMat& src, Stream& stream)
{
CV_Assert( src.type() == srcType_ );
filterDx_->apply(src, Dx_, stream);
filterDy_->apply(src, Dy_, stream);
}
}
void cv::gpu::cornerHarris(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, double k, int borderType)
{
GpuMat Dx, Dy;
cornerHarris(src, dst, Dx, Dy, blockSize, ksize, k, borderType);
}
class Harris : public CornerBase
{
public:
Harris(int srcType, int blockSize, int ksize, double k, int borderType) :
CornerBase(srcType, blockSize, ksize, borderType), k_(static_cast<float>(k))
{
}
void cv::gpu::cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, double k, int borderType)
{
GpuMat buf;
cornerHarris(src, dst, Dx, Dy, buf, blockSize, ksize, k, borderType);
}
void compute(InputArray src, OutputArray dst, Stream& stream = Stream::Null());
void cv::gpu::cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize, double k, int borderType, Stream& stream)
{
using namespace cv::gpu::cudev::imgproc;
private:
float k_;
};
CV_Assert(borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
void Harris::compute(InputArray _src, OutputArray _dst, Stream& stream)
{
using namespace cv::gpu::cudev::imgproc;
extractCovData(src, Dx, Dy, buf, blockSize, ksize, borderType, stream);
GpuMat src = _src.getGpuMat();
dst.create(src.size(), CV_32F);
extractCovData(src, stream);
cornerHarris_gpu(blockSize, static_cast<float>(k), Dx, Dy, dst, borderType, StreamAccessor::getStream(stream));
}
_dst.create(src.size(), CV_32FC1);
GpuMat dst = _dst.getGpuMat();
void cv::gpu::cornerMinEigenVal(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, int borderType)
{
GpuMat Dx, Dy;
cornerMinEigenVal(src, dst, Dx, Dy, blockSize, ksize, borderType);
}
cornerHarris_gpu(blockSize_, k_, Dx_, Dy_, dst, borderType_, StreamAccessor::getStream(stream));
}
void cv::gpu::cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, int borderType)
{
GpuMat buf;
cornerMinEigenVal(src, dst, Dx, Dy, buf, blockSize, ksize, borderType);
}
class MinEigenVal : public CornerBase
{
public:
MinEigenVal(int srcType, int blockSize, int ksize, int borderType) :
CornerBase(srcType, blockSize, ksize, borderType)
{
}
void cv::gpu::cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize, int borderType, Stream& stream)
{
using namespace ::cv::gpu::cudev::imgproc;
void compute(InputArray src, OutputArray dst, Stream& stream = Stream::Null());
CV_Assert(borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
private:
float k_;
};
extractCovData(src, Dx, Dy, buf, blockSize, ksize, borderType, stream);
void MinEigenVal::compute(InputArray _src, OutputArray _dst, Stream& stream)
{
using namespace cv::gpu::cudev::imgproc;
GpuMat src = _src.getGpuMat();
extractCovData(src, stream);
_dst.create(src.size(), CV_32FC1);
GpuMat dst = _dst.getGpuMat();
cornerMinEigenVal_gpu(blockSize_, Dx_, Dy_, dst, borderType_, StreamAccessor::getStream(stream));
}
}
dst.create(src.size(), CV_32F);
Ptr<gpu::CornernessCriteria> cv::gpu::createHarrisCorner(int srcType, int blockSize, int ksize, double k, int borderType)
{
return new Harris(srcType, blockSize, ksize, k, borderType);
}
cornerMinEigenVal_gpu(blockSize, Dx, Dy, dst, borderType, StreamAccessor::getStream(stream));
Ptr<gpu::CornernessCriteria> cv::gpu::createMinEigenValCorner(int srcType, int blockSize, int ksize, int borderType)
{
return new MinEigenVal(srcType, blockSize, ksize, borderType);
}
#endif /* !defined (HAVE_CUDA) */

@ -75,10 +75,12 @@ void cv::gpu::GoodFeaturesToTrackDetector_GPU::operator ()(const GpuMat& image,
ensureSizeIsEnough(image.size(), CV_32F, eig_);
if (useHarrisDetector)
cornerHarris(image, eig_, Dx_, Dy_, buf_, blockSize, 3, harrisK);
else
cornerMinEigenVal(image, eig_, Dx_, Dy_, buf_, blockSize, 3);
Ptr<gpu::CornernessCriteria> cornerCriteria =
useHarrisDetector ?
gpu::createHarrisCorner(image.type(), blockSize, 3, harrisK) :
gpu::createMinEigenValCorner(image.type(), blockSize, 3);
cornerCriteria->compute(image, eig_);
double maxVal = 0;
gpu::minMax(eig_, 0, &maxVal, GpuMat(), minMaxbuf_);

@ -82,8 +82,10 @@ GPU_TEST_P(CornerHarris, Accuracy)
double k = randomDouble(0.1, 0.9);
cv::Ptr<cv::gpu::CornernessCriteria> harris = cv::gpu::createHarrisCorner(src.type(), blockSize, apertureSize, k, borderType);
cv::gpu::GpuMat dst;
cv::gpu::cornerHarris(loadMat(src), dst, blockSize, apertureSize, k, borderType);
harris->compute(loadMat(src), dst);
cv::Mat dst_gold;
cv::cornerHarris(src, dst_gold, blockSize, apertureSize, k, borderType);
@ -126,8 +128,10 @@ GPU_TEST_P(CornerMinEigen, Accuracy)
cv::Mat src = readImageType("stereobm/aloe-L.png", type);
ASSERT_FALSE(src.empty());
cv::Ptr<cv::gpu::CornernessCriteria> minEigenVal = cv::gpu::createMinEigenValCorner(src.type(), blockSize, apertureSize, borderType);
cv::gpu::GpuMat dst;
cv::gpu::cornerMinEigenVal(loadMat(src), dst, blockSize, apertureSize, borderType);
minEigenVal->compute(loadMat(src), dst);
cv::Mat dst_gold;
cv::cornerMinEigenVal(src, dst_gold, blockSize, apertureSize, borderType);

@ -176,10 +176,12 @@ TEST(cornerHarris)
d_src.upload(src);
gpu::cornerHarris(d_src, d_dst, 5, 7, 0.1, BORDER_REFLECT101);
Ptr<gpu::CornernessCriteria> harris = gpu::createHarrisCorner(src.type(), 5, 7, 0.1, BORDER_REFLECT101);
harris->compute(d_src, d_dst);
GPU_ON;
gpu::cornerHarris(d_src, d_dst, 5, 7, 0.1, BORDER_REFLECT101);
harris->compute(d_src, d_dst);
GPU_OFF;
}
}

Loading…
Cancel
Save