cuda_invoker.hpp 4.77 KB
Newer Older
marina.kolpakova's avatar
marina.kolpakova committed
1
//M///////////////////////////////////////////////////////////////////////////////////////
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
25
//     and / or other materials provided with the distribution.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
marina.kolpakova's avatar
marina.kolpakova committed
41
//M
42

marina.kolpakova's avatar
marina.kolpakova committed
43

marina.kolpakova's avatar
marina.kolpakova committed
44 45
#ifndef __OPENCV_ICF_HPP__
#define __OPENCV_ICF_HPP__
marina.kolpakova's avatar
marina.kolpakova committed
46

47
#include "opencv2/core/cuda_types.hpp"
48
#include "cuda_runtime_api.h"
49

50
#if defined __CUDACC__
51
# define __device_inline__ __device__ __forceinline__
52
#else
53
# define __device_inline__
54
#endif
marina.kolpakova's avatar
marina.kolpakova committed
55 56


57
namespace cv { namespace softcascade { namespace cudev {
marina.kolpakova's avatar
marina.kolpakova committed
58

59
typedef unsigned char uchar;
60 61
typedef unsigned int uint;
typedef unsigned short ushort;
62

marina.kolpakova's avatar
marina.kolpakova committed
63
struct Octave
marina.kolpakova's avatar
marina.kolpakova committed
64
{
65 66
    float scale;
    ushort2 size;
marina.kolpakova's avatar
marina.kolpakova committed
67 68 69 70 71
    ushort index;
    ushort stages;
    ushort shrinkage;

    Octave(const ushort i, const ushort s, const ushort sh, const ushort2 sz, const float sc)
72
    : scale(sc), size(sz), index(i), stages(s), shrinkage(sh) {}
marina.kolpakova's avatar
marina.kolpakova committed
73 74
};

75
struct Level
marina.kolpakova's avatar
marina.kolpakova committed
76 77
{
    int octave;
marina.kolpakova's avatar
marina.kolpakova committed
78
    int step;
marina.kolpakova's avatar
marina.kolpakova committed
79 80

    float relScale;
81
    float scaling[2];// calculated according to Dollar paper
marina.kolpakova's avatar
marina.kolpakova committed
82 83 84 85

    uchar2 workRect;
    uchar2 objSize;

86
    Level(int idx, const Octave& oct, const float scale, const int w, const int h);
87
    __device_inline__ Level(){}
marina.kolpakova's avatar
marina.kolpakova committed
88 89
};

marina.kolpakova's avatar
marina.kolpakova committed
90
struct Node
marina.kolpakova's avatar
marina.kolpakova committed
91 92
{
    uchar4 rect;
marina.kolpakova's avatar
marina.kolpakova committed
93
    // ushort channel;
94
    unsigned int threshold;
marina.kolpakova's avatar
marina.kolpakova committed
95

marina.kolpakova's avatar
marina.kolpakova committed
96 97
    enum { THRESHOLD_MASK = 0x0FFFFFFF };

98
    Node(const uchar4 r, const unsigned int ch, const unsigned int t) : rect(r), threshold(t + (ch << 28)) {}
marina.kolpakova's avatar
marina.kolpakova committed
99 100
};

marina.kolpakova's avatar
marina.kolpakova committed
101
struct Detection
102 103 104 105 106
{
    ushort x;
    ushort y;
    ushort w;
    ushort h;
marina.kolpakova's avatar
marina.kolpakova committed
107

108 109 110 111
    float confidence;
    int kind;

    Detection(){}
112
    __device_inline__ Detection(int _x, int _y, uchar _w, uchar _h, float c)
113
    : x(static_cast<ushort>(_x)), y(static_cast<ushort>(_y)), w(_w), h(_h), confidence(c), kind(0) {}
114
};
marina.kolpakova's avatar
marina.kolpakova committed
115

marina.kolpakova's avatar
marina.kolpakova committed
116
struct GK107PolicyX4
marina.kolpakova's avatar
marina.kolpakova committed
117
{
marina.kolpakova's avatar
marina.kolpakova committed
118
    enum {WARP = 32, STA_X = WARP, STA_Y = 8, SHRINKAGE = 4};
119
    typedef float2 roi_type;
marina.kolpakova's avatar
marina.kolpakova committed
120 121
    static const dim3 block()
    {
122
        return dim3(STA_X, STA_Y);
marina.kolpakova's avatar
marina.kolpakova committed
123
    }
marina.kolpakova's avatar
marina.kolpakova committed
124 125 126 127 128
};

template<typename Policy>
struct CascadeInvoker
{
marina.kolpakova's avatar
marina.kolpakova committed
129 130
    CascadeInvoker(): levels(0), stages(0), nodes(0), leaves(0), scales(0) {}

131 132
    CascadeInvoker(const cv::cuda::PtrStepSzb& _levels, const cv::cuda::PtrStepSzf& _stages,
                   const cv::cuda::PtrStepSzb& _nodes,  const cv::cuda::PtrStepSzf& _leaves)
marina.kolpakova's avatar
marina.kolpakova committed
133 134 135 136
    : levels((const Level*)_levels.ptr()),
      stages((const float*)_stages.ptr()),
      nodes((const Node*)_nodes.ptr()), leaves((const float*)_leaves.ptr()),
      scales(_levels.cols / sizeof(Level))
marina.kolpakova's avatar
marina.kolpakova committed
137 138 139 140 141 142 143 144
    {}

    const Level*  levels;
    const float*  stages;

    const Node*   nodes;
    const float*  leaves;

marina.kolpakova's avatar
marina.kolpakova committed
145 146
    int scales;

147
    void operator()(const cv::cuda::PtrStepSzb& roi, const cv::cuda::PtrStepSzi& hogluv, cv::cuda::PtrStepSz<uchar4> objects,
148
        const int downscales, const cudaStream_t& stream = 0) const;
marina.kolpakova's avatar
marina.kolpakova committed
149 150

    template<bool isUp>
151
    __device_inline__ void detect(Detection* objects, const unsigned int ndetections, unsigned int* ctr, const int downscales) const;
marina.kolpakova's avatar
marina.kolpakova committed
152 153
};

marina.kolpakova's avatar
marina.kolpakova committed
154
}}}
marina.kolpakova's avatar
marina.kolpakova committed
155

156
#endif