scikit-image

Форк
0
/
_hoghistogram.pyx 
160 строк · 5.5 Кб
1
# cython: cdivision=True
2
# cython: boundscheck=False
3
# cython: wraparound=False
4

5
import numpy as np
6
cimport numpy as cnp
7

8
from .._shared.fused_numerics cimport np_floats
9
cnp.import_array()
10

11

12
cdef np_floats cell_hog(np_floats[:, ::1] magnitude,
13
                        np_floats[:, ::1] orientation,
14
                        np_floats orientation_start, np_floats orientation_end,
15
                        int cell_columns, int cell_rows,
16
                        int column_index, int row_index,
17
                        int size_columns, int size_rows,
18
                        int range_rows_start, int range_rows_stop,
19
                        int range_columns_start, int range_columns_stop) noexcept nogil:
20
    """Calculation of the cell's HOG value
21

22
    Parameters
23
    ----------
24
    magnitude : ndarray
25
        The gradient magnitudes of the pixels.
26
    orientation : ndarray
27
        Lookup table for orientations.
28
    orientation_start : float
29
        Orientation range start.
30
    orientation_end : float
31
        Orientation range end.
32
    cell_columns : int
33
        Pixels per cell (rows).
34
    cell_rows : int
35
        Pixels per cell (columns).
36
    column_index : int
37
        Block column index.
38
    row_index : int
39
        Block row index.
40
    size_columns : int
41
        Number of columns.
42
    size_rows : int
43
        Number of rows.
44
    range_rows_start : int
45
        Start row of cell.
46
    range_rows_stop : int
47
        Stop row of cell.
48
    range_columns_start : int
49
        Start column of cell.
50
    range_columns_stop : int
51
        Stop column of cell
52

53
    Returns
54
    -------
55
    total : float
56
        The total HOG value.
57
    """
58
    cdef int cell_column, cell_row, cell_row_index, cell_column_index
59
    cdef cnp.float32_t total = 0.
60

61
    for cell_row in range(range_rows_start, range_rows_stop):
62
        cell_row_index = row_index + cell_row
63
        if (cell_row_index < 0 or cell_row_index >= size_rows):
64
            continue
65

66
        for cell_column in range(range_columns_start, range_columns_stop):
67
            cell_column_index = column_index + cell_column
68
            if (cell_column_index < 0 or cell_column_index >= size_columns
69
                    or orientation[cell_row_index, cell_column_index]
70
                    >= orientation_start
71
                    or orientation[cell_row_index, cell_column_index]
72
                    < orientation_end):
73
                continue
74

75
            total += magnitude[cell_row_index, cell_column_index]
76

77
    return total / (cell_rows * cell_columns)
78

79

80
def hog_histograms(np_floats[:, ::1] gradient_columns,
81
                   np_floats[:, ::1] gradient_rows,
82
                   int cell_columns, int cell_rows,
83
                   int size_columns, int size_rows,
84
                   int number_of_cells_columns, int number_of_cells_rows,
85
                   int number_of_orientations,
86
                   np_floats[:, :, ::1] orientation_histogram):
87
    """Extract Histogram of Oriented Gradients (HOG) for a given image.
88

89
    Parameters
90
    ----------
91
    gradient_columns : ndarray
92
        First order image gradients (rows).
93
    gradient_rows : ndarray
94
        First order image gradients (columns).
95
    cell_columns : int
96
        Pixels per cell (rows).
97
    cell_rows : int
98
        Pixels per cell (columns).
99
    size_columns : int
100
        Number of columns.
101
    size_rows : int
102
        Number of rows.
103
    number_of_cells_columns : int
104
        Number of cells (rows).
105
    number_of_cells_rows : int
106
        Number of cells (columns).
107
    number_of_orientations : int
108
        Number of orientation bins.
109
    orientation_histogram : ndarray
110
        The histogram array which is modified in place.
111
    """
112

113
    cdef np_floats[:, ::1] magnitude = np.hypot(gradient_columns,
114
                                                gradient_rows)
115
    cdef np_floats[:, ::1] orientation = \
116
        np.rad2deg(np.arctan2(gradient_rows, gradient_columns)) % 180
117
    cdef int i, c, r, r_i, c_i, cc, cr, c_0, r_0, \
118
        range_rows_start, range_rows_stop, \
119
        range_columns_start, range_columns_stop
120
    cdef np_floats orientation_start, orientation_end, \
121
        number_of_orientations_per_180
122

123
    r_0 = cell_rows / 2
124
    c_0 = cell_columns / 2
125
    cc = cell_rows * number_of_cells_rows
126
    cr = cell_columns * number_of_cells_columns
127
    range_rows_stop = (cell_rows + 1) / 2
128
    range_rows_start = -(cell_rows / 2)
129
    range_columns_stop = (cell_columns + 1) / 2
130
    range_columns_start = -(cell_columns / 2)
131
    number_of_orientations_per_180 = 180. / number_of_orientations
132

133
    with nogil:
134
        # compute orientations integral images
135
        for i in range(number_of_orientations):
136
            # isolate orientations in this range
137
            orientation_start = number_of_orientations_per_180 * (i + 1)
138
            orientation_end = number_of_orientations_per_180 * i
139
            c = c_0
140
            r = r_0
141
            r_i = 0
142
            c_i = 0
143

144
            while r < cc:
145
                c_i = 0
146
                c = c_0
147

148
                while c < cr:
149
                    orientation_histogram[r_i, c_i, i] = \
150
                        cell_hog(magnitude, orientation,
151
                                 orientation_start, orientation_end,
152
                                 cell_columns, cell_rows, c, r,
153
                                 size_columns, size_rows,
154
                                 range_rows_start, range_rows_stop,
155
                                 range_columns_start, range_columns_stop)
156
                    c_i += 1
157
                    c += cell_columns
158

159
                r_i += 1
160
                r += cell_rows
161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.