File: colormaps.h

package info (click to toggle)
dlib 19.10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 124,860 kB
  • sloc: cpp: 293,144; xml: 25,083; python: 1,452; sh: 324; java: 229; makefile: 181; perl: 18
file content (269 lines) | stat: -rw-r--r-- 7,577 bytes parent folder | download | duplicates (5)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_RANDOMLY_COlOR_IMAGE_Hh_
#define DLIB_RANDOMLY_COlOR_IMAGE_Hh_

#include "colormaps_abstract.h"
#include "../hash.h"
#include "../pixel.h"
#include "../matrix.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    template <typename T>
    struct op_randomly_color_image : does_not_alias 
    {
        op_randomly_color_image( const T& img_) : img(img_){}

        const T& img;

        const static long cost = 7;
        const static long NR = 0;
        const static long NC = 0;
        typedef rgb_pixel type;
        typedef const rgb_pixel const_ret_type;
        typedef default_memory_manager mem_manager_type;
        typedef row_major_layout layout_type;

        const_ret_type apply (long r, long c ) const 
        { 
            const unsigned long gray = get_pixel_intensity(mat(img)(r,c));
            if (gray != 0)
            {
                const uint32 h = murmur_hash3_2(gray,0);
                rgb_pixel pix;
                pix.red   = static_cast<unsigned char>(h)%200 + 55;
                pix.green = static_cast<unsigned char>(h>>8)%200 + 55;
                pix.blue  = static_cast<unsigned char>(h>>16)%200 + 55;
                return pix;
            }
            else
            {
                // keep black pixels black
                return rgb_pixel(0,0,0);
            }
        }

        long nr () const { return num_rows(img); }
        long nc () const { return num_columns(img); }
    }; 

    template <
        typename image_type
        >
    const matrix_op<op_randomly_color_image<image_type> >  
    randomly_color_image (
        const image_type& img
    )
    {
        typedef op_randomly_color_image<image_type> op;
        return matrix_op<op>(op(img));
    }

// ----------------------------------------------------------------------------------------

    inline rgb_pixel colormap_heat (
        double value,
        double min_val,
        double max_val
    )
    {
        // scale the gray value into the range [0, 1]
        const double gray = put_in_range(0, 1, (value - min_val)/(max_val-min_val));
        rgb_pixel pix(0,0,0);

        pix.red = static_cast<unsigned char>(std::min(gray/0.4,1.0)*255 + 0.5);

        if (gray > 0.4)
        {
            pix.green = static_cast<unsigned char>(std::min((gray-0.4)/0.4,1.0)*255 + 0.5);
        }
        if (gray > 0.8)
        {
            pix.blue = static_cast<unsigned char>(std::min((gray-0.8)/0.2,1.0)*255 + 0.5);
        }

        return pix;
    }

// ----------------------------------------------------------------------------------------

    template <typename T>
    struct op_heatmap : does_not_alias 
    {
        op_heatmap( 
            const T& img_,
            const double max_val_,
            const double min_val_
            ) : img(img_), max_val(max_val_), min_val(min_val_){}

        const T& img;

        const double max_val;
        const double min_val;

        const static long cost = 7;
        const static long NR = 0;
        const static long NC = 0;
        typedef rgb_pixel type;
        typedef const rgb_pixel const_ret_type;
        typedef default_memory_manager mem_manager_type;
        typedef row_major_layout layout_type;

        const_ret_type apply (long r, long c ) const 
        { 
            return colormap_heat(get_pixel_intensity(mat(img)(r,c)), min_val, max_val);
        }

        long nr () const { return num_rows(img); }
        long nc () const { return num_columns(img); }
    }; 

    template <
        typename image_type
        >
    const matrix_op<op_heatmap<image_type> >  
    heatmap (
        const image_type& img,
        double max_val,
        double min_val = 0
    )
    {
        typedef op_heatmap<image_type> op;
        return matrix_op<op>(op(img,max_val,min_val));
    }

    template <
        typename image_type
        >
    const matrix_op<op_heatmap<image_type> >  
    heatmap (
        const image_type& img
    )
    {
        typedef op_heatmap<image_type> op;
        if (num_columns(img) * num_rows(img) != 0)
            return matrix_op<op>(op(img,max(mat(img)),min(mat(img))));
        else
            return matrix_op<op>(op(img,0,0));
    }

// ----------------------------------------------------------------------------------------

    inline rgb_pixel colormap_jet (
        double value,
        double min_val,
        double max_val
    )
    {
        // scale the gray value into the range [0, 8]
        const double gray = 8*put_in_range(0, 1, (value - min_val)/(max_val-min_val));
        rgb_pixel pix;
        // s is the slope of color change
        const double s = 1.0/2.0;

        if (gray <= 1)
        {
            pix.red = 0;
            pix.green = 0;
            pix.blue = static_cast<unsigned char>((gray+1)*s*255 + 0.5);
        }
        else if (gray <= 3)
        {
            pix.red = 0;
            pix.green = static_cast<unsigned char>((gray-1)*s*255 + 0.5);
            pix.blue = 255;
        }
        else if (gray <= 5)
        {
            pix.red = static_cast<unsigned char>((gray-3)*s*255 + 0.5);
            pix.green = 255;
            pix.blue = static_cast<unsigned char>((5-gray)*s*255 + 0.5);
        }
        else if (gray <= 7)
        {
            pix.red = 255;
            pix.green = static_cast<unsigned char>((7-gray)*s*255 + 0.5);
            pix.blue = 0;
        }
        else
        {
            pix.red = static_cast<unsigned char>((9-gray)*s*255 + 0.5);
            pix.green = 0;
            pix.blue = 0;
        }

        return pix;
    }

// ----------------------------------------------------------------------------------------

    template <typename T>
    struct op_jet : does_not_alias 
    {
        op_jet( 
            const T& img_,
            const double max_val_,
            const double min_val_
            ) : img(img_), max_val(max_val_), min_val(min_val_){}

        const T& img;

        const double max_val;
        const double min_val;

        const static long cost = 7;
        const static long NR = 0;
        const static long NC = 0;
        typedef rgb_pixel type;
        typedef const rgb_pixel const_ret_type;
        typedef default_memory_manager mem_manager_type;
        typedef row_major_layout layout_type;

        const_ret_type apply (long r, long c ) const 
        { 
            return colormap_jet(get_pixel_intensity(mat(img)(r,c)), min_val, max_val);
        }

        long nr () const { return num_rows(img); }
        long nc () const { return num_columns(img); }
    }; 

    template <
        typename image_type
        >
    const matrix_op<op_jet<image_type> >  
    jet (
        const image_type& img,
        double max_val,
        double min_val = 0
    )
    {
        typedef op_jet<image_type> op;
        return matrix_op<op>(op(img,max_val,min_val));
    }

    template <
        typename image_type
        >
    const matrix_op<op_jet<image_type> >  
    jet (
        const image_type& img
    )
    {
        typedef op_jet<image_type> op;
        if (num_columns(img) * num_rows(img) != 0)
            return matrix_op<op>(op(img,max(mat(img)),min(mat(img))));
        else
            return matrix_op<op>(op(img,0,0));
    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_RANDOMLY_COlOR_IMAGE_Hh_