added accuracy and performance tests
authorVladislav Vinogradov <vlad.vinogradov@itseez.com>
Tue, 14 Aug 2012 07:36:32 +0000 (11:36 +0400)
committerVladislav Vinogradov <vlad.vinogradov@itseez.com>
Tue, 14 Aug 2012 07:36:32 +0000 (11:36 +0400)
modules/gpu/perf/perf_imgproc.cpp
modules/gpu/perf_cpu/perf_imgproc.cpp
modules/gpu/src/hough.cpp
modules/gpu/test/test_imgproc.cpp

index b5c986d..77f6074 100644 (file)
@@ -1331,4 +1331,45 @@ INSTANTIATE_TEST_CASE_P(ImgProc, ImagePyramid_getLayer, testing::Combine(
                     MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4),\r
                     MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4))));\r
 \r
+//////////////////////////////////////////////////////////////////////\r
+// HoughLines\r
+\r
+GPU_PERF_TEST(HoughLines, cv::gpu::DeviceInfo, std::string)\r
+{\r
+    const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);\r
+    cv::gpu::setDevice(devInfo.deviceID());\r
+    const std::string fileName = GET_PARAM(1);\r
+\r
+    const float rho = 1.0f;\r
+    const float theta = CV_PI / 180.0f;\r
+    const int threshold = 300;\r
+\r
+    cv::Mat img_base = readImage(fileName, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(img_base.empty());\r
+\r
+    cv::Mat img;\r
+    cv::resize(img_base, img, cv::Size(1920, 1080));\r
+\r
+    cv::Mat edges;\r
+    cv::Canny(img, edges, 50, 200);\r
+\r
+    cv::gpu::GpuMat d_edges(edges);\r
+    cv::gpu::GpuMat d_lines;\r
+    cv::gpu::GpuMat d_accum;\r
+    cv::gpu::HoughLines(d_edges, d_lines, d_accum, rho, theta, threshold);\r
+\r
+    TEST_CYCLE()\r
+    {\r
+        cv::gpu::HoughLines(d_edges, d_lines, d_accum, rho, theta, threshold);\r
+    }\r
+}\r
+\r
+INSTANTIATE_TEST_CASE_P(ImgProc, HoughLines, testing::Combine(\r
+    ALL_DEVICES,\r
+    testing::Values(std::string("cv/shared/pic1.png"),\r
+                    std::string("cv/shared/pic3.png"),\r
+                    std::string("cv/shared/pic4.png"),\r
+                    std::string("cv/shared/pic5.png"),\r
+                    std::string("cv/shared/pic6.png"))));\r
+\r
 #endif\r
index b6686b7..bc77646 100644 (file)
@@ -727,4 +727,41 @@ INSTANTIATE_TEST_CASE_P(ImgProc, CvtColor, testing::Combine(
                     CvtColorInfo(1, 3, cv::COLOR_BayerGR2BGR),\r
                     CvtColorInfo(4, 4, cv::COLOR_RGBA2mRGBA))));\r
 \r
+//////////////////////////////////////////////////////////////////////\r
+// HoughLines\r
+\r
+GPU_PERF_TEST(HoughLines, cv::gpu::DeviceInfo, std::string)\r
+{\r
+    const std::string fileName = GET_PARAM(1);\r
+\r
+    const float rho = 1.0f;\r
+    const float theta = CV_PI / 180.0f;\r
+    const int threshold = 300;\r
+\r
+    cv::Mat img_base = readImage(fileName, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(img_base.empty());\r
+\r
+    cv::Mat img;\r
+    cv::resize(img_base, img, cv::Size(1920, 1080));\r
+\r
+    cv::Mat edges;\r
+    cv::Canny(img, edges, 50, 200);\r
+\r
+    std::vector<cv::Vec2f> lines;\r
+    cv::HoughLines(edges, lines, rho, theta, threshold);\r
+\r
+    TEST_CYCLE()\r
+    {\r
+        cv::HoughLines(edges, lines, rho, theta, threshold);\r
+    }\r
+}\r
+\r
+INSTANTIATE_TEST_CASE_P(ImgProc, HoughLines, testing::Combine(\r
+    ALL_DEVICES,\r
+    testing::Values(std::string("cv/shared/pic1.png"),\r
+                    std::string("cv/shared/pic3.png"),\r
+                    std::string("cv/shared/pic4.png"),\r
+                    std::string("cv/shared/pic5.png"),\r
+                    std::string("cv/shared/pic6.png"))));\r
+\r
 #endif\r
index 888c027..721e680 100644 (file)
@@ -74,7 +74,12 @@ void cv::gpu::HoughLinesGet(const GpuMat& accum, GpuMat& lines, float rho, float
     CV_Assert(accum.type() == CV_32SC1);
 
     lines.create(2, maxLines, CV_32FC2);
-    lines.cols = hough::linesGetResult_gpu(accum, lines.ptr<float2>(0), lines.ptr<int>(1), maxLines, threshold, theta, rho, doSort);
+    int count = hough::linesGetResult_gpu(accum, lines.ptr<float2>(0), lines.ptr<int>(1), maxLines, threshold, theta, rho, doSort);
+
+    if (count > 0)
+        lines.cols = std::min(count, maxLines);
+    else
+        lines.release();
 }
 
 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
@@ -91,6 +96,16 @@ void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, GpuMat& accum, float
 
 void cv::gpu::HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines_, OutputArray h_voices_)
 {
+    if (d_lines.empty())
+    {
+        h_lines_.release();
+        if (h_voices_.needed())
+            h_voices_.release();
+        return;
+    }
+
+    CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2);
+
     h_lines_.create(1, d_lines.cols, CV_32FC2);
     cv::Mat h_lines = h_lines_.getMat();
     d_lines.row(0).download(h_lines);
