2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
2015-01-12 15:59:30 +08:00
|
|
|
// Copyright (C) 2014, Itseez Inc., all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_imgproc.hpp"
|
2016-09-30 23:22:24 +08:00
|
|
|
#include "opencv2/core/hal/intrin.hpp"
|
2017-05-24 21:20:25 +08:00
|
|
|
#include <deque>
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-11-25 17:35:55 +08:00
|
|
|
#include "opencv2/core/openvx/ovx_defs.hpp"
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
#define CV_MALLOC_SIMD128 16
|
|
|
|
#endif
|
|
|
|
|
2013-08-28 21:00:26 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2016-07-11 21:08:09 +08:00
|
|
|
|
2015-09-25 22:13:11 +08:00
|
|
|
#ifdef HAVE_IPP
|
2017-04-21 19:52:45 +08:00
|
|
|
static bool ipp_Canny(const Mat& src , const Mat& dx_, const Mat& dy_, Mat& dst, float low, float high, bool L2gradient, int aperture_size)
|
2013-08-28 21:00:26 +08:00
|
|
|
{
|
2017-04-21 19:52:45 +08:00
|
|
|
#ifdef HAVE_IPP_IW
|
2016-08-18 14:53:00 +08:00
|
|
|
CV_INSTRUMENT_REGION_IPP()
|
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
#if IPP_DISABLE_PERF_CANNY_MT
|
|
|
|
if(cv::getNumThreads()>1)
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-09-07 23:30:17 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
::ipp::IwiSize size(dst.cols, dst.rows);
|
|
|
|
IppDataType type = ippiGetDataType(dst.depth());
|
|
|
|
int channels = dst.channels();
|
|
|
|
IppNormType norm = (L2gradient)?ippNormL2:ippNormL1;
|
2013-08-28 21:00:26 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if(size.width <= 3 || size.height <= 3)
|
2014-04-16 23:23:44 +08:00
|
|
|
return false;
|
2016-07-11 21:34:21 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if(channels != 1)
|
|
|
|
return false;
|
2013-08-28 21:00:26 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if(type != ipp8u)
|
|
|
|
return false;
|
2013-08-28 21:00:26 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if(src.empty())
|
2016-07-11 21:34:21 +08:00
|
|
|
{
|
2017-04-21 19:52:45 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
::ipp::IwiImage iwSrcDx;
|
|
|
|
::ipp::IwiImage iwSrcDy;
|
|
|
|
::ipp::IwiImage iwDst;
|
2013-08-28 21:00:26 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
ippiGetImage(dx_, iwSrcDx);
|
|
|
|
ippiGetImage(dy_, iwSrcDy);
|
|
|
|
ippiGetImage(dst, iwDst);
|
2013-08-28 21:00:26 +08:00
|
|
|
|
2017-08-17 19:57:58 +08:00
|
|
|
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterCannyDeriv, iwSrcDx, iwSrcDy, iwDst, low, high, ::ipp::IwiFilterCannyDerivParams(norm));
|
2017-04-21 19:52:45 +08:00
|
|
|
}
|
|
|
|
catch (::ipp::IwException ex)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-11 21:34:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-21 19:52:45 +08:00
|
|
|
IppiMaskSize kernel;
|
|
|
|
|
|
|
|
if(aperture_size == 3)
|
|
|
|
kernel = ippMskSize3x3;
|
|
|
|
else if(aperture_size == 5)
|
|
|
|
kernel = ippMskSize5x5;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
::ipp::IwiImage iwSrc;
|
|
|
|
::ipp::IwiImage iwDst;
|
|
|
|
|
|
|
|
ippiGetImage(src, iwSrc);
|
|
|
|
ippiGetImage(dst, iwDst);
|
|
|
|
|
2017-08-17 19:57:58 +08:00
|
|
|
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterCanny, iwSrc, iwDst, low, high, ::ipp::IwiFilterCannyParams(ippFilterSobel, kernel, norm), ippBorderRepl);
|
2017-04-21 19:52:45 +08:00
|
|
|
}
|
|
|
|
catch (::ipp::IwException)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-11 21:34:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-28 21:00:26 +08:00
|
|
|
return true;
|
2015-09-25 22:13:11 +08:00
|
|
|
#else
|
2017-04-21 19:52:45 +08:00
|
|
|
CV_UNUSED(src); CV_UNUSED(dx_); CV_UNUSED(dy_); CV_UNUSED(dst); CV_UNUSED(low); CV_UNUSED(high); CV_UNUSED(L2gradient); CV_UNUSED(aperture_size);
|
2015-09-25 22:13:11 +08:00
|
|
|
return false;
|
|
|
|
#endif
|
2013-08-28 21:00:26 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2016-07-11 21:34:21 +08:00
|
|
|
template <bool useCustomDeriv>
|
|
|
|
static bool ocl_Canny(InputArray _src, const UMat& dx_, const UMat& dy_, OutputArray _dst, float low_thresh, float high_thresh,
|
2014-01-14 18:10:24 +08:00
|
|
|
int aperture_size, bool L2gradient, int cn, const Size & size)
|
|
|
|
{
|
2016-11-07 17:15:51 +08:00
|
|
|
CV_INSTRUMENT_REGION_OPENCL()
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
UMat map;
|
|
|
|
|
|
|
|
const ocl::Device &dev = ocl::Device::getDefault();
|
|
|
|
int max_wg_size = (int)dev.maxWorkGroupSize();
|
|
|
|
|
|
|
|
int lSizeX = 32;
|
|
|
|
int lSizeY = max_wg_size / 32;
|
|
|
|
|
|
|
|
if (lSizeY == 0)
|
|
|
|
{
|
|
|
|
lSizeX = 16;
|
|
|
|
lSizeY = max_wg_size / 16;
|
|
|
|
}
|
|
|
|
if (lSizeY == 0)
|
|
|
|
{
|
|
|
|
lSizeY = 1;
|
|
|
|
}
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2018-02-02 22:00:11 +08:00
|
|
|
if (aperture_size == 7)
|
|
|
|
{
|
|
|
|
low_thresh = low_thresh / 16.0f;
|
|
|
|
high_thresh = high_thresh / 16.0f;
|
|
|
|
}
|
|
|
|
|
2014-01-14 18:10:24 +08:00
|
|
|
if (L2gradient)
|
|
|
|
{
|
|
|
|
low_thresh = std::min(32767.0f, low_thresh);
|
|
|
|
high_thresh = std::min(32767.0f, high_thresh);
|
|
|
|
|
2014-03-19 23:56:40 +08:00
|
|
|
if (low_thresh > 0)
|
|
|
|
low_thresh *= low_thresh;
|
|
|
|
if (high_thresh > 0)
|
|
|
|
high_thresh *= high_thresh;
|
2014-01-14 18:10:24 +08:00
|
|
|
}
|
|
|
|
int low = cvFloor(low_thresh), high = cvFloor(high_thresh);
|
|
|
|
|
2016-07-11 21:34:21 +08:00
|
|
|
if (!useCustomDeriv &&
|
|
|
|
aperture_size == 3 && !_src.isSubmatrix())
|
2014-01-14 18:10:24 +08:00
|
|
|
{
|
2014-07-15 21:04:50 +08:00
|
|
|
/*
|
|
|
|
stage1_with_sobel:
|
|
|
|
Sobel operator
|
|
|
|
Calc magnitudes
|
|
|
|
Non maxima suppression
|
|
|
|
Double thresholding
|
|
|
|
*/
|
|
|
|
char cvt[40];
|
|
|
|
ocl::Kernel with_sobel("stage1_with_sobel", ocl::imgproc::canny_oclsrc,
|
2014-11-06 18:07:33 +08:00
|
|
|
format("-D WITH_SOBEL -D cn=%d -D TYPE=%s -D convert_floatN=%s -D floatN=%s -D GRP_SIZEX=%d -D GRP_SIZEY=%d%s",
|
2014-07-15 21:04:50 +08:00
|
|
|
cn, ocl::memopTypeToStr(_src.depth()),
|
2014-11-06 18:07:33 +08:00
|
|
|
ocl::convertTypeStr(_src.depth(), CV_32F, cn, cvt),
|
|
|
|
ocl::typeToStr(CV_MAKE_TYPE(CV_32F, cn)),
|
2014-07-15 21:04:50 +08:00
|
|
|
lSizeX, lSizeY,
|
|
|
|
L2gradient ? " -D L2GRAD" : ""));
|
|
|
|
if (with_sobel.empty())
|
2014-01-14 18:10:24 +08:00
|
|
|
return false;
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
UMat src = _src.getUMat();
|
|
|
|
map.create(size, CV_32S);
|
|
|
|
with_sobel.args(ocl::KernelArg::ReadOnly(src),
|
|
|
|
ocl::KernelArg::WriteOnlyNoSize(map),
|
2014-11-06 18:07:33 +08:00
|
|
|
(float) low, (float) high);
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = { (size_t)size.width, (size_t)size.height },
|
|
|
|
localsize[2] = { (size_t)lSizeX, (size_t)lSizeY };
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
if (!with_sobel.run(2, globalsize, localsize, false))
|
2014-01-14 18:10:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-15 21:04:50 +08:00
|
|
|
/*
|
|
|
|
stage1_without_sobel:
|
|
|
|
Calc magnitudes
|
|
|
|
Non maxima suppression
|
|
|
|
Double thresholding
|
|
|
|
*/
|
2018-02-02 22:00:11 +08:00
|
|
|
double scale = 1.0;
|
|
|
|
if (aperture_size == 7)
|
|
|
|
{
|
|
|
|
scale = 1 / 16.0;
|
|
|
|
}
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
UMat dx, dy;
|
2016-07-11 21:34:21 +08:00
|
|
|
if (!useCustomDeriv)
|
|
|
|
{
|
2018-02-02 22:00:11 +08:00
|
|
|
Sobel(_src, dx, CV_16S, 1, 0, aperture_size, scale, 0, BORDER_REPLICATE);
|
|
|
|
Sobel(_src, dy, CV_16S, 0, 1, aperture_size, scale, 0, BORDER_REPLICATE);
|
2016-07-11 21:34:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dx = dx_;
|
|
|
|
dy = dy_;
|
|
|
|
}
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
ocl::Kernel without_sobel("stage1_without_sobel", ocl::imgproc::canny_oclsrc,
|
|
|
|
format("-D WITHOUT_SOBEL -D cn=%d -D GRP_SIZEX=%d -D GRP_SIZEY=%d%s",
|
|
|
|
cn, lSizeX, lSizeY, L2gradient ? " -D L2GRAD" : ""));
|
|
|
|
if (without_sobel.empty())
|
2014-01-14 18:10:24 +08:00
|
|
|
return false;
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
map.create(size, CV_32S);
|
|
|
|
without_sobel.args(ocl::KernelArg::ReadOnlyNoSize(dx), ocl::KernelArg::ReadOnlyNoSize(dy),
|
|
|
|
ocl::KernelArg::WriteOnly(map),
|
|
|
|
low, high);
|
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = { (size_t)size.width, (size_t)size.height },
|
|
|
|
localsize[2] = { (size_t)lSizeX, (size_t)lSizeY };
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
if (!without_sobel.run(2, globalsize, localsize, false))
|
2014-01-14 18:10:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
int PIX_PER_WI = 8;
|
|
|
|
/*
|
|
|
|
stage2:
|
|
|
|
hysteresis (add weak edges if they are connected with strong edges)
|
|
|
|
*/
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2014-09-02 21:25:25 +08:00
|
|
|
int sizey = lSizeY / PIX_PER_WI;
|
|
|
|
if (sizey == 0)
|
|
|
|
sizey = 1;
|
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = { (size_t)size.width, ((size_t)size.height + PIX_PER_WI - 1) / PIX_PER_WI }, localsize[2] = { (size_t)lSizeX, (size_t)sizey };
|
2014-09-02 21:25:25 +08:00
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
ocl::Kernel edgesHysteresis("stage2_hysteresis", ocl::imgproc::canny_oclsrc,
|
2014-09-02 21:25:25 +08:00
|
|
|
format("-D STAGE2 -D PIX_PER_WI=%d -D LOCAL_X=%d -D LOCAL_Y=%d",
|
|
|
|
PIX_PER_WI, lSizeX, sizey));
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
if (edgesHysteresis.empty())
|
2014-01-14 18:10:24 +08:00
|
|
|
return false;
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
edgesHysteresis.args(ocl::KernelArg::ReadWrite(map));
|
|
|
|
if (!edgesHysteresis.run(2, globalsize, localsize, false))
|
|
|
|
return false;
|
2014-01-14 18:10:24 +08:00
|
|
|
|
|
|
|
// get edges
|
2014-07-15 21:04:50 +08:00
|
|
|
|
|
|
|
ocl::Kernel getEdgesKernel("getEdges", ocl::imgproc::canny_oclsrc,
|
|
|
|
format("-D GET_EDGES -D PIX_PER_WI=%d", PIX_PER_WI));
|
2014-01-14 18:10:24 +08:00
|
|
|
if (getEdgesKernel.empty())
|
|
|
|
return false;
|
|
|
|
|
2014-03-19 23:56:40 +08:00
|
|
|
_dst.create(size, CV_8UC1);
|
2014-01-14 18:10:24 +08:00
|
|
|
UMat dst = _dst.getUMat();
|
|
|
|
|
2014-07-15 21:04:50 +08:00
|
|
|
getEdgesKernel.args(ocl::KernelArg::ReadOnly(map), ocl::KernelArg::WriteOnlyNoSize(dst));
|
2014-03-19 23:56:40 +08:00
|
|
|
|
2014-01-14 18:10:24 +08:00
|
|
|
return getEdgesKernel.run(2, globalsize, NULL, false);
|
|
|
|
}
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
#define CANNY_PUSH(map, stack) *map = 2, stack.push_back(map)
|
|
|
|
|
|
|
|
#define CANNY_CHECK_SIMD(m, high, map, stack) \
|
|
|
|
if (m > high) \
|
|
|
|
CANNY_PUSH(map, stack); \
|
|
|
|
else \
|
|
|
|
*map = 0
|
|
|
|
|
|
|
|
#define CANNY_CHECK(m, high, map, stack) \
|
|
|
|
if (m > high) \
|
|
|
|
CANNY_PUSH(map, stack); \
|
|
|
|
else \
|
|
|
|
*map = 0; \
|
|
|
|
continue
|
|
|
|
|
2016-08-05 22:20:56 +08:00
|
|
|
class parallelCanny : public ParallelLoopBody
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
|
|
|
public:
|
2017-05-24 21:20:25 +08:00
|
|
|
parallelCanny(const Mat &_src, Mat &_map, std::deque<uchar*> &borderPeaksParallel,
|
|
|
|
int _low, int _high, int _aperture_size, bool _L2gradient) :
|
|
|
|
src(_src), src2(_src), map(_map), _borderPeaksParallel(borderPeaksParallel),
|
|
|
|
low(_low), high(_high), aperture_size(_aperture_size), L2gradient(_L2gradient)
|
2016-08-05 22:20:56 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
haveSIMD = hasSIMD128();
|
|
|
|
if(haveSIMD)
|
|
|
|
_map.create(src.rows + 2, (int)alignSize((size_t)(src.cols + CV_MALLOC_SIMD128 + 1), CV_MALLOC_SIMD128), CV_8UC1);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
_map.create(src.rows + 2, src.cols + 2, CV_8UC1);
|
|
|
|
map = _map;
|
|
|
|
map.row(0).setTo(1);
|
|
|
|
map.row(src.rows + 1).setTo(1);
|
|
|
|
mapstep = map.cols;
|
|
|
|
needGradient = true;
|
|
|
|
cn = src.channels();
|
2016-08-05 22:20:56 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
parallelCanny(const Mat &_dx, const Mat &_dy, Mat &_map, std::deque<uchar*> &borderPeaksParallel,
|
|
|
|
int _low, int _high, bool _L2gradient) :
|
|
|
|
src(_dx), src2(_dy), map(_map), _borderPeaksParallel(borderPeaksParallel),
|
|
|
|
low(_low), high(_high), aperture_size(0), L2gradient(_L2gradient)
|
2016-08-05 22:20:56 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
haveSIMD = hasSIMD128();
|
|
|
|
if(haveSIMD)
|
|
|
|
_map.create(src.rows + 2, (int)alignSize((size_t)(src.cols + CV_MALLOC_SIMD128 + 1), CV_MALLOC_SIMD128), CV_8UC1);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
_map.create(src.rows + 2, src.cols + 2, CV_8UC1);
|
|
|
|
map = _map;
|
|
|
|
map.row(0).setTo(1);
|
|
|
|
map.row(src.rows + 1).setTo(1);
|
|
|
|
mapstep = map.cols;
|
|
|
|
needGradient = false;
|
|
|
|
cn = src.channels();
|
2016-08-05 22:20:56 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
~parallelCanny() {}
|
|
|
|
|
2016-08-05 22:20:56 +08:00
|
|
|
parallelCanny& operator=(const parallelCanny&) { return *this; }
|
|
|
|
|
|
|
|
void operator()(const Range &boundaries) const
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
|
|
|
|
2015-01-08 01:54:20 +08:00
|
|
|
Mat dx, dy;
|
2017-05-24 21:20:25 +08:00
|
|
|
AutoBuffer<short> dxMax(0), dyMax(0);
|
|
|
|
std::deque<uchar*> stack, borderPeaksLocal;
|
|
|
|
const int rowStart = max(0, boundaries.start - 1), rowEnd = min(src.rows, boundaries.end + 1);
|
|
|
|
int *_mag_p, *_mag_a, *_mag_n;
|
|
|
|
short *_dx, *_dy, *_dx_a = NULL, *_dy_a = NULL, *_dx_n = NULL, *_dy_n = NULL;
|
|
|
|
uchar *_pmap;
|
2018-02-02 22:00:11 +08:00
|
|
|
double scale = 1.0;
|
2017-05-24 21:20:25 +08:00
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION("gradient")
|
2017-05-24 21:20:25 +08:00
|
|
|
if(needGradient)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2018-02-02 22:00:11 +08:00
|
|
|
if (aperture_size == 7)
|
|
|
|
{
|
|
|
|
scale = 1 / 16.0;
|
|
|
|
}
|
|
|
|
Sobel(src.rowRange(rowStart, rowEnd), dx, CV_16S, 1, 0, aperture_size, scale, 0, BORDER_REPLICATE);
|
|
|
|
Sobel(src.rowRange(rowStart, rowEnd), dy, CV_16S, 0, 1, aperture_size, scale, 0, BORDER_REPLICATE);
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
else
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
dx = src.rowRange(rowStart, rowEnd);
|
|
|
|
dy = src2.rowRange(rowStart, rowEnd);
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION_NEXT("magnitude");
|
2017-05-24 21:20:25 +08:00
|
|
|
if(cn > 1)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
dxMax.allocate(2 * dx.cols);
|
|
|
|
dyMax.allocate(2 * dy.cols);
|
|
|
|
_dx_a = (short*)dxMax;
|
|
|
|
_dx_n = _dx_a + dx.cols;
|
|
|
|
_dy_a = (short*)dyMax;
|
|
|
|
_dy_n = _dy_a + dy.cols;
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// _mag_p: previous row, _mag_a: actual row, _mag_n: next row
|
|
|
|
#if CV_SIMD128
|
|
|
|
AutoBuffer<int> buffer(3 * (mapstep * cn + CV_MALLOC_SIMD128));
|
|
|
|
_mag_p = alignPtr((int*)buffer + 1, CV_MALLOC_SIMD128);
|
|
|
|
_mag_a = alignPtr(_mag_p + mapstep * cn, CV_MALLOC_SIMD128);
|
|
|
|
_mag_n = alignPtr(_mag_a + mapstep * cn, CV_MALLOC_SIMD128);
|
|
|
|
#else
|
|
|
|
AutoBuffer<int> buffer(3 * (mapstep * cn));
|
|
|
|
_mag_p = (int*)buffer + 1;
|
|
|
|
_mag_a = _mag_p + mapstep * cn;
|
|
|
|
_mag_n = _mag_a + mapstep * cn;
|
|
|
|
#endif
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// For the first time when just 2 rows are filled and for left and right borders
|
|
|
|
if(rowStart == boundaries.start)
|
|
|
|
memset(_mag_n - 1, 0, mapstep * sizeof(int));
|
|
|
|
else
|
|
|
|
_mag_n[src.cols] = _mag_n[-1] = 0;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
_mag_a[src.cols] = _mag_a[-1] = _mag_p[src.cols] = _mag_p[-1] = 0;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
|
|
|
// calculate magnitude and angle of gradient, perform non-maxima suppression.
|
|
|
|
// fill the map with one of the following values:
|
|
|
|
// 0 - the pixel might belong to an edge
|
|
|
|
// 1 - the pixel can not belong to an edge
|
|
|
|
// 2 - the pixel does belong to an edge
|
2017-05-24 21:20:25 +08:00
|
|
|
for (int i = rowStart; i <= boundaries.end; ++i)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
// Scroll the ring buffer
|
|
|
|
std::swap(_mag_n, _mag_a);
|
|
|
|
std::swap(_mag_n, _mag_p);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
if(i < rowEnd)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
// Next row calculation
|
|
|
|
_dx = dx.ptr<short>(i - rowStart);
|
|
|
|
_dy = dy.ptr<short>(i - rowStart);
|
|
|
|
|
|
|
|
if (L2gradient)
|
|
|
|
{
|
|
|
|
int j = 0, width = src.cols * cn;
|
2016-09-30 23:22:24 +08:00
|
|
|
#if CV_SIMD128
|
2017-05-24 21:20:25 +08:00
|
|
|
if (haveSIMD)
|
|
|
|
{
|
|
|
|
for ( ; j <= width - 8; j += 8)
|
|
|
|
{
|
|
|
|
v_int16x8 v_dx = v_load((const short*)(_dx + j));
|
|
|
|
v_int16x8 v_dy = v_load((const short*)(_dy + j));
|
|
|
|
|
|
|
|
v_int32x4 v_dxp_low, v_dxp_high;
|
|
|
|
v_int32x4 v_dyp_low, v_dyp_high;
|
|
|
|
v_expand(v_dx, v_dxp_low, v_dxp_high);
|
|
|
|
v_expand(v_dy, v_dyp_low, v_dyp_high);
|
|
|
|
|
|
|
|
v_store_aligned((int *)(_mag_n + j), v_dxp_low*v_dxp_low+v_dyp_low*v_dyp_low);
|
|
|
|
v_store_aligned((int *)(_mag_n + j + 4), v_dxp_high*v_dxp_high+v_dyp_high*v_dyp_high);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for ( ; j < width; ++j)
|
|
|
|
_mag_n[j] = int(_dx[j])*_dx[j] + int(_dy[j])*_dy[j];
|
|
|
|
}
|
|
|
|
else
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
int j = 0, width = src.cols * cn;
|
|
|
|
#if CV_SIMD128
|
|
|
|
if (haveSIMD)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
for(; j <= width - 8; j += 8)
|
|
|
|
{
|
|
|
|
v_int16x8 v_dx = v_load((const short *)(_dx + j));
|
|
|
|
v_int16x8 v_dy = v_load((const short *)(_dy + j));
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_dx = v_reinterpret_as_s16(v_abs(v_dx));
|
|
|
|
v_dy = v_reinterpret_as_s16(v_abs(v_dy));
|
2016-08-05 22:20:56 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_int32x4 v_dx_ml, v_dy_ml, v_dx_mh, v_dy_mh;
|
|
|
|
v_expand(v_dx, v_dx_ml, v_dx_mh);
|
|
|
|
v_expand(v_dy, v_dy_ml, v_dy_mh);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_store_aligned((int *)(_mag_n + j), v_dx_ml + v_dy_ml);
|
|
|
|
v_store_aligned((int *)(_mag_n + j + 4), v_dx_mh + v_dy_mh);
|
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
#endif
|
2017-05-24 21:20:25 +08:00
|
|
|
for ( ; j < width; ++j)
|
|
|
|
_mag_n[j] = std::abs(int(_dx[j])) + std::abs(int(_dy[j]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cn > 1)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
std::swap(_dx_n, _dx_a);
|
|
|
|
std::swap(_dy_n, _dy_a);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
for(int j = 0, jn = 0; j < src.cols; ++j, jn += cn)
|
|
|
|
{
|
|
|
|
int maxIdx = jn;
|
|
|
|
for(int k = 1; k < cn; ++k)
|
|
|
|
if(_mag_n[jn + k] > _mag_n[maxIdx]) maxIdx = jn + k;
|
2016-08-05 22:20:56 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
_mag_n[j] = _mag_n[maxIdx];
|
|
|
|
_dx_n[j] = _dx[maxIdx];
|
|
|
|
_dy_n[j] = _dy[maxIdx];
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
|
|
|
|
_mag_n[src.cols] = 0;
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// at the very beginning we do not have a complete ring
|
|
|
|
// buffer of 3 magnitude rows for non-maxima suppression
|
|
|
|
if (i <= boundaries.start)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
memset(_mag_n - 1, 0, mapstep * sizeof(int));
|
|
|
|
|
|
|
|
if(cn > 1)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
std::swap(_dx_n, _dx_a);
|
|
|
|
std::swap(_dy_n, _dy_a);
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// From here actual src row is (i - 1)
|
|
|
|
// Set left and right border to 1
|
|
|
|
#if CV_SIMD128
|
|
|
|
if(haveSIMD)
|
|
|
|
_pmap = map.ptr<uchar>(i) + CV_MALLOC_SIMD128;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
_pmap = map.ptr<uchar>(i) + 1;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
_pmap[src.cols] =_pmap[-1] = 1;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
if(cn == 1)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
_dx = dx.ptr<short>(i - rowStart - 1);
|
|
|
|
_dy = dy.ptr<short>(i - rowStart - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dx = _dx_a;
|
|
|
|
_dy = _dy_a;
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
const int TG22 = 13573;
|
|
|
|
int j = 0;
|
2016-09-30 23:22:24 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
if (haveSIMD)
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
const v_int32x4 v_low = v_setall_s32(low);
|
|
|
|
const v_int8x16 v_one = v_setall_s8(1);
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
for (; j <= src.cols - 32; j += 32)
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
v_int32x4 v_m1 = v_load_aligned((const int*)(_mag_a + j));
|
|
|
|
v_int32x4 v_m2 = v_load_aligned((const int*)(_mag_a + j + 4));
|
|
|
|
v_int32x4 v_m3 = v_load_aligned((const int*)(_mag_a + j + 8));
|
|
|
|
v_int32x4 v_m4 = v_load_aligned((const int*)(_mag_a + j + 12));
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2016-09-30 23:22:24 +08:00
|
|
|
v_int32x4 v_cmp1 = v_m1 > v_low;
|
|
|
|
v_int32x4 v_cmp2 = v_m2 > v_low;
|
|
|
|
v_int32x4 v_cmp3 = v_m3 > v_low;
|
|
|
|
v_int32x4 v_cmp4 = v_m4 > v_low;
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_m1 = v_load_aligned((const int*)(_mag_a + j + 16));
|
|
|
|
v_m2 = v_load_aligned((const int*)(_mag_a + j + 20));
|
|
|
|
v_m3 = v_load_aligned((const int*)(_mag_a + j + 24));
|
|
|
|
v_m4 = v_load_aligned((const int*)(_mag_a + j + 28));
|
|
|
|
|
|
|
|
v_store_aligned((signed char*)(_pmap + j), v_one);
|
|
|
|
v_store_aligned((signed char*)(_pmap + j + 16), v_one);
|
|
|
|
|
2016-09-30 23:22:24 +08:00
|
|
|
v_int16x8 v_cmp80 = v_pack(v_cmp1, v_cmp2);
|
|
|
|
v_int16x8 v_cmp81 = v_pack(v_cmp3, v_cmp4);
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_cmp1 = v_m1 > v_low;
|
|
|
|
v_cmp2 = v_m2 > v_low;
|
|
|
|
v_cmp3 = v_m3 > v_low;
|
|
|
|
v_cmp4 = v_m4 > v_low;
|
|
|
|
|
2016-09-30 23:22:24 +08:00
|
|
|
v_int8x16 v_cmp = v_pack(v_cmp80, v_cmp81);
|
2017-05-24 21:20:25 +08:00
|
|
|
|
|
|
|
v_cmp80 = v_pack(v_cmp1, v_cmp2);
|
|
|
|
v_cmp81 = v_pack(v_cmp3, v_cmp4);
|
|
|
|
|
2016-09-30 23:22:24 +08:00
|
|
|
unsigned int mask = v_signmask(v_cmp);
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_cmp = v_pack(v_cmp80, v_cmp81);
|
|
|
|
mask |= v_signmask(v_cmp) << 16;
|
|
|
|
|
2016-09-05 18:51:36 +08:00
|
|
|
if (mask)
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
int k = j;
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
do
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
int l = trailingZeros32(mask);
|
|
|
|
k += l;
|
|
|
|
mask >>= l;
|
|
|
|
|
|
|
|
int m = _mag_a[k];
|
|
|
|
short xs = _dx[k];
|
|
|
|
short ys = _dy[k];
|
|
|
|
int x = (int)std::abs(xs);
|
|
|
|
int y = (int)std::abs(ys) << 15;
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
int tg22x = x * TG22;
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
if (y < tg22x)
|
|
|
|
{
|
|
|
|
if (m > _mag_a[k - 1] && m >= _mag_a[k + 1])
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
2016-09-05 18:51:36 +08:00
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int tg67x = tg22x + (x << 16);
|
|
|
|
if (y > tg67x)
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
if (m > _mag_p[k] && m >= _mag_n[k])
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int s = (xs ^ ys) < 0 ? -1 : 1;
|
|
|
|
if(m > _mag_p[k - s] && m > _mag_n[k + s])
|
2016-09-05 18:51:36 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
2016-09-05 18:51:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
++k;
|
|
|
|
} while((mask >>= 1));
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
if (j <= src.cols - 16)
|
|
|
|
{
|
|
|
|
v_int32x4 v_m1 = v_load_aligned((const int*)(_mag_a + j));
|
|
|
|
v_int32x4 v_m2 = v_load_aligned((const int*)(_mag_a + j + 4));
|
|
|
|
v_int32x4 v_m3 = v_load_aligned((const int*)(_mag_a + j + 8));
|
|
|
|
v_int32x4 v_m4 = v_load_aligned((const int*)(_mag_a + j + 12));
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_store_aligned((signed char*)(_pmap + j), v_one);
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
v_int32x4 v_cmp1 = v_m1 > v_low;
|
|
|
|
v_int32x4 v_cmp2 = v_m2 > v_low;
|
|
|
|
v_int32x4 v_cmp3 = v_m3 > v_low;
|
|
|
|
v_int32x4 v_cmp4 = v_m4 > v_low;
|
|
|
|
|
|
|
|
v_int16x8 v_cmp80 = v_pack(v_cmp1, v_cmp2);
|
|
|
|
v_int16x8 v_cmp81 = v_pack(v_cmp3, v_cmp4);
|
|
|
|
|
|
|
|
v_int8x16 v_cmp = v_pack(v_cmp80, v_cmp81);
|
|
|
|
unsigned int mask = v_signmask(v_cmp);
|
|
|
|
|
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
int k = j;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int l = trailingZeros32(mask);
|
|
|
|
k += l;
|
|
|
|
mask >>= l;
|
|
|
|
|
|
|
|
int m = _mag_a[k];
|
|
|
|
short xs = _dx[k];
|
|
|
|
short ys = _dy[k];
|
|
|
|
int x = (int)std::abs(xs);
|
|
|
|
int y = (int)std::abs(ys) << 15;
|
2016-09-05 18:51:36 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
int tg22x = x * TG22;
|
|
|
|
|
|
|
|
if (y < tg22x)
|
|
|
|
{
|
|
|
|
if (m > _mag_a[k - 1] && m >= _mag_a[k + 1])
|
|
|
|
{
|
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int tg67x = tg22x + (x << 16);
|
|
|
|
if (y > tg67x)
|
|
|
|
{
|
|
|
|
if (m > _mag_p[k] && m >= _mag_n[k])
|
|
|
|
{
|
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int s = (xs ^ ys) < 0 ? -1 : 1;
|
|
|
|
if(m > _mag_p[k - s] && m > _mag_n[k + s])
|
|
|
|
{
|
|
|
|
CANNY_CHECK_SIMD(m, high, (_pmap+k), stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++k;
|
|
|
|
} while((mask >>= 1));
|
2016-09-05 18:51:36 +08:00
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
j += 16;
|
2016-09-05 18:51:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; j < src.cols; j++)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
int m = _mag_a[j];
|
2015-01-08 01:54:20 +08:00
|
|
|
|
|
|
|
if (m > low)
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
short xs = _dx[j];
|
|
|
|
short ys = _dy[j];
|
|
|
|
int x = (int)std::abs(xs);
|
|
|
|
int y = (int)std::abs(ys) << 15;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
|
|
|
int tg22x = x * TG22;
|
|
|
|
|
|
|
|
if (y < tg22x)
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
if (m > _mag_a[j - 1] && m >= _mag_a[j + 1])
|
|
|
|
{
|
|
|
|
CANNY_CHECK(m, high, (_pmap+j), stack);
|
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
int tg67x = tg22x + (x << 16);
|
2015-01-08 01:54:20 +08:00
|
|
|
if (y > tg67x)
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
if (m > _mag_p[j] && m >= _mag_n[j])
|
|
|
|
{
|
|
|
|
CANNY_CHECK(m, high, (_pmap+j), stack);
|
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int s = (xs ^ ys) < 0 ? -1 : 1;
|
2017-05-24 21:20:25 +08:00
|
|
|
if(m > _mag_p[j - s] && m > _mag_n[j + s])
|
|
|
|
{
|
|
|
|
CANNY_CHECK(m, high, (_pmap+j), stack);
|
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
_pmap[j] = 1;
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// Not for first row of first slice or last row of last slice
|
|
|
|
uchar *pmapLower = (rowStart == 0) ? map.data : (map.data + (boundaries.start + 2) * mapstep);
|
|
|
|
uint pmapDiff = (uint)(((rowEnd == src.rows) ? map.datalimit : (map.data + boundaries.end * mapstep)) - pmapLower);
|
|
|
|
|
2015-01-08 01:54:20 +08:00
|
|
|
// now track the edges (hysteresis thresholding)
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION_NEXT("hysteresis");
|
2017-05-24 21:20:25 +08:00
|
|
|
while (!stack.empty())
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
uchar *m = stack.back();
|
|
|
|
stack.pop_back();
|
2015-01-08 01:54:20 +08:00
|
|
|
|
|
|
|
// Stops thresholding from expanding to other slices by sending pixels in the borders of each
|
|
|
|
// slice in a queue to be serially processed later.
|
2017-05-24 21:20:25 +08:00
|
|
|
if((unsigned)(m - pmapLower) < pmapDiff)
|
2015-01-08 01:54:20 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
if (!m[-mapstep-1]) CANNY_PUSH((m-mapstep-1), stack);
|
|
|
|
if (!m[-mapstep]) CANNY_PUSH((m-mapstep), stack);
|
|
|
|
if (!m[-mapstep+1]) CANNY_PUSH((m-mapstep+1), stack);
|
|
|
|
if (!m[-1]) CANNY_PUSH((m-1), stack);
|
|
|
|
if (!m[1]) CANNY_PUSH((m+1), stack);
|
|
|
|
if (!m[mapstep-1]) CANNY_PUSH((m+mapstep-1), stack);
|
|
|
|
if (!m[mapstep]) CANNY_PUSH((m+mapstep), stack);
|
|
|
|
if (!m[mapstep+1]) CANNY_PUSH((m+mapstep+1), stack);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
borderPeaksLocal.push_back(m);
|
|
|
|
ptrdiff_t mapstep2 = m < pmapLower ? mapstep : -mapstep;
|
|
|
|
|
|
|
|
if (!m[-1]) CANNY_PUSH((m-1), stack);
|
|
|
|
if (!m[1]) CANNY_PUSH((m+1), stack);
|
|
|
|
if (!m[mapstep2-1]) CANNY_PUSH((m+mapstep2-1), stack);
|
|
|
|
if (!m[mapstep2]) CANNY_PUSH((m+mapstep2), stack);
|
|
|
|
if (!m[mapstep2+1]) CANNY_PUSH((m+mapstep2+1), stack);
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 22:20:56 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
if(!borderPeaksLocal.empty())
|
|
|
|
{
|
|
|
|
AutoLock lock(mutex);
|
|
|
|
_borderPeaksParallel.insert(_borderPeaksParallel.end(), borderPeaksLocal.begin(), borderPeaksLocal.end());
|
2016-08-05 22:20:56 +08:00
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-24 21:20:25 +08:00
|
|
|
const Mat &src, &src2;
|
|
|
|
Mat ↦
|
|
|
|
std::deque<uchar*> &_borderPeaksParallel;
|
2016-08-05 22:20:56 +08:00
|
|
|
int low, high, aperture_size;
|
2017-05-24 21:20:25 +08:00
|
|
|
bool L2gradient, needGradient;
|
|
|
|
ptrdiff_t mapstep;
|
|
|
|
int cn;
|
|
|
|
#if CV_SIMD128
|
|
|
|
bool haveSIMD;
|
|
|
|
#endif
|
2016-08-05 22:20:56 +08:00
|
|
|
mutable Mutex mutex;
|
2015-01-08 01:54:20 +08:00
|
|
|
};
|
|
|
|
|
2016-08-05 22:20:56 +08:00
|
|
|
class finalPass : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2017-05-24 21:20:25 +08:00
|
|
|
finalPass(const Mat &_map, Mat &_dst) :
|
|
|
|
map(_map), dst(_dst)
|
|
|
|
{
|
|
|
|
dst = _dst;
|
|
|
|
#if CV_SIMD128
|
|
|
|
haveSIMD = hasSIMD128();
|
|
|
|
#endif
|
|
|
|
}
|
2016-08-05 22:20:56 +08:00
|
|
|
|
|
|
|
~finalPass() {}
|
|
|
|
|
|
|
|
void operator()(const Range &boundaries) const
|
|
|
|
{
|
|
|
|
// the final pass, form the final image
|
2017-05-24 21:20:25 +08:00
|
|
|
for (int i = boundaries.start; i < boundaries.end; i++)
|
2016-08-05 22:20:56 +08:00
|
|
|
{
|
|
|
|
int j = 0;
|
2017-05-24 21:20:25 +08:00
|
|
|
uchar *pdst = dst.ptr<uchar>(i);
|
2017-11-16 02:53:32 +08:00
|
|
|
const uchar *pmap = map.ptr<uchar>(i + 1);
|
2017-05-24 21:20:25 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
if(haveSIMD)
|
2017-11-16 02:53:32 +08:00
|
|
|
pmap += CV_MALLOC_SIMD128;
|
2017-05-24 21:20:25 +08:00
|
|
|
else
|
|
|
|
#endif
|
2017-11-16 02:53:32 +08:00
|
|
|
pmap += 1;
|
2016-09-30 23:22:24 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
if(haveSIMD) {
|
2017-11-16 02:53:32 +08:00
|
|
|
const v_uint8x16 v_zero = v_setzero_u8();
|
|
|
|
const v_uint8x16 v_ff = ~v_zero;
|
|
|
|
const v_uint8x16 v_two(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
|
2016-08-05 22:20:56 +08:00
|
|
|
|
2017-11-16 02:53:32 +08:00
|
|
|
for (; j <= dst.cols - 16; j += 16)
|
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
v_uint8x16 v_pmap = v_load_aligned((const unsigned char*)(pmap + j));
|
2017-11-16 02:53:32 +08:00
|
|
|
v_pmap = v_select(v_pmap == v_two, v_ff, v_zero);
|
2016-09-30 23:22:24 +08:00
|
|
|
v_store((pdst + j), v_pmap);
|
2017-05-24 21:20:25 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 02:53:32 +08:00
|
|
|
if (j <= dst.cols - 8)
|
|
|
|
{
|
|
|
|
v_uint8x16 v_pmap = v_load_low((const unsigned char*)(pmap + j));
|
|
|
|
v_pmap = v_select(v_pmap == v_two, v_ff, v_zero);
|
2017-05-24 21:20:25 +08:00
|
|
|
v_store_low((pdst + j), v_pmap);
|
|
|
|
j += 8;
|
2016-08-05 22:20:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-05 18:51:36 +08:00
|
|
|
for (; j < dst.cols; j++)
|
2017-11-16 02:53:32 +08:00
|
|
|
{
|
2016-08-05 22:20:56 +08:00
|
|
|
pdst[j] = (uchar)-(pmap[j] >> 1);
|
2017-11-16 02:53:32 +08:00
|
|
|
}
|
2016-08-05 22:20:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-24 21:20:25 +08:00
|
|
|
const Mat ↦
|
2016-09-05 18:51:36 +08:00
|
|
|
Mat &dst;
|
2017-05-24 21:20:25 +08:00
|
|
|
#if CV_SIMD128
|
|
|
|
bool haveSIMD;
|
|
|
|
#endif
|
2017-11-16 02:53:32 +08:00
|
|
|
|
|
|
|
finalPass(const finalPass&); // = delete
|
|
|
|
finalPass& operator=(const finalPass&); // = delete
|
2016-08-05 22:20:56 +08:00
|
|
|
};
|
|
|
|
|
2016-11-25 17:35:55 +08:00
|
|
|
#ifdef HAVE_OPENVX
|
2017-05-26 00:10:21 +08:00
|
|
|
namespace ovx {
|
|
|
|
template <> inline bool skipSmallImages<VX_KERNEL_CANNY_EDGE_DETECTOR>(int w, int h) { return w*h < 640 * 480; }
|
|
|
|
}
|
2016-11-25 17:35:55 +08:00
|
|
|
static bool openvx_canny(const Mat& src, Mat& dst, int loVal, int hiVal, int kSize, bool useL2)
|
|
|
|
{
|
|
|
|
using namespace ivx;
|
|
|
|
|
2017-02-14 18:32:17 +08:00
|
|
|
Context context = ovx::getOpenVXContext();
|
2016-11-25 17:35:55 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Image _src = Image::createFromHandle(
|
|
|
|
context,
|
|
|
|
Image::matTypeToFormat(src.type()),
|
|
|
|
Image::createAddressing(src),
|
|
|
|
src.data );
|
|
|
|
Image _dst = Image::createFromHandle(
|
|
|
|
context,
|
|
|
|
Image::matTypeToFormat(dst.type()),
|
|
|
|
Image::createAddressing(dst),
|
|
|
|
dst.data );
|
|
|
|
Threshold threshold = Threshold::createRange(context, VX_TYPE_UINT8, saturate_cast<uchar>(loVal), saturate_cast<uchar>(hiVal));
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// the code below is disabled because vxuCannyEdgeDetector()
|
|
|
|
// ignores context attribute VX_CONTEXT_IMMEDIATE_BORDER
|
|
|
|
|
|
|
|
// FIXME: may fail in multithread case
|
|
|
|
border_t prevBorder = context.immediateBorder();
|
|
|
|
context.setImmediateBorder(VX_BORDER_REPLICATE);
|
|
|
|
IVX_CHECK_STATUS( vxuCannyEdgeDetector(context, _src, threshold, kSize, (useL2 ? VX_NORM_L2 : VX_NORM_L1), _dst) );
|
|
|
|
context.setImmediateBorder(prevBorder);
|
|
|
|
#else
|
|
|
|
// alternative code without vxuCannyEdgeDetector()
|
|
|
|
Graph graph = Graph::create(context);
|
|
|
|
ivx::Node node = ivx::Node(vxCannyEdgeDetectorNode(graph, _src, threshold, kSize, (useL2 ? VX_NORM_L2 : VX_NORM_L1), _dst) );
|
|
|
|
node.setBorder(VX_BORDER_REPLICATE);
|
|
|
|
graph.verify();
|
|
|
|
graph.process();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VX_VERSION_1_1
|
|
|
|
_src.swapHandle();
|
|
|
|
_dst.swapHandle();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
catch(const WrapperError& e)
|
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-11-25 17:35:55 +08:00
|
|
|
}
|
|
|
|
catch(const RuntimeError& e)
|
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-11-25 17:35:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // HAVE_OPENVX
|
|
|
|
|
2016-07-11 21:08:09 +08:00
|
|
|
void Canny( InputArray _src, OutputArray _dst,
|
2012-02-16 05:10:11 +08:00
|
|
|
double low_thresh, double high_thresh,
|
|
|
|
int aperture_size, bool L2gradient )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-08-18 14:53:00 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
CV_Assert( _src.depth() == CV_8U );
|
|
|
|
|
2014-01-14 18:10:24 +08:00
|
|
|
const Size size = _src.size();
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2017-07-01 00:32:16 +08:00
|
|
|
// we don't support inplace parameters in case with RGB/BGR src
|
|
|
|
CV_Assert((_dst.getObj() != _src.getObj() || _src.type() == CV_8UC1) && "Inplace parameters are not supported");
|
|
|
|
|
2014-01-14 18:10:24 +08:00
|
|
|
_dst.create(size, CV_8U);
|
2012-02-21 19:16:49 +08:00
|
|
|
|
|
|
|
if (!L2gradient && (aperture_size & CV_CANNY_L2_GRADIENT) == CV_CANNY_L2_GRADIENT)
|
|
|
|
{
|
2014-01-14 18:10:24 +08:00
|
|
|
// backward compatibility
|
2012-02-21 19:16:49 +08:00
|
|
|
aperture_size &= ~CV_CANNY_L2_GRADIENT;
|
|
|
|
L2gradient = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aperture_size & 1) == 0 || (aperture_size != -1 && (aperture_size < 3 || aperture_size > 7)))
|
2016-03-28 20:06:55 +08:00
|
|
|
CV_Error(CV_StsBadFlag, "Aperture size should be odd between 3 and 7");
|
2012-02-21 19:16:49 +08:00
|
|
|
|
2018-02-02 22:00:11 +08:00
|
|
|
if (aperture_size == 7)
|
|
|
|
{
|
|
|
|
low_thresh = low_thresh / 16.0;
|
|
|
|
high_thresh = high_thresh / 16.0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 21:00:26 +08:00
|
|
|
if (low_thresh > high_thresh)
|
|
|
|
std::swap(low_thresh, high_thresh);
|
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
CV_OCL_RUN(_dst.isUMat() && (_src.channels() == 1 || _src.channels() == 3),
|
|
|
|
ocl_Canny<false>(_src, UMat(), UMat(), _dst, (float)low_thresh, (float)high_thresh, aperture_size, L2gradient, _src.channels(), size))
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2017-11-30 21:56:13 +08:00
|
|
|
Mat src0 = _src.getMat(), dst = _dst.getMat();
|
|
|
|
Mat src(src0.size(), src0.type(), src0.data, src0.step);
|
|
|
|
|
|
|
|
CALL_HAL(canny, cv_hal_canny, src.data, src.step, dst.data, dst.step, src.cols, src.rows, src.channels(),
|
|
|
|
low_thresh, high_thresh, aperture_size, L2gradient);
|
2014-01-14 18:10:24 +08:00
|
|
|
|
2016-11-25 17:35:55 +08:00
|
|
|
CV_OVX_RUN(
|
2016-12-06 23:55:09 +08:00
|
|
|
false && /* disabling due to accuracy issues */
|
2016-12-02 20:59:09 +08:00
|
|
|
src.type() == CV_8UC1 &&
|
|
|
|
!src.isSubmatrix() &&
|
|
|
|
src.cols >= aperture_size &&
|
2017-03-28 23:02:42 +08:00
|
|
|
src.rows >= aperture_size &&
|
|
|
|
!ovx::skipSmallImages<VX_KERNEL_CANNY_EDGE_DETECTOR>(src.cols, src.rows),
|
2016-11-25 17:35:55 +08:00
|
|
|
openvx_canny(
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
cvFloor(low_thresh),
|
|
|
|
cvFloor(high_thresh),
|
|
|
|
aperture_size,
|
2016-12-14 22:31:41 +08:00
|
|
|
L2gradient ) )
|
2016-11-25 17:35:55 +08:00
|
|
|
|
2011-11-11 22:45:53 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-02-27 17:52:11 +08:00
|
|
|
if (tegra::useTegra() && tegra::canny(src, dst, low_thresh, high_thresh, aperture_size, L2gradient))
|
2011-11-11 22:45:53 +08:00
|
|
|
return;
|
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
CV_IPP_RUN_FAST(ipp_Canny(src, Mat(), Mat(), dst, (float)low_thresh, (float)high_thresh, L2gradient, aperture_size))
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if (L2gradient)
|
|
|
|
{
|
|
|
|
low_thresh = std::min(32767.0, low_thresh);
|
|
|
|
high_thresh = std::min(32767.0, high_thresh);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
if (low_thresh > 0) low_thresh *= low_thresh;
|
|
|
|
if (high_thresh > 0) high_thresh *= high_thresh;
|
|
|
|
}
|
|
|
|
int low = cvFloor(low_thresh);
|
|
|
|
int high = cvFloor(high_thresh);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// If Scharr filter: aperture size is 3, ksize2 is 1
|
|
|
|
int ksize2 = aperture_size < 0 ? 1 : aperture_size / 2;
|
2016-08-05 22:20:56 +08:00
|
|
|
// Minimum number of threads should be 1, maximum should not exceed number of CPU's, because of overhead
|
|
|
|
int numOfThreads = std::max(1, std::min(getNumThreads(), getNumberOfCPUs()));
|
|
|
|
// Make a fallback for pictures with too few rows.
|
|
|
|
int grainSize = src.rows / numOfThreads;
|
|
|
|
int minGrainSize = 2 * (ksize2 + 1);
|
|
|
|
if (grainSize < minGrainSize)
|
|
|
|
numOfThreads = std::max(1, src.rows / minGrainSize);
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
Mat map;
|
|
|
|
std::deque<uchar*> stack;
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
parallel_for_(Range(0, src.rows), parallelCanny(src, map, stack, low, high, aperture_size, L2gradient), numOfThreads);
|
2016-08-05 22:20:56 +08:00
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION("global_hysteresis");
|
2016-08-05 22:20:56 +08:00
|
|
|
// now track the edges (hysteresis thresholding)
|
2017-05-24 21:20:25 +08:00
|
|
|
ptrdiff_t mapstep = map.cols;
|
|
|
|
|
|
|
|
while (!stack.empty())
|
2016-08-05 22:20:56 +08:00
|
|
|
{
|
2017-05-24 21:20:25 +08:00
|
|
|
uchar* m = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
|
|
|
|
if (!m[-mapstep-1]) CANNY_PUSH((m-mapstep-1), stack);
|
|
|
|
if (!m[-mapstep]) CANNY_PUSH((m-mapstep), stack);
|
|
|
|
if (!m[-mapstep+1]) CANNY_PUSH((m-mapstep+1), stack);
|
|
|
|
if (!m[-1]) CANNY_PUSH((m-1), stack);
|
|
|
|
if (!m[1]) CANNY_PUSH((m+1), stack);
|
|
|
|
if (!m[mapstep-1]) CANNY_PUSH((m+mapstep-1), stack);
|
|
|
|
if (!m[mapstep]) CANNY_PUSH((m+mapstep), stack);
|
|
|
|
if (!m[mapstep+1]) CANNY_PUSH((m+mapstep+1), stack);
|
2017-04-21 19:52:45 +08:00
|
|
|
}
|
2015-01-08 01:54:20 +08:00
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION_NEXT("finalPass");
|
2017-05-24 21:20:25 +08:00
|
|
|
parallel_for_(Range(0, src.rows), finalPass(map, dst), src.total()/(double)(1<<16));
|
2016-07-11 21:08:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Canny( InputArray _dx, InputArray _dy, OutputArray _dst,
|
|
|
|
double low_thresh, double high_thresh,
|
|
|
|
bool L2gradient )
|
|
|
|
{
|
2017-04-21 19:52:45 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
|
|
|
|
2016-07-11 21:08:09 +08:00
|
|
|
CV_Assert(_dx.dims() == 2);
|
|
|
|
CV_Assert(_dx.type() == CV_16SC1 || _dx.type() == CV_16SC3);
|
|
|
|
CV_Assert(_dy.type() == _dx.type());
|
|
|
|
CV_Assert(_dx.sameSize(_dy));
|
|
|
|
|
|
|
|
if (low_thresh > high_thresh)
|
|
|
|
std::swap(low_thresh, high_thresh);
|
|
|
|
|
|
|
|
const Size size = _dx.size();
|
2016-07-11 21:34:21 +08:00
|
|
|
|
|
|
|
CV_OCL_RUN(_dst.isUMat(),
|
2017-05-24 21:20:25 +08:00
|
|
|
ocl_Canny<true>(UMat(), _dx.getUMat(), _dy.getUMat(), _dst, (float)low_thresh, (float)high_thresh, 0, L2gradient, _dx.channels(), size))
|
2016-07-11 21:34:21 +08:00
|
|
|
|
2016-07-11 21:08:09 +08:00
|
|
|
_dst.create(size, CV_8U);
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
|
|
|
|
Mat dx = _dx.getMat();
|
|
|
|
Mat dy = _dy.getMat();
|
2016-07-11 21:34:21 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
CV_IPP_RUN_FAST(ipp_Canny(Mat(), dx, dy, dst, (float)low_thresh, (float)high_thresh, L2gradient, 0))
|
2016-07-11 21:34:21 +08:00
|
|
|
|
2012-02-21 19:16:49 +08:00
|
|
|
if (L2gradient)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-21 19:16:49 +08:00
|
|
|
low_thresh = std::min(32767.0, low_thresh);
|
|
|
|
high_thresh = std::min(32767.0, high_thresh);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-02-21 19:16:49 +08:00
|
|
|
if (low_thresh > 0) low_thresh *= low_thresh;
|
|
|
|
if (high_thresh > 0) high_thresh *= high_thresh;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2017-05-24 21:20:25 +08:00
|
|
|
|
2012-02-21 19:16:49 +08:00
|
|
|
int low = cvFloor(low_thresh);
|
|
|
|
int high = cvFloor(high_thresh);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
std::deque<uchar*> stack;
|
|
|
|
Mat map;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
// Minimum number of threads should be 1, maximum should not exceed number of CPU's, because of overhead
|
|
|
|
int numOfThreads = std::max(1, std::min(getNumThreads(), getNumberOfCPUs()));
|
|
|
|
if (dx.rows / numOfThreads < 3)
|
|
|
|
numOfThreads = std::max(1, dx.rows / 3);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
parallel_for_(Range(0, dx.rows), parallelCanny(dx, dy, map, stack, low, high, L2gradient), numOfThreads);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION("global_hysteresis")
|
2010-05-12 01:44:00 +08:00
|
|
|
// now track the edges (hysteresis thresholding)
|
2017-05-24 21:20:25 +08:00
|
|
|
ptrdiff_t mapstep = map.cols;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-05-24 21:20:25 +08:00
|
|
|
while (!stack.empty())
|
|
|
|
{
|
|
|
|
uchar* m = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
|
|
|
|
if (!m[-mapstep-1]) CANNY_PUSH((m-mapstep-1), stack);
|
|
|
|
if (!m[-mapstep]) CANNY_PUSH((m-mapstep), stack);
|
|
|
|
if (!m[-mapstep+1]) CANNY_PUSH((m-mapstep+1), stack);
|
|
|
|
if (!m[-1]) CANNY_PUSH((m-1), stack);
|
|
|
|
if (!m[1]) CANNY_PUSH((m+1), stack);
|
|
|
|
if (!m[mapstep-1]) CANNY_PUSH((m+mapstep-1), stack);
|
|
|
|
if (!m[mapstep]) CANNY_PUSH((m+mapstep), stack);
|
|
|
|
if (!m[mapstep+1]) CANNY_PUSH((m+mapstep+1), stack);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_REGION_NEXT("finalPass");
|
2017-05-24 21:20:25 +08:00
|
|
|
parallel_for_(Range(0, dx.rows), finalPass(map, dst), dx.total()/(double)(1<<16));
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2016-07-11 21:08:09 +08:00
|
|
|
} // namespace cv
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
void cvCanny( const CvArr* image, CvArr* edges, double threshold1,
|
|
|
|
double threshold2, int aperture_size )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
cv::Mat src = cv::cvarrToMat(image), dst = cv::cvarrToMat(edges);
|
|
|
|
CV_Assert( src.size == dst.size && src.depth() == CV_8U && dst.type() == CV_8U );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
cv::Canny(src, dst, threshold1, threshold2, aperture_size & 255,
|
|
|
|
(aperture_size & CV_CANNY_L2_GRADIENT) != 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|