Home | History | Annotate | Download | only in grid
      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 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
     16 // Third party copyrights are property of their respective owners.
     17 //
     18 // Redistribution and use in source and binary forms, with or without modification,
     19 // are permitted provided that the following conditions are met:
     20 //
     21 //   * Redistribution's of source code must retain the above copyright notice,
     22 //     this list of conditions and the following disclaimer.
     23 //
     24 //   * Redistribution's in binary form must reproduce the above copyright notice,
     25 //     this list of conditions and the following disclaimer in the documentation
     26 //     and/or other materials provided with the distribution.
     27 //
     28 //   * The name of the copyright holders may not be used to endorse or promote products
     29 //     derived from this software without specific prior written permission.
     30 //
     31 // This software is provided by the copyright holders and contributors "as is" and
     32 // any express or implied warranties, including, but not limited to, the implied
     33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     34 // In no event shall the Intel Corporation or contributors be liable for any direct,
     35 // indirect, incidental, special, exemplary, or consequential damages
     36 // (including, but not limited to, procurement of substitute goods or services;
     37 // loss of use, data, or profits; or business interruption) however caused
     38 // and on any theory of liability, whether in contract, strict liability,
     39 // or tort (including negligence or otherwise) arising in any way out of
     40 // the use of this software, even if advised of the possibility of such damage.
     41 //
     42 //M*/
     43 
     44 #pragma once
     45 
     46 #ifndef __OPENCV_CUDEV_GRID_REDUCE_HPP__
     47 #define __OPENCV_CUDEV_GRID_REDUCE_HPP__
     48 
     49 #include <limits>
     50 #include "../common.hpp"
     51 #include "../ptr2d/traits.hpp"
     52 #include "../ptr2d/gpumat.hpp"
     53 #include "../ptr2d/mask.hpp"
     54 #include "../ptr2d/transform.hpp"
     55 #include "detail/reduce.hpp"
     56 #include "detail/minmaxloc.hpp"
     57 
     58 namespace cv { namespace cudev {
     59 
     60 //! @addtogroup cudev
     61 //! @{
     62 
     63 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
     64 __host__ void gridCalcSum_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
     65 {
     66     typedef typename PtrTraits<SrcPtr>::value_type src_type;
     67 
     68     CV_StaticAssert( unsigned(VecTraits<src_type>::cn) == unsigned(VecTraits<ResType>::cn), "" );
     69 
     70     dst.create(1, 1);
     71     dst.setTo(0, stream);
     72 
     73     const int rows = getRows(src);
     74     const int cols = getCols(src);
     75 
     76     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
     77 
     78     grid_reduce_detail::sum<Policy>(shrinkPtr(src),
     79                                     dst[0],
     80                                     shrinkPtr(mask),
     81                                     rows, cols,
     82                                     StreamAccessor::getStream(stream));
     83 }
     84 
     85 template <class Policy, class SrcPtr, typename ResType>
     86 __host__ void gridCalcSum_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
     87 {
     88     typedef typename PtrTraits<SrcPtr>::value_type src_type;
     89 
     90     CV_StaticAssert( unsigned(VecTraits<src_type>::cn) == unsigned(VecTraits<ResType>::cn), "" );
     91 
     92     dst.create(1, 1);
     93     dst.setTo(0, stream);
     94 
     95     const int rows = getRows(src);
     96     const int cols = getCols(src);
     97 
     98     grid_reduce_detail::sum<Policy>(shrinkPtr(src),
     99                                     dst[0],
    100                                     WithOutMask(),
    101                                     rows, cols,
    102                                     StreamAccessor::getStream(stream));
    103 }
    104 
    105 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    106 __host__ void gridFindMinVal_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    107 {
    108     dst.create(1, 1);
    109     dst.setTo(Scalar::all(std::numeric_limits<ResType>::max()), stream);
    110 
    111     const int rows = getRows(src);
    112     const int cols = getCols(src);
    113 
    114     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
    115 
    116     grid_reduce_detail::minVal<Policy>(shrinkPtr(src),
    117                                        dst[0],
    118                                        shrinkPtr(mask),
    119                                        rows, cols,
    120                                        StreamAccessor::getStream(stream));
    121 }
    122 
    123 template <class Policy, class SrcPtr, typename ResType>
    124 __host__ void gridFindMinVal_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    125 {
    126     dst.create(1, 1);
    127     dst.setTo(Scalar::all(std::numeric_limits<ResType>::max()), stream);
    128 
    129     const int rows = getRows(src);
    130     const int cols = getCols(src);
    131 
    132     grid_reduce_detail::minVal<Policy>(shrinkPtr(src),
    133                                        dst[0],
    134                                        WithOutMask(),
    135                                        rows, cols,
    136                                        StreamAccessor::getStream(stream));
    137 }
    138 
    139 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    140 __host__ void gridFindMaxVal_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    141 {
    142     dst.create(1, 1);
    143     dst.setTo(Scalar::all(-std::numeric_limits<ResType>::max()), stream);
    144 
    145     const int rows = getRows(src);
    146     const int cols = getCols(src);
    147 
    148     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
    149 
    150     grid_reduce_detail::maxVal<Policy>(shrinkPtr(src),
    151                                        dst[0],
    152                                        shrinkPtr(mask),
    153                                        rows, cols,
    154                                        StreamAccessor::getStream(stream));
    155 }
    156 
    157 template <class Policy, class SrcPtr, typename ResType>
    158 __host__ void gridFindMaxVal_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    159 {
    160     dst.create(1, 1);
    161     dst.setTo(Scalar::all(-std::numeric_limits<ResType>::max()), stream);
    162 
    163     const int rows = getRows(src);
    164     const int cols = getCols(src);
    165 
    166     grid_reduce_detail::maxVal<Policy>(shrinkPtr(src),
    167                                        dst[0],
    168                                        WithOutMask(),
    169                                        rows, cols,
    170                                        StreamAccessor::getStream(stream));
    171 }
    172 
    173 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    174 __host__ void gridFindMinMaxVal_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    175 {
    176     dst.create(1, 2);
    177     dst.col(0).setTo(Scalar::all(std::numeric_limits<ResType>::max()), stream);
    178     dst.col(1).setTo(Scalar::all(-std::numeric_limits<ResType>::max()), stream);
    179 
    180     const int rows = getRows(src);
    181     const int cols = getCols(src);
    182 
    183     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
    184 
    185     grid_reduce_detail::minMaxVal<Policy>(shrinkPtr(src),
    186                                           dst[0],
    187                                           shrinkPtr(mask),
    188                                           rows, cols,
    189                                           StreamAccessor::getStream(stream));
    190 }
    191 
    192 template <class Policy, class SrcPtr, typename ResType>
    193 __host__ void gridFindMinMaxVal_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    194 {
    195     dst.create(1, 2);
    196     dst.col(0).setTo(Scalar::all(std::numeric_limits<ResType>::max()), stream);
    197     dst.col(1).setTo(Scalar::all(-std::numeric_limits<ResType>::max()), stream);
    198 
    199     const int rows = getRows(src);
    200     const int cols = getCols(src);
    201 
    202     grid_reduce_detail::minMaxVal<Policy>(shrinkPtr(src),
    203                                           dst[0],
    204                                           WithOutMask(),
    205                                           rows, cols,
    206                                           StreamAccessor::getStream(stream));
    207 }
    208 
    209 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    210 __host__ void gridMinMaxLoc_(const SrcPtr& src, GpuMat_<ResType>& valBuf, GpuMat_<int>& locBuf, const MaskPtr& mask, Stream& stream = Stream::Null())
    211 {
    212     const int rows = getRows(src);
    213     const int cols = getCols(src);
    214 
    215     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
    216 
    217     dim3 grid, block;
    218     grid_minmaxloc_detail::getLaunchCfg<Policy>(rows, cols, block, grid);
    219 
    220     valBuf.create(2, grid.x * grid.y);
    221     locBuf.create(2, grid.x * grid.y);
    222 
    223     grid_minmaxloc_detail::minMaxLoc<Policy>(shrinkPtr(src),
    224                                              valBuf[0], valBuf[1], locBuf[0], locBuf[1],
    225                                              shrinkPtr(mask),
    226                                              rows, cols,
    227                                              StreamAccessor::getStream(stream));
    228 }
    229 
    230 template <class Policy, class SrcPtr, typename ResType>
    231 __host__ void gridMinMaxLoc_(const SrcPtr& src, GpuMat_<ResType>& valBuf, GpuMat_<int>& locBuf, Stream& stream = Stream::Null())
    232 {
    233     const int rows = getRows(src);
    234     const int cols = getCols(src);
    235 
    236     dim3 grid, block;
    237     grid_minmaxloc_detail::getLaunchCfg<Policy>(rows, cols, block, grid);
    238 
    239     valBuf.create(2, grid.x * grid.y);
    240     locBuf.create(2, grid.x * grid.y);
    241 
    242     grid_minmaxloc_detail::minMaxLoc<Policy>(shrinkPtr(src),
    243                                              valBuf[0], valBuf[1], locBuf[0], locBuf[1],
    244                                              WithOutMask(),
    245                                              rows, cols,
    246                                              StreamAccessor::getStream(stream));
    247 }
    248 
    249 template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    250 __host__ void gridCountNonZero_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    251 {
    252     dst.create(1, 1);
    253     dst.setTo(0, stream);
    254 
    255     const int rows = getRows(src);
    256     const int cols = getCols(src);
    257 
    258     CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
    259 
    260     typedef typename PtrTraits<SrcPtr>::value_type src_type;
    261     not_equal_to<src_type> ne_op;
    262     const src_type zero = VecTraits<src_type>::all(0);
    263 
    264     grid_reduce_detail::sum<Policy>(shrinkPtr(transformPtr(src, bind2nd(ne_op, zero))),
    265                                     dst[0],
    266                                     shrinkPtr(mask),
    267                                     rows, cols,
    268                                     StreamAccessor::getStream(stream));
    269 }
    270 
    271 template <class Policy, class SrcPtr, typename ResType>
    272 __host__ void gridCountNonZero_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    273 {
    274     dst.create(1, 1);
    275     dst.setTo(0, stream);
    276 
    277     const int rows = getRows(src);
    278     const int cols = getCols(src);
    279 
    280     typedef typename PtrTraits<SrcPtr>::value_type src_type;
    281     not_equal_to<src_type> ne_op;
    282     const src_type zero = VecTraits<src_type>::all(0);
    283 
    284     grid_reduce_detail::sum<Policy>(shrinkPtr(transformPtr(src, bind2nd(ne_op, zero))),
    285                                     dst[0],
    286                                     WithOutMask(),
    287                                     rows, cols,
    288                                     StreamAccessor::getStream(stream));
    289 }
    290 
    291 // default policy
    292 
    293 struct DefaultGlobReducePolicy
    294 {
    295     enum {
    296         block_size_x = 32,
    297         block_size_y = 8,
    298 
    299         patch_size_x = 4,
    300         patch_size_y = 4
    301     };
    302 };
    303 
    304 template <class SrcPtr, typename ResType, class MaskPtr>
    305 __host__ void gridCalcSum(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    306 {
    307     gridCalcSum_<DefaultGlobReducePolicy>(src, dst, mask, stream);
    308 }
    309 
    310 template <class SrcPtr, typename ResType>
    311 __host__ void gridCalcSum(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    312 {
    313     gridCalcSum_<DefaultGlobReducePolicy>(src, dst, stream);
    314 }
    315 
    316 template <class SrcPtr, typename ResType, class MaskPtr>
    317 __host__ void gridFindMinVal(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    318 {
    319     gridFindMinVal_<DefaultGlobReducePolicy>(src, dst, mask, stream);
    320 }
    321 
    322 template <class SrcPtr, typename ResType>
    323 __host__ void gridFindMinVal(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    324 {
    325     gridFindMinVal_<DefaultGlobReducePolicy>(src, dst, stream);
    326 }
    327 
    328 template <class SrcPtr, typename ResType, class MaskPtr>
    329 __host__ void gridFindMaxVal(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    330 {
    331     gridFindMaxVal_<DefaultGlobReducePolicy>(src, dst, mask, stream);
    332 }
    333 
    334 template <class SrcPtr, typename ResType>
    335 __host__ void gridFindMaxVal(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    336 {
    337     gridFindMaxVal_<DefaultGlobReducePolicy>(src, dst, stream);
    338 }
    339 
    340 template <class SrcPtr, typename ResType, class MaskPtr>
    341 __host__ void gridFindMinMaxVal(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    342 {
    343     gridFindMinMaxVal_<DefaultGlobReducePolicy>(src, dst, mask, stream);
    344 }
    345 
    346 template <class SrcPtr, typename ResType>
    347 __host__ void gridFindMinMaxVal(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    348 {
    349     gridFindMinMaxVal_<DefaultGlobReducePolicy>(src, dst, stream);
    350 }
    351 
    352 template <class SrcPtr, typename ResType, class MaskPtr>
    353 __host__ void gridMinMaxLoc(const SrcPtr& src, GpuMat_<ResType>& valBuf, GpuMat_<int>& locBuf, const MaskPtr& mask, Stream& stream = Stream::Null())
    354 {
    355     gridMinMaxLoc_<DefaultGlobReducePolicy>(src, valBuf, locBuf, mask, stream);
    356 }
    357 
    358 template <class SrcPtr, typename ResType>
    359 __host__ void gridMinMaxLoc(const SrcPtr& src, GpuMat_<ResType>& valBuf, GpuMat_<int>& locBuf, Stream& stream = Stream::Null())
    360 {
    361     gridMinMaxLoc_<DefaultGlobReducePolicy>(src, valBuf, locBuf, stream);
    362 }
    363 
    364 template <class SrcPtr, typename ResType, class MaskPtr>
    365 __host__ void gridCountNonZero(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
    366 {
    367     gridCountNonZero_<DefaultGlobReducePolicy>(src, dst, mask, stream);
    368 }
    369 
    370 template <class SrcPtr, typename ResType>
    371 __host__ void gridCountNonZero(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
    372 {
    373     gridCountNonZero_<DefaultGlobReducePolicy>(src, dst, stream);
    374 }
    375 
    376 //! @}
    377 
    378 }}
    379 
    380 #endif
    381