diff --git a/dlib/image_transforms/colormaps.h b/dlib/image_transforms/colormaps.h index 67552bc18..40d9c335c 100644 --- a/dlib/image_transforms/colormaps.h +++ b/dlib/image_transforms/colormaps.h @@ -63,6 +63,32 @@ namespace dlib return matrix_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(std::min(gray/0.4,1.0)*255 + 0.5); + + if (gray > 0.4) + { + pix.green = static_cast(std::min((gray-0.4)/0.4,1.0)*255 + 0.5); + } + if (gray > 0.8) + { + pix.blue = static_cast(std::min((gray-0.8)/0.2,1.0)*255 + 0.5); + } + + return pix; + } + // ---------------------------------------------------------------------------------------- template @@ -89,22 +115,7 @@ namespace dlib const_ret_type apply (long r, long c ) const { - // scale the gray value into the range [0, 1] - const double gray = put_in_range(0, 1, (get_pixel_intensity(mat(img)(r,c)) - min_val)/(max_val-min_val)); - rgb_pixel pix(0,0,0); - - pix.red = static_cast(std::min(gray/0.4,1.0)*255 + 0.5); - - if (gray > 0.4) - { - pix.green = static_cast(std::min((gray-0.4)/0.4,1.0)*255 + 0.5); - } - if (gray > 0.8) - { - pix.blue = static_cast(std::min((gray-0.8)/0.2,1.0)*255 + 0.5); - } - - return pix; + return colormap_heat(get_pixel_intensity(mat(img)(r,c)), min_val, max_val); } long nr () const { return num_rows(img); } @@ -137,6 +148,54 @@ namespace dlib return matrix_op(op(img,max(mat(img)),min(mat(img)))); } +// ---------------------------------------------------------------------------------------- + + 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((gray+1)*s*255 + 0.5); + } + else if (gray <= 3) + { + pix.red = 0; + pix.green = static_cast((gray-1)*s*255 + 0.5); + pix.blue = 255; + } + else if (gray <= 5) + { + pix.red = static_cast((gray-3)*s*255 + 0.5); + pix.green = 255; + pix.blue = static_cast((5-gray)*s*255 + 0.5); + } + else if (gray <= 7) + { + pix.red = 255; + pix.green = static_cast((7-gray)*s*255 + 0.5); + pix.blue = 0; + } + else + { + pix.red = static_cast((9-gray)*s*255 + 0.5); + pix.green = 0; + pix.blue = 0; + } + + return pix; + } + // ---------------------------------------------------------------------------------------- template @@ -163,44 +222,7 @@ namespace dlib const_ret_type apply (long r, long c ) const { - // scale the gray value into the range [0, 8] - const double gray = 8*put_in_range(0, 1, (get_pixel_intensity(mat(img)(r,c)) - 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((gray+1)*s*255 + 0.5); - } - else if (gray <= 3) - { - pix.red = 0; - pix.green = static_cast((gray-1)*s*255 + 0.5); - pix.blue = 255; - } - else if (gray <= 5) - { - pix.red = static_cast((gray-3)*s*255 + 0.5); - pix.green = 255; - pix.blue = static_cast((5-gray)*s*255 + 0.5); - } - else if (gray <= 7) - { - pix.red = 255; - pix.green = static_cast((7-gray)*s*255 + 0.5); - pix.blue = 0; - } - else - { - pix.red = static_cast((9-gray)*s*255 + 0.5); - pix.green = 0; - pix.blue = 0; - } - - return pix; + return colormap_jet(get_pixel_intensity(mat(img)(r,c)), min_val, max_val); } long nr () const { return num_rows(img); } diff --git a/dlib/image_transforms/colormaps_abstract.h b/dlib/image_transforms/colormaps_abstract.h index f47624877..cc85d90c7 100644 --- a/dlib/image_transforms/colormaps_abstract.h +++ b/dlib/image_transforms/colormaps_abstract.h @@ -37,6 +37,18 @@ namespace dlib // ---------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------- + rgb_pixel colormap_heat ( + double value, + double min_val, + double max_val + ); + /*! + ensures + - Maps value to a color. In particular, we use a heatmap color scheme where + values <= min_val are black and larger values become more red, then yellow, + and then white as they approach max_val. + !*/ + template < typename image_type > @@ -51,11 +63,9 @@ namespace dlib dlib/image_processing/generic_image.h, or something convertible to a matrix via mat(). ensures - - Interprets img as a grayscale image and returns a new matrix - which represents a colored version of img. In particular, the - colors will depict img using a heatmap where pixels with a - value <= min_val are black and larger pixel values become - more red, then yellow, and then white as they approach max_val. + - Interprets img as a grayscale image and returns a new matrix which represents + a colored version of img. In particular, the colormap is defined by + colormap_heat(). - The returned matrix will have the same dimensions as img. !*/ @@ -79,6 +89,18 @@ namespace dlib // ---------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------- + rgb_pixel colormap_jet ( + double value, + double min_val, + double max_val + ); + /*! + ensures + - Maps value to a color. In particular, we use a jet color scheme where + values <= min_val are dark blue and larger values become light blue, then + yellow, and then finally red as they approach max_val. + !*/ + template < typename image_type > @@ -94,10 +116,8 @@ namespace dlib via mat(). ensures - Interprets img as a grayscale image and returns a new matrix which represents - a colored version of img. In particular, the colors will depict img using a - jet color scheme where pixels with a value <= min_val are dark blue and - larger pixel values become light blue, then yellow, and then finally red as - they approach max_Val. + a colored version of img. In particular, the colormap is defined by + colormap_jet(). - The returned matrix will have the same dimensions as img. !*/