index 388badf..b0e587e 100644 (file)
@@ -1124,4 +1124,66 @@ INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CornerMinEigen, testing::Combine(
     testing::Values(BlockSize(3), BlockSize(5), BlockSize(7)),\r
     testing::Values(ApertureSize(0), ApertureSize(3), ApertureSize(5), ApertureSize(7))));\r
 \r
+///////////////////////////////////////////////////////////////////////////////////////////////////////\r
+// HoughLines\r
+\r
+PARAM_TEST_CASE(HoughLines, cv::gpu::DeviceInfo, std::string)\r
+{\r
+};\r
+\r
+void drawLines(cv::Mat& dst, const std::vector<cv::Vec2f>& lines)\r
+{\r
+    for (size_t i = 0; i < lines.size(); ++i)\r
+    {\r
+        float rho = lines[i][0], theta = lines[i][1];\r
+        cv::Point pt1, pt2;\r
+        double a = std::cos(theta), b = std::sin(theta);\r
+        double x0 = a*rho, y0 = b*rho;\r
+        pt1.x = cvRound(x0 + 1000*(-b));\r
+        pt1.y = cvRound(y0 + 1000*(a));\r
+        pt2.x = cvRound(x0 - 1000*(-b));\r
+        pt2.y = cvRound(y0 - 1000*(a));\r
+        cv::line(dst, pt1, pt2, cv::Scalar::all(255));\r
+    }\r
+}\r
+\r
+TEST_P(HoughLines, Accuracy)\r
+{\r
+    const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);\r
+    cv::gpu::setDevice(devInfo.deviceID());\r
+    const std::string fileName = GET_PARAM(1);\r
+\r
+    const float rho = 1.0f;\r
+    const float theta = CV_PI / 180.0f;\r
+    const int threshold = 300;\r
+\r
+    cv::Mat img = readImage(fileName, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(img.empty());\r
+\r
+    cv::Mat edges;\r
+    cv::Canny(img, edges, 50, 200);\r
+\r
+    cv::gpu::GpuMat d_lines;\r
+    cv::gpu::HoughLines(loadMat(edges), d_lines, rho, theta, threshold);\r
+    std::vector<cv::Vec2f> lines;\r
+    cv::gpu::HoughLinesDownload(d_lines, lines);\r
+    cv::Mat dst(img.size(), CV_8UC1, cv::Scalar::all(0));\r
+    drawLines(dst, lines);\r
+\r
+    std::vector<cv::Vec2f> lines_gold;\r
+    cv::HoughLines(edges, lines_gold, rho, theta, threshold);\r
+    cv::Mat dst_gold(img.size(), CV_8UC1, cv::Scalar::all(0));\r
+    drawLines(dst_gold, lines_gold);\r
+\r
+    ASSERT_MAT_NEAR(dst_gold, dst, 0.0);\r
+}\r
+\r
+INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HoughLines, testing::Combine(\r
+    ALL_DEVICES,\r
+    testing::Values(std::string("../cv/shared/pic1.png"),\r
+                    std::string("../cv/shared/pic3.png"),\r
+                    std::string("../cv/shared/pic4.png"),\r
+                    std::string("../cv/shared/pic5.png"),\r
+                    std::string("../cv/shared/pic6.png"))));\r
+\r
 } // namespace\r