new optimized version
[profile/ivi/opencv.git] / modules / gpu / src / hough.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44
45 namespace cv { namespace gpu { namespace device
46 {
47     namespace hough
48     {
49         unsigned int buildPointList_gpu(DevMem2Db src, unsigned int* list);
50         void linesAccum_gpu(const unsigned int* list, unsigned int count, DevMem2D_<unsigned int> accum, float rho, float theta);
51         unsigned int linesGetResult_gpu(DevMem2D_<uint> accum, float2* out, int* voices, unsigned int maxSize, float rho, float theta, float threshold, bool doSort);
52     }
53 }}}
54
55 void cv::gpu::HoughLinesTransform(const GpuMat& src, GpuMat& accum, GpuMat& buf, float rho, float theta)
56 {
57     using namespace cv::gpu::device::hough;
58
59     CV_Assert(src.type() == CV_8UC1);
60     CV_Assert(src.cols < std::numeric_limits<unsigned short>::max());
61     CV_Assert(src.rows < std::numeric_limits<unsigned short>::max());
62
63     ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf);
64
65     unsigned int count = buildPointList_gpu(src, buf.ptr<unsigned int>());
66
67     const int numangle = cvRound(CV_PI / theta);
68     const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
69
70     ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, accum);
71     accum.setTo(cv::Scalar::all(0));
72
73     linesAccum_gpu(buf.ptr<unsigned int>(), count, accum, rho, theta);
74 }
75
76 void cv::gpu::HoughLinesGet(const GpuMat& accum, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
77 {
78     using namespace cv::gpu::device;
79
80     CV_Assert(accum.type() == CV_32SC1);
81
82     ensureSizeIsEnough(2, maxLines, CV_32FC2, lines);
83     unsigned int count = hough::linesGetResult_gpu(accum, lines.ptr<float2>(0), lines.ptr<int>(1), maxLines, rho, theta, threshold, doSort);
84
85     if (count > 0)
86         lines.cols = count;
87     else
88         lines.release();
89 }
90
91 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
92 {
93     cv::gpu::GpuMat accum, buf;
94     HoughLines(src, lines, accum, buf, rho, theta, threshold, doSort, maxLines);
95 }
96
97 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, GpuMat& accum, GpuMat& buf, float rho, float theta, int threshold, bool doSort, int maxLines)
98 {
99     HoughLinesTransform(src, accum, buf, rho, theta);
100     HoughLinesGet(accum, lines, rho, theta, threshold, doSort, maxLines);
101 }
102
103 void cv::gpu::HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines_, OutputArray h_voices_)
104 {
105     if (d_lines.empty())
106     {
107         h_lines_.release();
108         if (h_voices_.needed())
109             h_voices_.release();
110         return;
111     }
112
113     CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2);
114
115     h_lines_.create(1, d_lines.cols, CV_32FC2);
116     cv::Mat h_lines = h_lines_.getMat();
117     d_lines.row(0).download(h_lines);
118
119     if (h_voices_.needed())
120     {
121         h_voices_.create(1, d_lines.cols, CV_32SC1);
122         cv::Mat h_voices = h_voices_.getMat();
123         cv::gpu::GpuMat d_voices(1, d_lines.cols, CV_32SC1, const_cast<int*>(d_lines.ptr<int>(1)));
124         d_voices.download(h_voices);
125     }
126 }