Fix in Canny when Sobel apertureSize is 7 (#10743)

* Fixing a bug in Canny implemetation when Sobel aperture size is 7.

* Fixing the bug in Canny accross variants and in test_canny.cpp

* Replacing a tab with white space
pull/10766/head
Harshal Nishar 7 years ago committed by Vadim Pisarevsky
parent 83761d5f8e
commit 384fa95680
  1. 31
      modules/imgproc/src/canny.cpp
  2. 10
      modules/imgproc/test/test_canny.cpp

@ -159,6 +159,12 @@ static bool ocl_Canny(InputArray _src, const UMat& dx_, const UMat& dy_, OutputA
lSizeY = 1;
}
if (aperture_size == 7)
{
low_thresh = low_thresh / 16.0f;
high_thresh = high_thresh / 16.0f;
}
if (L2gradient)
{
low_thresh = std::min(32767.0f, low_thresh);
@ -212,11 +218,17 @@ static bool ocl_Canny(InputArray _src, const UMat& dx_, const UMat& dy_, OutputA
Non maxima suppression
Double thresholding
*/
double scale = 1.0;
if (aperture_size == 7)
{
scale = 1 / 16.0;
}
UMat dx, dy;
if (!useCustomDeriv)
{
Sobel(_src, dx, CV_16S, 1, 0, aperture_size, 1, 0, BORDER_REPLICATE);
Sobel(_src, dy, CV_16S, 0, 1, aperture_size, 1, 0, BORDER_REPLICATE);
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);
}
else
{
@ -355,12 +367,17 @@ public:
int *_mag_p, *_mag_a, *_mag_n;
short *_dx, *_dy, *_dx_a = NULL, *_dy_a = NULL, *_dx_n = NULL, *_dy_n = NULL;
uchar *_pmap;
double scale = 1.0;
CV_TRACE_REGION("gradient")
if(needGradient)
{
Sobel(src.rowRange(rowStart, rowEnd), dx, CV_16S, 1, 0, aperture_size, 1, 0, BORDER_REPLICATE);
Sobel(src.rowRange(rowStart, rowEnd), dy, CV_16S, 0, 1, aperture_size, 1, 0, BORDER_REPLICATE);
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);
}
else
{
@ -946,6 +963,12 @@ void Canny( InputArray _src, OutputArray _dst,
if ((aperture_size & 1) == 0 || (aperture_size != -1 && (aperture_size < 3 || aperture_size > 7)))
CV_Error(CV_StsBadFlag, "Aperture size should be odd between 3 and 7");
if (aperture_size == 7)
{
low_thresh = low_thresh / 16.0;
high_thresh = high_thresh / 16.0;
}
if (low_thresh > high_thresh)
std::swap(low_thresh, high_thresh);

@ -211,15 +211,15 @@ test_Canny( const Mat& src, Mat& dst,
Mat dxkernel = cvtest::calcSobelKernel2D( 1, 0, m, 0 );
Mat dykernel = cvtest::calcSobelKernel2D( 0, 1, m, 0 );
Mat dx, dy, mag(height, width, CV_32F);
cvtest::filter2D(src, dx, CV_16S, dxkernel, anchor, 0, BORDER_REPLICATE);
cvtest::filter2D(src, dy, CV_16S, dykernel, anchor, 0, BORDER_REPLICATE);
cvtest::filter2D(src, dx, CV_32S, dxkernel, anchor, 0, BORDER_REPLICATE);
cvtest::filter2D(src, dy, CV_32S, dykernel, anchor, 0, BORDER_REPLICATE);
// calc gradient magnitude
for( y = 0; y < height; y++ )
{
for( x = 0; x < width; x++ )
{
int dxval = dx.at<short>(y, x), dyval = dy.at<short>(y, x);
int dxval = dx.at<int>(y, x), dyval = dy.at<int>(y, x);
mag.at<float>(y, x) = use_true_gradient ?
(float)sqrt((double)(dxval*dxval + dyval*dyval)) :
(float)(fabs((double)dxval) + fabs((double)dyval));
@ -238,8 +238,8 @@ test_Canny( const Mat& src, Mat& dst,
if( a <= lowThreshold )
continue;
int dxval = dx.at<short>(y, x);
int dyval = dy.at<short>(y, x);
int dxval = dx.at<int>(y, x);
int dyval = dy.at<int>(y, x);
double tg = dxval ? (double)dyval/dxval : DBL_MAX*CV_SIGN(dyval);

Loading…
Cancel
Save