basic版本的yolo,在yolov3版本上增加人体跟踪
xuepengqiang
2020-05-26 5966f2b095841627d62daac0159e81f83544b85c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef _MATRIX_H_
#define _MATRIX_H_
 
#include <initializer_list>
#include <cstdlib>
#include <ostream>
#include <cassert>
#include <cstdlib>
#include <algorithm>
 
#define XYZMIN(x, y) (x)<(y)?(x):(y)
#define XYZMAX(x, y) (x)>(y)?(x):(y)
 
template <class T>
class Matrix {
public:
    Matrix(){
        m_rows = 0;
        m_columns = 0;
        m_matrix = nullptr;
    }
    Matrix(const size_t rows, const size_t columns)  {
        m_matrix = nullptr;
        resize(rows, columns);
    }
    Matrix(const std::initializer_list<std::initializer_list<T>> init) {
        m_matrix = nullptr;
        m_rows = init.size();
        if ( m_rows == 0 ) {
            m_columns = 0;
        } else {
            m_columns = init.begin()->size();
            if ( m_columns > 0 ) {
                resize(m_rows, m_columns);
            }
        }
 
        size_t i = 0, j;
        for ( auto row = init.begin() ; row != init.end() ; ++row, ++i ) {
            assert ( row->size() == m_columns && "error." );
            j = 0;
            for ( auto value = row->begin() ; value != row->end() ; ++value, ++j ) {
                m_matrix[i][j] = *value;
            }
        }
    }
    Matrix(const Matrix<T> &other)  {
        if ( other.m_matrix != nullptr ) {
            m_matrix = nullptr;
            resize(other.m_rows, other.m_columns);
            for ( size_t i = 0 ; i < m_rows ; i++ ) {
                for ( size_t j = 0 ; j < m_columns ; j++ ) {
                    m_matrix[i][j] = other.m_matrix[i][j];
                }
            }
        } else {
            m_matrix = nullptr;
            m_rows = 0;
            m_columns = 0;
        }
    }
    Matrix<T> & operator= (const Matrix<T> &other){
        if ( other.m_matrix != nullptr ) {
            resize(other.m_rows, other.m_columns);
            for ( size_t i = 0 ; i < m_rows ; i++ ) {
                for ( size_t j = 0 ; j < m_columns ; j++ ) {
                    m_matrix[i][j] = other.m_matrix[i][j];
                }
            }
        } else {
            for ( size_t i = 0 ; i < m_columns ; i++ ) {
                delete [] m_matrix[i];
            }
 
            delete [] m_matrix;
 
            m_matrix = nullptr;
            m_rows = 0;
            m_columns = 0;
        }
 
        return *this;
    }
    ~Matrix(){
        if ( m_matrix != nullptr ) {
            for ( size_t i = 0 ; i < m_rows ; i++ ) {
                delete [] m_matrix[i];
            }
 
            delete [] m_matrix;
        }
        m_matrix = nullptr;
    }
    void resize(const size_t rows, const size_t columns, const T default_value = 0) {
        assert ( rows > 0 && columns > 0 && "Columns and rows must exist." );
 
        if ( m_matrix == nullptr ) {
            // alloc arrays
            m_matrix = new T*[rows]; // rows
            for ( size_t i = 0 ; i < rows ; i++ ) {
                m_matrix[i] = new T[columns]; // columns
            }
 
            m_rows = rows;
            m_columns = columns;
            clear();
        } else {
            T **new_matrix;
            new_matrix = new T*[rows]; // rows
            for ( size_t i = 0 ; i < rows ; i++ ) {
                new_matrix[i] = new T[columns]; // columns
                for ( size_t j = 0 ; j < columns ; j++ ) {
                    new_matrix[i][j] = default_value;
                }
            }
 
            size_t minrows = XYZMIN(rows, m_rows);
            size_t mincols = XYZMIN(columns, m_columns);
            for ( size_t x = 0 ; x < minrows ; x++ ) {
                for ( size_t y = 0 ; y < mincols ; y++ ) {
                    new_matrix[x][y] = m_matrix[x][y];
                }
            }
 
            if ( m_matrix != nullptr ) {
                for ( size_t i = 0 ; i < m_rows ; i++ ) {
                    delete [] m_matrix[i];
                }
 
                delete [] m_matrix;
            }
 
            m_matrix = new_matrix;
        }
 
        m_rows = rows;
        m_columns = columns;
    }
    void clear() {
        assert( m_matrix != nullptr );
 
        for ( size_t i = 0 ; i < m_rows ; i++ ) {
            for ( size_t j = 0 ; j < m_columns ; j++ ) {
                m_matrix[i][j] = 0;
            }
        }
    }
    T& operator () (const size_t x, const size_t y) {
        assert ( x < m_rows );
        assert ( y < m_columns );
        assert ( m_matrix != nullptr );
        return m_matrix[x][y];
    }
 
    const T& operator () (const size_t x, const size_t y) const {
        assert ( x < m_rows );
        assert ( y < m_columns );
        assert ( m_matrix != nullptr );
        return m_matrix[x][y];
    }
    const T mmin() const {
        assert( m_matrix != nullptr );
        assert ( m_rows > 0 );
        assert ( m_columns > 0 );
        T min = m_matrix[0][0];
 
        for ( size_t i = 0 ; i < m_rows ; i++ ) {
            for ( size_t j = 0 ; j < m_columns ; j++ ) {
                min = std::min<T>(min, m_matrix[i][j]);
            }
        }
 
        return min;
    }
 
    const T mmax() const {
        assert( m_matrix != nullptr );
        assert ( m_rows > 0 );
        assert ( m_columns > 0 );
        T max = m_matrix[0][0];
 
        for ( size_t i = 0 ; i < m_rows ; i++ ) {
            for ( size_t j = 0 ; j < m_columns ; j++ ) {
                max = std::max<T>(max, m_matrix[i][j]);
            }
        }
 
        return max;
    }
    inline size_t minsize() { return ((m_rows < m_columns) ? m_rows : m_columns); }
    inline size_t columns() const { return m_columns;}
    inline size_t rows() const { return m_rows;}
 
    friend std::ostream& operator<<(std::ostream& os, const Matrix &matrix)
    {
        os << "Matrix:" << std::endl;
        for (size_t row = 0 ; row < matrix.rows() ; row++ )
        {
            for (size_t col = 0 ; col < matrix.columns() ; col++ )
            {
                os.width(8);
                os << matrix(row, col) << ",";
            }
            os << std::endl;
        }
        return os;
    }
 
private:
    T **m_matrix;
    size_t m_rows;
    size_t m_columns;
};
 
 
#endif /* !defined(_MATRIX_H_) */