b2007ec089ee94135cf12b4a789fc2d3db70b7d3
[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 #if !defined (HAVE_CUDA)
46
47 void cv::gpu::HoughLinesTransform(const GpuMat&, GpuMat&, GpuMat&, float, float) { throw_nogpu(); }
48 void cv::gpu::HoughLinesGet(const GpuMat&, GpuMat&, float, float, int, bool, int) { throw_nogpu(); }
49 void cv::gpu::HoughLines(const GpuMat&, GpuMat&, float, float, int, bool, int) { throw_nogpu(); }
50 void cv::gpu::HoughLines(const GpuMat&, GpuMat&, GpuMat&, GpuMat&, float, float, int, bool, int) { throw_nogpu(); }
51 void cv::gpu::HoughLinesDownload(const GpuMat&, OutputArray, OutputArray) { throw_nogpu(); }
52
53 #else /* !defined (HAVE_CUDA) */
54
55 namespace cv { namespace gpu { namespace device
56 {
57     namespace hough
58     {
59         int buildPointList_gpu(DevMem2Db src, unsigned int* list);
60
61         void linesAccum_gpu(const unsigned int* list, int count, DevMem2Di accum, float rho, float theta, size_t sharedMemPerBlock, bool has20);
62         int linesGetResult_gpu(DevMem2Di accum, float2* out, int* votes, int maxSize, float rho, float theta, float threshold, bool doSort);
63     }
64 }}}
65
66 //////////////////////////////////////////////////////////
67 // HoughLines
68
69 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
70 {
71     GpuMat accum, buf;
72     HoughLines(src, lines, accum, buf, rho, theta, threshold, doSort, maxLines);
73 }
74
75 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, GpuMat& accum, GpuMat& buf, float rho, float theta, int threshold, bool doSort, int maxLines)
76 {
77     HoughLinesTransform(src, accum, buf, rho, theta);
78     HoughLinesGet(accum, lines, rho, theta, threshold, doSort, maxLines);
79 }
80
81 void cv::gpu::HoughLinesTransform(const GpuMat& src, GpuMat& accum, GpuMat& buf, float rho, float theta)
82 {
83     using namespace cv::gpu::device::hough;
84
85     CV_Assert(src.type() == CV_8UC1);
86     CV_Assert(src.cols < std::numeric_limits<unsigned short>::max());
87     CV_Assert(src.rows < std::numeric_limits<unsigned short>::max());
88
89     ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf);
90
91     const int count = buildPointList_gpu(src, buf.ptr<unsigned int>());
92
93     const int numangle = cvRound(CV_PI / theta);
94     const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
95
96     CV_Assert(numangle > 0 && numrho > 0);
97
98     ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, accum);
99     accum.setTo(Scalar::all(0));
100
101     DeviceInfo devInfo;
102
103     if (count > 0)
104         linesAccum_gpu(buf.ptr<unsigned int>(), count, accum, rho, theta, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20));
105 }
106
107 void cv::gpu::HoughLinesGet(const GpuMat& accum, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
108 {
109     using namespace cv::gpu::device::hough;
110
111     CV_Assert(accum.type() == CV_32SC1);
112
113     ensureSizeIsEnough(2, maxLines, CV_32FC2, lines);
114
115     int count = linesGetResult_gpu(accum, lines.ptr<float2>(0), lines.ptr<int>(1), maxLines, rho, theta, (float)threshold, doSort);
116
117     if (count > 0)
118         lines.cols = count;
119     else
120         lines.release();
121 }
122
123 void cv::gpu::HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines_, OutputArray h_votes_)
124 {
125     if (d_lines.empty())
126     {
127         h_lines_.release();
128         if (h_votes_.needed())
129             h_votes_.release();
130         return;
131     }
132
133     CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2);
134
135     h_lines_.create(1, d_lines.cols, CV_32FC2);
136     Mat h_lines = h_lines_.getMat();
137     d_lines.row(0).download(h_lines);
138
139     if (h_votes_.needed())
140     {
141         h_votes_.create(1, d_lines.cols, CV_32SC1);
142         Mat h_votes = h_votes_.getMat();
143         GpuMat d_votes(1, d_lines.cols, CV_32SC1, const_cast<int*>(d_lines.ptr<int>(1)));
144         d_votes.download(h_votes);
145     }
146 }
147
148 #endif /* !defined (HAVE_CUDA) */