Path: blob/master/modules/imgproc/test/ocl/test_houghlines.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34// Copyright (C) 2014, Itseez, Inc., all rights reserved.5// Third party copyrights are property of their respective owners.67#include "../test_precomp.hpp"8#include "opencv2/ts/ocl_test.hpp"910#ifdef HAVE_OPENCL1112namespace opencv_test {13namespace ocl {1415struct Vec2fComparator16{17bool operator()(const Vec2f& a, const Vec2f b) const18{19if(a[0] != b[0]) return a[0] < b[0];20else return a[1] < b[1];21}22};2324/////////////////////////////// HoughLines ////////////////////////////////////2526PARAM_TEST_CASE(HoughLines, double, double, int)27{28double rhoStep, thetaStep;29int threshold;3031Size src_size;32Mat src, dst;33UMat usrc, udst;3435virtual void SetUp()36{37rhoStep = GET_PARAM(0);38thetaStep = GET_PARAM(1);39threshold = GET_PARAM(2);40}4142void generateTestData()43{44src_size = randomSize(500, 1920);45src.create(src_size, CV_8UC1);46src.setTo(Scalar::all(0));47line(src, Point(0, 100), Point(100, 100), Scalar::all(255), 1);48line(src, Point(0, 200), Point(100, 200), Scalar::all(255), 1);49line(src, Point(0, 400), Point(100, 400), Scalar::all(255), 1);50line(src, Point(100, 0), Point(100, 200), Scalar::all(255), 1);51line(src, Point(200, 0), Point(200, 200), Scalar::all(255), 1);52line(src, Point(400, 0), Point(400, 200), Scalar::all(255), 1);5354src.copyTo(usrc);55}5657void readRealTestData()58{59Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);60Canny(img, src, 100, 150, 3);6162src.copyTo(usrc);63}6465void Near(double eps = 0.)66{67EXPECT_EQ(dst.size(), udst.size());6869if (dst.total() > 0)70{71Mat lines_cpu, lines_gpu;72dst.copyTo(lines_cpu);73udst.copyTo(lines_gpu);7475std::sort(lines_cpu.begin<Vec2f>(), lines_cpu.end<Vec2f>(), Vec2fComparator());76std::sort(lines_gpu.begin<Vec2f>(), lines_gpu.end<Vec2f>(), Vec2fComparator());7778EXPECT_LE(TestUtils::checkNorm2(lines_cpu, lines_gpu), eps);79}80}81};8283OCL_TEST_P(HoughLines, RealImage)84{85readRealTestData();8687OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));88OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));8990Near(1e-5);91}9293OCL_TEST_P(HoughLines, GeneratedImage)94{95for (int j = 0; j < test_loop_times; j++)96{97generateTestData();9899OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));100OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));101102Near(1e-5);103}104}105106/////////////////////////////// HoughLinesP ///////////////////////////////////107108PARAM_TEST_CASE(HoughLinesP, int, double, double)109{110double rhoStep, thetaStep, minLineLength, maxGap;111int threshold;112113Size src_size;114Mat src, dst;115UMat usrc, udst;116117virtual void SetUp()118{119rhoStep = 1.0;120thetaStep = CV_PI / 180;121threshold = GET_PARAM(0);122minLineLength = GET_PARAM(1);123maxGap = GET_PARAM(2);124}125126void readRealTestData()127{128Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);129Canny(img, src, 50, 200, 3);130131src.copyTo(usrc);132}133134void Near(double eps = 0.)135{136Mat lines_gpu = udst.getMat(ACCESS_READ);137138if (dst.total() > 0 && lines_gpu.total() > 0)139{140Mat result_cpu(src.size(), CV_8UC1, Scalar::all(0));141Mat result_gpu(src.size(), CV_8UC1, Scalar::all(0));142143MatConstIterator_<Vec4i> it = dst.begin<Vec4i>(), end = dst.end<Vec4i>();144for ( ; it != end; it++)145{146Vec4i p = *it;147line(result_cpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));148}149150it = lines_gpu.begin<Vec4i>(), end = lines_gpu.end<Vec4i>();151for ( ; it != end; it++)152{153Vec4i p = *it;154line(result_gpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));155}156157EXPECT_MAT_SIMILAR(result_cpu, result_gpu, eps);158}159}160};161162163OCL_TEST_P(HoughLinesP, RealImage)164{165readRealTestData();166167OCL_OFF(cv::HoughLinesP(src, dst, rhoStep, thetaStep, threshold, minLineLength, maxGap));168OCL_ON(cv::HoughLinesP(usrc, udst, rhoStep, thetaStep, threshold, minLineLength, maxGap));169170Near(0.25);171}172173OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLines, Combine(Values(1, 0.5), // rhoStep174Values(CV_PI / 180.0, CV_PI / 360.0), // thetaStep175Values(80, 150))); // threshold176177OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLinesP, Combine(Values(100, 150), // threshold178Values(50, 100), // minLineLength179Values(5, 10))); // maxLineGap180181} } // namespace opencv_test::ocl182183#endif // HAVE_OPENCL184185