Commit a455858d authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #6878 from alalek:canny_custom_gradient

parents 848c66ba e20a93f7
......@@ -1664,6 +1664,19 @@ CV_EXPORTS_W void Canny( InputArray image, OutputArray edges,
double threshold1, double threshold2,
int apertureSize = 3, bool L2gradient = false );
/** \overload
Finds edges in an image using the Canny algorithm with custom image gradient.
@param dx 16-bit x derivative of input image (CV_16SC1 or CV_16SC3).
@param dy 16-bit y derivative of input image (same type as dx).
@param edges,threshold1,threshold2,L2gradient See cv::Canny
*/
CV_EXPORTS_W void Canny( InputArray dx, InputArray dy,
OutputArray edges,
double threshold1, double threshold2,
bool L2gradient = false );
/** @brief Calculates the minimal eigenvalue of gradient matrices for corner detection.
The function is similar to cornerEigenValsAndVecs but it calculates and stores only the minimal
......
This diff is collapsed.
......@@ -54,13 +54,13 @@ namespace ocl {
////////////////////////////////////////////////////////
// Canny
IMPLEMENT_PARAM_CLASS(AppertureSize, int)
IMPLEMENT_PARAM_CLASS(ApertureSize, int)
IMPLEMENT_PARAM_CLASS(L2gradient, bool)
IMPLEMENT_PARAM_CLASS(UseRoi, bool)
PARAM_TEST_CASE(Canny, Channels, AppertureSize, L2gradient, UseRoi)
PARAM_TEST_CASE(Canny, Channels, ApertureSize, L2gradient, UseRoi)
{
int cn, apperture_size;
int cn, aperture_size;
bool useL2gradient, use_roi;
TEST_DECLARE_INPUT_PARAMETER(src);
......@@ -69,7 +69,7 @@ PARAM_TEST_CASE(Canny, Channels, AppertureSize, L2gradient, UseRoi)
virtual void SetUp()
{
cn = GET_PARAM(0);
apperture_size = GET_PARAM(1);
aperture_size = GET_PARAM(1);
useL2gradient = GET_PARAM(2);
use_roi = GET_PARAM(3);
}
......@@ -105,8 +105,31 @@ OCL_TEST_P(Canny, Accuracy)
eps = 12e-3;
#endif
OCL_OFF(cv::Canny(src_roi, dst_roi, low_thresh, high_thresh, apperture_size, useL2gradient));
OCL_ON(cv::Canny(usrc_roi, udst_roi, low_thresh, high_thresh, apperture_size, useL2gradient));
OCL_OFF(cv::Canny(src_roi, dst_roi, low_thresh, high_thresh, aperture_size, useL2gradient));
OCL_ON(cv::Canny(usrc_roi, udst_roi, low_thresh, high_thresh, aperture_size, useL2gradient));
EXPECT_MAT_SIMILAR(dst_roi, udst_roi, eps);
EXPECT_MAT_SIMILAR(dst, udst, eps);
}
OCL_TEST_P(Canny, AccuracyCustomGradient)
{
generateTestData();
const double low_thresh = 50.0, high_thresh = 100.0;
double eps = 1e-2;
#ifdef ANDROID
if (cv::ocl::Device::getDefault().isNVidia())
eps = 12e-3;
#endif
OCL_OFF(cv::Canny(src_roi, dst_roi, low_thresh, high_thresh, aperture_size, useL2gradient));
OCL_ON(
UMat dx, dy;
Sobel(usrc_roi, dx, CV_16S, 1, 0, aperture_size, 1, 0, BORDER_REPLICATE);
Sobel(usrc_roi, dy, CV_16S, 0, 1, aperture_size, 1, 0, BORDER_REPLICATE);
cv::Canny(dx, dy, udst_roi, low_thresh, high_thresh, useL2gradient);
);
EXPECT_MAT_SIMILAR(dst_roi, udst_roi, eps);
EXPECT_MAT_SIMILAR(dst, udst, eps);
......@@ -114,7 +137,7 @@ OCL_TEST_P(Canny, Accuracy)
OCL_INSTANTIATE_TEST_CASE_P(ImgProc, Canny, testing::Combine(
testing::Values(1, 3),
testing::Values(AppertureSize(3), AppertureSize(5)),
testing::Values(ApertureSize(3), ApertureSize(5)),
testing::Values(L2gradient(false), L2gradient(true)),
testing::Values(UseRoi(false), UseRoi(true))));
......
......@@ -47,7 +47,7 @@ using namespace std;
class CV_CannyTest : public cvtest::ArrayTest
{
public:
CV_CannyTest();
CV_CannyTest(bool custom_deriv = false);
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
......@@ -61,10 +61,11 @@ protected:
bool use_true_gradient;
double threshold1, threshold2;
bool test_cpp;
bool test_custom_deriv;
};
CV_CannyTest::CV_CannyTest()
CV_CannyTest::CV_CannyTest(bool custom_deriv)
{
test_array[INPUT].push_back(NULL);
test_array[OUTPUT].push_back(NULL);
......@@ -75,6 +76,7 @@ CV_CannyTest::CV_CannyTest()
threshold1 = threshold2 = 0;
test_cpp = false;
test_custom_deriv = custom_deriv;
}
......@@ -99,6 +101,9 @@ void CV_CannyTest::get_test_array_types_and_sizes( int test_case_idx,
use_true_gradient = cvtest::randInt(rng) % 2 != 0;
test_cpp = (cvtest::randInt(rng) & 256) == 0;
ts->printf(cvtest::TS::LOG, "Canny(size = %d x %d, aperture_size = %d, threshold1 = %g, threshold2 = %g, L2 = %s) test_cpp = %s (test case #%d)\n",
sizes[0][0].width, sizes[0][0].height, aperture_size, threshold1, threshold2, use_true_gradient ? "TRUE" : "FALSE", test_cpp ? "TRUE" : "FALSE", test_case_idx);
}
......@@ -123,9 +128,24 @@ double CV_CannyTest::get_success_error_level( int /*test_case_idx*/, int /*i*/,
void CV_CannyTest::run_func()
{
if(!test_cpp)
if (test_custom_deriv)
{
cv::Mat _out = cv::cvarrToMat(test_array[OUTPUT][0]);
cv::Mat src = cv::cvarrToMat(test_array[INPUT][0]);
cv::Mat dx, dy;
int m = aperture_size;
Point anchor(m/2, m/2);
Mat dxkernel = cvtest::calcSobelKernel2D( 1, 0, m, 0 );
Mat dykernel = cvtest::calcSobelKernel2D( 0, 1, m, 0 );
cvtest::filter2D(src, dx, CV_16S, dxkernel, anchor, 0, BORDER_REPLICATE);
cvtest::filter2D(src, dy, CV_16S, dykernel, anchor, 0, BORDER_REPLICATE);
cv::Canny(dx, dy, _out, threshold1, threshold2, use_true_gradient);
}
else if(!test_cpp)
{
cvCanny( test_array[INPUT][0], test_array[OUTPUT][0], threshold1, threshold2,
aperture_size + (use_true_gradient ? CV_CANNY_L2_GRADIENT : 0));
}
else
{
cv::Mat _out = cv::cvarrToMat(test_array[OUTPUT][0]);
......@@ -283,5 +303,6 @@ int CV_CannyTest::validate_test_results( int test_case_idx )
}
TEST(Imgproc_Canny, accuracy) { CV_CannyTest test; test.safe_run(); }
TEST(Imgproc_Canny, accuracy_deriv) { CV_CannyTest test(true); test.safe_run(); }
/* End of file. */
......@@ -349,8 +349,8 @@ IMPLEMENT_PARAM_CLASS(Channels, int)
#define OCL_TEST_F(name, ...) typedef name OCL_##name; TEST_F(OCL_##name, __VA_ARGS__)
#define OCL_TEST(name, ...) TEST(OCL_##name, __VA_ARGS__)
#define OCL_OFF(fn) cv::ocl::setUseOpenCL(false); fn
#define OCL_ON(fn) cv::ocl::setUseOpenCL(true); fn
#define OCL_OFF(...) cv::ocl::setUseOpenCL(false); __VA_ARGS__ ;
#define OCL_ON(...) cv::ocl::setUseOpenCL(true); __VA_ARGS__ ;
#define OCL_ALL_DEPTHS Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F)
#define OCL_ALL_CHANNELS Values(1, 2, 3, 4)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment