├── CMakeLists.txt ├── README.md ├── img └── lena.jpg ├── include └── colormap.hpp └── src ├── colormap.cpp └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( colormapscv ) 2 | cmake_minimum_required( VERSION 2.6 ) 3 | find_package( OpenCV REQUIRED ) 4 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) 5 | file(GLOB SRC_FILES "src/*.cpp") 6 | add_executable( cm_demo ${SRC_FILES} ) 7 | target_link_libraries( cm_demo opencv_core opencv_imgproc opencv_highgui) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Colormaps in OpenCV # 2 | 3 | ## Description ## 4 | 5 | This repository implements colormaps for use in OpenCV. This version now has the same interface as the colormaps in OpenCV 2.4+, so supporting multiple versions of OpenCV is easier. 6 | 7 | Currently the following GNU Octave/MATLAB equivalent colormaps are implemented: 8 | 9 | * Autumn 10 | * Bone 11 | * Cool 12 | * Hot 13 | * HSV 14 | * Jet 15 | * Ocean 16 | * Pink 17 | * Rainbow 18 | * Spring 19 | * Summer 20 | * Winter 21 | 22 | Feel free to fork the project and add your own colormaps. Please make a pull request to this branch then, so everybody can use your work. 23 | 24 | 25 | 26 | ## API ## 27 | 28 | You only need `applyColorMap` to apply a colormap on a given image: 29 | 30 | ```cpp 31 | void applyColorMap(InputArray src, OutputArray dst, int colormap) 32 | ``` 33 | 34 | The following sample code reads the path to an image from command line, applies a Jet colormap on it and shows the result: 35 | 36 | ```cpp 37 | #include 38 | #include 39 | #include "colormap.hpp" 40 | 41 | using namespace cv; 42 | 43 | int main(int argc, const char *argv[]) { 44 | // Get the path to the image, if it was given 45 | // if no arguments were given. 46 | string filename; 47 | if (argc > 1) { 48 | filename = string(argv[1]); 49 | } 50 | // The following lines show how to apply a colormap on a given image 51 | // and show it with cv::imshow example with an image. An exception is 52 | // thrown if the path to the image is invalid. 53 | if(!filename.empty()) { 54 | Mat img0 = imread(filename); 55 | // Throw an exception, if the image can't be read: 56 | if(img0.empty()) { 57 | CV_Error(CV_StsBadArg, "Sample image is empty. Please adjust your path, so it points to a valid input image!"); 58 | } 59 | // Holds the colormap version of the image: 60 | Mat cm_img0; 61 | // Apply the colormap: 62 | applyColorMap(img0, cm_img0, COLORMAP_JET); 63 | // Show the result: 64 | imshow("cm_img0", cm_img0); 65 | waitKey(0); 66 | } 67 | 68 | return 0; 69 | } 70 | ``` 71 | 72 | The available colormaps are: 73 | 74 | ```cpp 75 | enum 76 | { 77 | COLORMAP_AUTUMN = 0, 78 | COLORMAP_BONE = 1, 79 | COLORMAP_JET = 2, 80 | COLORMAP_WINTER = 3, 81 | COLORMAP_RAINBOW = 4, 82 | COLORMAP_OCEAN = 5, 83 | COLORMAP_SUMMER = 6, 84 | COLORMAP_SPRING = 7, 85 | COLORMAP_COOL = 8, 86 | COLORMAP_HSV = 9, 87 | COLORMAP_PINK = 10, 88 | COLORMAP_HOT = 11 89 | } 90 | ``` 91 | 92 | ## Demo ## 93 | 94 | Please have a look at the demo, to see the colormaps in action and learn how to use the colormaps. If you want to apply a Jet colormap on a given image, then call the demo with: 95 | 96 | ``` 97 | ./cm_demo /path/to/your/image.ext 98 | ``` 99 | 100 | Or if you just want to generate the color scales, then call the demo with: 101 | 102 | 103 | ``` 104 | ./cm_demo 105 | ``` 106 | 107 | (Replace ./cm_demo with cm_demo.exe if you are running Windows!) 108 | 109 | ## Building the demo ## 110 | 111 | The project comes as a CMake project, so building it is as simple as: 112 | 113 | ``` 114 | philipp@mango:~/some/dir/colormaps-opencv$ mkdir build 115 | philipp@mango:~/some/dir/colormaps-opencv$ cd build 116 | philipp@mango:~/some/dir/colormaps-opencv/build$ cmake .. 117 | philipp@mango:~/some/dir/colormaps-opencv/build$ make 118 | philipp@mango:~/some/dir/colormaps-opencv/build$ ./cm_demo path/to/your/image.ext 119 | ``` 120 | 121 | Or if you use MinGW on Windows: 122 | 123 | ``` 124 | C:\some\dir\colormaps-opencv> mkdir build 125 | C:\some\dir\colormaps-opencv> cd build 126 | C:\some\dir\colormaps-opencv\build> cmake -G "MinGW Makefiles" .. 127 | C:\some\dir\colormaps-opencv\build> mingw32-make 128 | C:\some\dir\colormaps-opencv\build> cm_demo.exe /path/to/your/image.ext 129 | ``` 130 | 131 | Or if you are using Visual Studio, you can generate yourself a solution like this (please adjust to your version): 132 | 133 | ``` 134 | C:\some\dir\colormaps-opencv> mkdir build 135 | C:\some\dir\colormaps-opencv> cd build 136 | C:\some\dir\colormaps-opencv\build> cmake -G "Visual Studio 9 2008" .. 137 | ``` 138 | -------------------------------------------------------------------------------- /img/lena.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bytefish/colormaps-opencv/08a7a4f506c2187490ccea6a63f56ed3d24a9a8c/img/lena.jpg -------------------------------------------------------------------------------- /include/colormap.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011. Philipp Wagner . 3 | * Released to public domain under terms of the BSD Simplified license. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the organization nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software 14 | * without specific prior written permission. 15 | * 16 | * See 17 | */ 18 | #ifndef OPENCV_COLORMAP_HPP_ 19 | #define OPENCV_COLORMAP_HPP_ 20 | 21 | #include "opencv2/core/core.hpp" 22 | 23 | namespace cv 24 | { 25 | enum 26 | { 27 | COLORMAP_AUTUMN = 0, 28 | COLORMAP_BONE = 1, 29 | COLORMAP_JET = 2, 30 | COLORMAP_WINTER = 3, 31 | COLORMAP_RAINBOW = 4, 32 | COLORMAP_OCEAN = 5, 33 | COLORMAP_SUMMER = 6, 34 | COLORMAP_SPRING = 7, 35 | COLORMAP_COOL = 8, 36 | COLORMAP_HSV = 9, 37 | COLORMAP_PINK = 10, 38 | COLORMAP_HOT = 11 39 | }; 40 | 41 | /** 42 | * Applies a colormap to the image given in src and returns the 43 | * colormap version in dst. Note: Mat::create is called on dst! 44 | * 45 | * The available colormaps are: 46 | * 47 | * COLORMAP_AUTUMN = 0, 48 | * COLORMAP_BONE = 1, 49 | * COLORMAP_JET = 2, 50 | * COLORMAP_WINTER = 3, 51 | * COLORMAP_RAINBOW = 4, 52 | * COLORMAP_OCEAN = 5, 53 | * COLORMAP_SUMMER = 6, 54 | * COLORMAP_SPRING = 7, 55 | * COLORMAP_COOL = 8, 56 | * COLORMAP_HSV = 9, 57 | * COLORMAP_PINK = 10, 58 | * COLORMAP_HOT = 11 59 | * 60 | */ 61 | CV_EXPORTS void applyColorMap(InputArray src, OutputArray dst, int colormap); 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/colormap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011. Philipp Wagner . 3 | * Released to public domain under terms of the BSD Simplified license. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the organization nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software 14 | * without specific prior written permission. 15 | * 16 | * See 17 | */ 18 | #include "colormap.hpp" 19 | #include "opencv2/imgproc/imgproc.hpp" 20 | 21 | #ifdef _MSC_VER 22 | #pragma warning( disable: 4305 ) 23 | #endif 24 | 25 | namespace cv 26 | { 27 | 28 | //------------------------------------------------------------------------------ 29 | // cv::sortMatrixRowsByIndices 30 | //------------------------------------------------------------------------------ 31 | static Mat linspace(float x0, float x1, int n) 32 | { 33 | Mat pts(n, 1, CV_32FC1); 34 | float step = (x1-x0)/(n-1); 35 | for(int i = 0; i < n; i++) 36 | pts.at(i,0) = x0+i*step; 37 | return pts; 38 | } 39 | 40 | //------------------------------------------------------------------------------ 41 | // cv::sortMatrixRowsByIndices 42 | //------------------------------------------------------------------------------ 43 | static void sortMatrixRowsByIndices(InputArray _src, InputArray _indices, OutputArray _dst) 44 | { 45 | if(_indices.getMat().type() != CV_32SC1) 46 | CV_Error(CV_StsUnsupportedFormat, "cv::sortRowsByIndices only works on integer indices!"); 47 | Mat src = _src.getMat(); 48 | vector indices = _indices.getMat(); 49 | _dst.create(src.rows, src.cols, src.type()); 50 | Mat dst = _dst.getMat(); 51 | for(size_t idx = 0; idx < indices.size(); idx++) { 52 | Mat originalRow = src.row(indices[idx]); 53 | Mat sortedRow = dst.row((int)idx); 54 | originalRow.copyTo(sortedRow); 55 | } 56 | } 57 | 58 | static Mat sortMatrixRowsByIndices(InputArray src, InputArray indices) 59 | { 60 | Mat dst; 61 | sortMatrixRowsByIndices(src, indices, dst); 62 | return dst; 63 | } 64 | 65 | 66 | static Mat argsort(InputArray _src, bool ascending=true) 67 | { 68 | Mat src = _src.getMat(); 69 | if (src.rows != 1 && src.cols != 1) 70 | CV_Error(CV_StsBadArg, "cv::argsort only sorts 1D matrices."); 71 | int flags = CV_SORT_EVERY_ROW+(ascending ? CV_SORT_ASCENDING : CV_SORT_DESCENDING); 72 | Mat sorted_indices; 73 | sortIdx(src.reshape(1,1),sorted_indices,flags); 74 | return sorted_indices; 75 | } 76 | 77 | template static 78 | Mat interp1_(const Mat& X_, const Mat& Y_, const Mat& XI) 79 | { 80 | int n = XI.rows; 81 | // sort input table 82 | vector sort_indices = argsort(X_); 83 | 84 | Mat X = sortMatrixRowsByIndices(X_,sort_indices); 85 | Mat Y = sortMatrixRowsByIndices(Y_,sort_indices); 86 | // interpolated values 87 | Mat yi = Mat::zeros(XI.size(), XI.type()); 88 | for(int i = 0; i < n; i++) { 89 | int c = 0; 90 | int low = 0; 91 | int high = X.rows - 1; 92 | // set bounds 93 | if(XI.at<_Tp>(i,0) < X.at<_Tp>(low, 0)) 94 | high = 1; 95 | if(XI.at<_Tp>(i,0) > X.at<_Tp>(high, 0)) 96 | low = high - 1; 97 | // binary search 98 | while((high-low)>1) { 99 | c = low + ((high - low) >> 1); 100 | if(XI.at<_Tp>(i,0) > X.at<_Tp>(c,0)) { 101 | low = c; 102 | } else { 103 | high = c; 104 | } 105 | } 106 | // linear interpolation 107 | yi.at<_Tp>(i,0) += Y.at<_Tp>(low,0) 108 | + (XI.at<_Tp>(i,0) - X.at<_Tp>(low,0)) 109 | * (Y.at<_Tp>(high,0) - Y.at<_Tp>(low,0)) 110 | / (X.at<_Tp>(high,0) - X.at<_Tp>(low,0)); 111 | } 112 | return yi; 113 | } 114 | 115 | static Mat interp1(InputArray _x, InputArray _Y, InputArray _xi) 116 | { 117 | // get matrices 118 | Mat x = _x.getMat(); 119 | Mat Y = _Y.getMat(); 120 | Mat xi = _xi.getMat(); 121 | // check types & alignment 122 | assert((x.type() == Y.type()) && (Y.type() == xi.type())); 123 | assert((x.cols == 1) && (x.rows == Y.rows) && (x.cols == Y.cols)); 124 | // call templated interp1 125 | switch(x.type()) { 126 | case CV_8SC1: return interp1_(x,Y,xi); break; 127 | case CV_8UC1: return interp1_(x,Y,xi); break; 128 | case CV_16SC1: return interp1_(x,Y,xi); break; 129 | case CV_16UC1: return interp1_(x,Y,xi); break; 130 | case CV_32SC1: return interp1_(x,Y,xi); break; 131 | case CV_32FC1: return interp1_(x,Y,xi); break; 132 | case CV_64FC1: return interp1_(x,Y,xi); break; 133 | default: CV_Error(CV_StsUnsupportedFormat, ""); break; 134 | } 135 | return Mat(); 136 | } 137 | 138 | namespace colormap 139 | { 140 | 141 | class ColorMap { 142 | 143 | protected: 144 | Mat _lut; 145 | 146 | public: 147 | virtual ~ColorMap() {} 148 | 149 | // Applies the colormap on a given image. 150 | // 151 | // This function expects BGR-aligned data of type CV_8UC1 or 152 | // CV_8UC3. If the wrong image type is given, the original image 153 | // will be returned. 154 | // 155 | // Throws an error for wrong-aligned lookup table, which must be 156 | // of size 256 in the latest OpenCV release (2.3.1). 157 | void operator()(InputArray src, OutputArray dst) const; 158 | 159 | // Setup base map to interpolate from. 160 | virtual void init(int n) = 0; 161 | 162 | // Interpolates from a base colormap. 163 | static Mat linear_colormap(InputArray X, 164 | InputArray r, InputArray g, InputArray b, 165 | int n) { 166 | return linear_colormap(X,r,g,b,linspace(0,1,n)); 167 | } 168 | 169 | // Interpolates from a base colormap. 170 | static Mat linear_colormap(InputArray X, 171 | InputArray r, InputArray g, InputArray b, 172 | float begin, float end, float n) { 173 | return linear_colormap(X,r,g,b,linspace(begin,end, cvRound(n))); 174 | } 175 | 176 | // Interpolates from a base colormap. 177 | static Mat linear_colormap(InputArray X, 178 | InputArray r, InputArray g, InputArray b, 179 | InputArray xi); 180 | }; 181 | 182 | // Equals the GNU Octave colormap "autumn". 183 | class Autumn : public ColorMap { 184 | public: 185 | Autumn() : ColorMap() { 186 | init(256); 187 | } 188 | 189 | Autumn(int n) : ColorMap() { 190 | init(n); 191 | } 192 | 193 | void init(int n) { 194 | float r[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 195 | float g[] = { 0, 0.01587301587301587, 0.03174603174603174, 0.04761904761904762, 0.06349206349206349, 0.07936507936507936, 0.09523809523809523, 0.1111111111111111, 0.126984126984127, 0.1428571428571428, 0.1587301587301587, 0.1746031746031746, 0.1904761904761905, 0.2063492063492063, 0.2222222222222222, 0.2380952380952381, 0.253968253968254, 0.2698412698412698, 0.2857142857142857, 0.3015873015873016, 0.3174603174603174, 0.3333333333333333, 0.3492063492063492, 0.3650793650793651, 0.3809523809523809, 0.3968253968253968, 0.4126984126984127, 0.4285714285714285, 0.4444444444444444, 0.4603174603174603, 0.4761904761904762, 0.492063492063492, 0.5079365079365079, 0.5238095238095238, 0.5396825396825397, 0.5555555555555556, 0.5714285714285714, 0.5873015873015873, 0.6031746031746031, 0.6190476190476191, 0.6349206349206349, 0.6507936507936508, 0.6666666666666666, 0.6825396825396826, 0.6984126984126984, 0.7142857142857143, 0.7301587301587301, 0.746031746031746, 0.7619047619047619, 0.7777777777777778, 0.7936507936507936, 0.8095238095238095, 0.8253968253968254, 0.8412698412698413, 0.8571428571428571, 0.873015873015873, 0.8888888888888888, 0.9047619047619048, 0.9206349206349206, 0.9365079365079365, 0.9523809523809523, 0.9682539682539683, 0.9841269841269841, 1}; 196 | float b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 197 | Mat X = linspace(0,1,64); 198 | this->_lut = ColorMap::linear_colormap(X, 199 | Mat(64,1, CV_32FC1, r).clone(), // red 200 | Mat(64,1, CV_32FC1, g).clone(), // green 201 | Mat(64,1, CV_32FC1, b).clone(), // blue 202 | n); // number of sample points 203 | } 204 | }; 205 | 206 | // Equals the GNU Octave colormap "bone". 207 | class Bone : public ColorMap { 208 | public: 209 | Bone() : ColorMap() { 210 | init(256); 211 | } 212 | 213 | Bone(int n) : ColorMap() { 214 | init(n); 215 | } 216 | 217 | void init(int n) { 218 | float r[] = { 0, 0.01388888888888889, 0.02777777777777778, 0.04166666666666666, 0.05555555555555555, 0.06944444444444445, 0.08333333333333333, 0.09722222222222221, 0.1111111111111111, 0.125, 0.1388888888888889, 0.1527777777777778, 0.1666666666666667, 0.1805555555555556, 0.1944444444444444, 0.2083333333333333, 0.2222222222222222, 0.2361111111111111, 0.25, 0.2638888888888889, 0.2777777777777778, 0.2916666666666666, 0.3055555555555555, 0.3194444444444444, 0.3333333333333333, 0.3472222222222222, 0.3611111111111111, 0.375, 0.3888888888888888, 0.4027777777777777, 0.4166666666666666, 0.4305555555555555, 0.4444444444444444, 0.4583333333333333, 0.4722222222222222, 0.4861111111111112, 0.5, 0.5138888888888888, 0.5277777777777778, 0.5416666666666667, 0.5555555555555556, 0.5694444444444444, 0.5833333333333333, 0.5972222222222222, 0.611111111111111, 0.6249999999999999, 0.6388888888888888, 0.6527777777777778, 0.6726190476190474, 0.6944444444444442, 0.7162698412698412, 0.7380952380952381, 0.7599206349206349, 0.7817460317460316, 0.8035714285714286, 0.8253968253968254, 0.8472222222222221, 0.8690476190476188, 0.8908730158730158, 0.9126984126984128, 0.9345238095238095, 0.9563492063492063, 0.978174603174603, 1}; 219 | float g[] = { 0, 0.01388888888888889, 0.02777777777777778, 0.04166666666666666, 0.05555555555555555, 0.06944444444444445, 0.08333333333333333, 0.09722222222222221, 0.1111111111111111, 0.125, 0.1388888888888889, 0.1527777777777778, 0.1666666666666667, 0.1805555555555556, 0.1944444444444444, 0.2083333333333333, 0.2222222222222222, 0.2361111111111111, 0.25, 0.2638888888888889, 0.2777777777777778, 0.2916666666666666, 0.3055555555555555, 0.3194444444444444, 0.3353174603174602, 0.3544973544973544, 0.3736772486772486, 0.3928571428571428, 0.412037037037037, 0.4312169312169312, 0.4503968253968254, 0.4695767195767195, 0.4887566137566137, 0.5079365079365078, 0.5271164021164021, 0.5462962962962963, 0.5654761904761904, 0.5846560846560845, 0.6038359788359787, 0.623015873015873, 0.6421957671957671, 0.6613756613756612, 0.6805555555555555, 0.6997354497354497, 0.7189153439153438, 0.7380952380952379, 0.7572751322751322, 0.7764550264550264, 0.7916666666666666, 0.8055555555555555, 0.8194444444444444, 0.8333333333333334, 0.8472222222222222, 0.861111111111111, 0.875, 0.8888888888888888, 0.9027777777777777, 0.9166666666666665, 0.9305555555555555, 0.9444444444444444, 0.9583333333333333, 0.9722222222222221, 0.986111111111111, 1}; 220 | float b[] = { 0, 0.01917989417989418, 0.03835978835978836, 0.05753968253968253, 0.07671957671957672, 0.09589947089947089, 0.1150793650793651, 0.1342592592592592, 0.1534391534391534, 0.1726190476190476, 0.1917989417989418, 0.210978835978836, 0.2301587301587301, 0.2493386243386243, 0.2685185185185185, 0.2876984126984127, 0.3068783068783069, 0.326058201058201, 0.3452380952380952, 0.3644179894179894, 0.3835978835978835, 0.4027777777777777, 0.4219576719576719, 0.4411375661375661, 0.4583333333333333, 0.4722222222222222, 0.4861111111111111, 0.5, 0.5138888888888888, 0.5277777777777777, 0.5416666666666666, 0.5555555555555556, 0.5694444444444444, 0.5833333333333333, 0.5972222222222222, 0.6111111111111112, 0.625, 0.6388888888888888, 0.6527777777777778, 0.6666666666666667, 0.6805555555555556, 0.6944444444444444, 0.7083333333333333, 0.7222222222222222, 0.736111111111111, 0.7499999999999999, 0.7638888888888888, 0.7777777777777778, 0.7916666666666666, 0.8055555555555555, 0.8194444444444444, 0.8333333333333334, 0.8472222222222222, 0.861111111111111, 0.875, 0.8888888888888888, 0.9027777777777777, 0.9166666666666665, 0.9305555555555555, 0.9444444444444444, 0.9583333333333333, 0.9722222222222221, 0.986111111111111, 1}; 221 | Mat X = linspace(0,1,64); 222 | this->_lut = ColorMap::linear_colormap(X, 223 | Mat(64,1, CV_32FC1, r).clone(), // red 224 | Mat(64,1, CV_32FC1, g).clone(), // green 225 | Mat(64,1, CV_32FC1, b).clone(), // blue 226 | n); // number of sample points 227 | } 228 | }; 229 | 230 | 231 | 232 | 233 | // Equals the GNU Octave colormap "jet". 234 | class Jet : public ColorMap { 235 | 236 | public: 237 | Jet() { 238 | init(256); 239 | } 240 | Jet(int n) : ColorMap() { 241 | init(n); 242 | } 243 | 244 | void init(int n) { 245 | // breakpoints 246 | Mat X = linspace(0,1,256); 247 | // define the basemap 248 | float r[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.00588235294117645,0.02156862745098032,0.03725490196078418,0.05294117647058827,0.06862745098039214,0.084313725490196,0.1000000000000001,0.115686274509804,0.1313725490196078,0.1470588235294117,0.1627450980392156,0.1784313725490196,0.1941176470588235,0.2098039215686274,0.2254901960784315,0.2411764705882353,0.2568627450980392,0.2725490196078431,0.2882352941176469,0.303921568627451,0.3196078431372549,0.3352941176470587,0.3509803921568628,0.3666666666666667,0.3823529411764706,0.3980392156862744,0.4137254901960783,0.4294117647058824,0.4450980392156862,0.4607843137254901,0.4764705882352942,0.4921568627450981,0.5078431372549019,0.5235294117647058,0.5392156862745097,0.5549019607843135,0.5705882352941174,0.5862745098039217,0.6019607843137256,0.6176470588235294,0.6333333333333333,0.6490196078431372,0.664705882352941,0.6803921568627449,0.6960784313725492,0.7117647058823531,0.7274509803921569,0.7431372549019608,0.7588235294117647,0.7745098039215685,0.7901960784313724,0.8058823529411763,0.8215686274509801,0.8372549019607844,0.8529411764705883,0.8686274509803922,0.884313725490196,0.8999999999999999,0.9156862745098038,0.9313725490196076,0.947058823529412,0.9627450980392158,0.9784313725490197,0.9941176470588236,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.9862745098039216,0.9705882352941178,0.9549019607843139,0.93921568627451,0.9235294117647062,0.9078431372549018,0.892156862745098,0.8764705882352941,0.8607843137254902,0.8450980392156864,0.8294117647058825,0.8137254901960786,0.7980392156862743,0.7823529411764705,0.7666666666666666,0.7509803921568627,0.7352941176470589,0.719607843137255,0.7039215686274511,0.6882352941176473,0.6725490196078434,0.6568627450980391,0.6411764705882352,0.6254901960784314,0.6098039215686275,0.5941176470588236,0.5784313725490198,0.5627450980392159,0.5470588235294116,0.5313725490196077,0.5156862745098039,0.5}; 249 | float g[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.001960784313725483,0.01764705882352935,0.03333333333333333,0.0490196078431373,0.06470588235294117,0.08039215686274503,0.09607843137254901,0.111764705882353,0.1274509803921569,0.1431372549019607,0.1588235294117647,0.1745098039215687,0.1901960784313725,0.2058823529411764,0.2215686274509804,0.2372549019607844,0.2529411764705882,0.2686274509803921,0.2843137254901961,0.3,0.3156862745098039,0.3313725490196078,0.3470588235294118,0.3627450980392157,0.3784313725490196,0.3941176470588235,0.4098039215686274,0.4254901960784314,0.4411764705882353,0.4568627450980391,0.4725490196078431,0.4882352941176471,0.503921568627451,0.5196078431372548,0.5352941176470587,0.5509803921568628,0.5666666666666667,0.5823529411764705,0.5980392156862746,0.6137254901960785,0.6294117647058823,0.6450980392156862,0.6607843137254901,0.6764705882352942,0.692156862745098,0.7078431372549019,0.723529411764706,0.7392156862745098,0.7549019607843137,0.7705882352941176,0.7862745098039214,0.8019607843137255,0.8176470588235294,0.8333333333333333,0.8490196078431373,0.8647058823529412,0.8803921568627451,0.8960784313725489,0.9117647058823528,0.9274509803921569,0.9431372549019608,0.9588235294117646,0.9745098039215687,0.9901960784313726,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.9901960784313726,0.9745098039215687,0.9588235294117649,0.943137254901961,0.9274509803921571,0.9117647058823528,0.8960784313725489,0.8803921568627451,0.8647058823529412,0.8490196078431373,0.8333333333333335,0.8176470588235296,0.8019607843137253,0.7862745098039214,0.7705882352941176,0.7549019607843137,0.7392156862745098,0.723529411764706,0.7078431372549021,0.6921568627450982,0.6764705882352944,0.6607843137254901,0.6450980392156862,0.6294117647058823,0.6137254901960785,0.5980392156862746,0.5823529411764707,0.5666666666666669,0.5509803921568626,0.5352941176470587,0.5196078431372548,0.503921568627451,0.4882352941176471,0.4725490196078432,0.4568627450980394,0.4411764705882355,0.4254901960784316,0.4098039215686273,0.3941176470588235,0.3784313725490196,0.3627450980392157,0.3470588235294119,0.331372549019608,0.3156862745098041,0.2999999999999998,0.284313725490196,0.2686274509803921,0.2529411764705882,0.2372549019607844,0.2215686274509805,0.2058823529411766,0.1901960784313728,0.1745098039215689,0.1588235294117646,0.1431372549019607,0.1274509803921569,0.111764705882353,0.09607843137254912,0.08039215686274526,0.06470588235294139,0.04901960784313708,0.03333333333333321,0.01764705882352935,0.001960784313725483,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 250 | float b[] = {0.5,0.5156862745098039,0.5313725490196078,0.5470588235294118,0.5627450980392157,0.5784313725490196,0.5941176470588235,0.6098039215686275,0.6254901960784314,0.6411764705882352,0.6568627450980392,0.6725490196078432,0.6882352941176471,0.7039215686274509,0.7196078431372549,0.7352941176470589,0.7509803921568627,0.7666666666666666,0.7823529411764706,0.7980392156862746,0.8137254901960784,0.8294117647058823,0.8450980392156863,0.8607843137254902,0.8764705882352941,0.892156862745098,0.907843137254902,0.9235294117647059,0.9392156862745098,0.9549019607843137,0.9705882352941176,0.9862745098039216,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.9941176470588236,0.9784313725490197,0.9627450980392158,0.9470588235294117,0.9313725490196079,0.915686274509804,0.8999999999999999,0.884313725490196,0.8686274509803922,0.8529411764705883,0.8372549019607844,0.8215686274509804,0.8058823529411765,0.7901960784313726,0.7745098039215685,0.7588235294117647,0.7431372549019608,0.7274509803921569,0.7117647058823531,0.696078431372549,0.6803921568627451,0.6647058823529413,0.6490196078431372,0.6333333333333333,0.6176470588235294,0.6019607843137256,0.5862745098039217,0.5705882352941176,0.5549019607843138,0.5392156862745099,0.5235294117647058,0.5078431372549019,0.4921568627450981,0.4764705882352942,0.4607843137254903,0.4450980392156865,0.4294117647058826,0.4137254901960783,0.3980392156862744,0.3823529411764706,0.3666666666666667,0.3509803921568628,0.335294117647059,0.3196078431372551,0.3039215686274508,0.2882352941176469,0.2725490196078431,0.2568627450980392,0.2411764705882353,0.2254901960784315,0.2098039215686276,0.1941176470588237,0.1784313725490199,0.1627450980392156,0.1470588235294117,0.1313725490196078,0.115686274509804,0.1000000000000001,0.08431372549019622,0.06862745098039236,0.05294117647058805,0.03725490196078418,0.02156862745098032,0.00588235294117645,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 251 | // now build lookup table 252 | this->_lut = ColorMap::linear_colormap(X, 253 | Mat(256,1, CV_32FC1, r).clone(), // red 254 | Mat(256,1, CV_32FC1, g).clone(), // green 255 | Mat(256,1, CV_32FC1, b).clone(), // blue 256 | n); 257 | } 258 | }; 259 | 260 | // Equals the GNU Octave colormap "winter". 261 | class Winter : public ColorMap { 262 | public: 263 | Winter() : ColorMap() { 264 | init(256); 265 | } 266 | 267 | Winter(int n) : ColorMap() { 268 | init(n); 269 | } 270 | 271 | void init(int n) { 272 | float r[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; 273 | float g[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0}; 274 | float b[] = {1.0, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.65, 0.6, 0.55, 0.5}; 275 | Mat X = linspace(0,1,11); 276 | this->_lut = ColorMap::linear_colormap(X, 277 | Mat(11,1, CV_32FC1, r).clone(), // red 278 | Mat(11,1, CV_32FC1, g).clone(), // green 279 | Mat(11,1, CV_32FC1, b).clone(), // blue 280 | n); // number of sample points 281 | } 282 | }; 283 | 284 | // Equals the GNU Octave colormap "rainbow". 285 | class Rainbow : public ColorMap { 286 | public: 287 | Rainbow() : ColorMap() { 288 | init(256); 289 | } 290 | 291 | Rainbow(int n) : ColorMap() { 292 | init(n); 293 | } 294 | 295 | void init(int n) { 296 | float r[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9365079365079367, 0.8571428571428572, 0.7777777777777777, 0.6984126984126986, 0.6190476190476191, 0.53968253968254, 0.4603174603174605, 0.3809523809523814, 0.3015873015873018, 0.2222222222222223, 0.1428571428571432, 0.06349206349206415, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03174603174603208, 0.08465608465608465, 0.1375661375661377, 0.1904761904761907, 0.2433862433862437, 0.2962962962962963, 0.3492063492063493, 0.4021164021164023, 0.4550264550264553, 0.5079365079365079, 0.5608465608465609, 0.6137566137566139, 0.666666666666667}; 297 | float g[] = { 0, 0.03968253968253968, 0.07936507936507936, 0.119047619047619, 0.1587301587301587, 0.1984126984126984, 0.2380952380952381, 0.2777777777777778, 0.3174603174603174, 0.3571428571428571, 0.3968253968253968, 0.4365079365079365, 0.4761904761904762, 0.5158730158730158, 0.5555555555555556, 0.5952380952380952, 0.6349206349206349, 0.6746031746031745, 0.7142857142857142, 0.753968253968254, 0.7936507936507936, 0.8333333333333333, 0.873015873015873, 0.9126984126984127, 0.9523809523809523, 0.992063492063492, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9841269841269842, 0.9047619047619047, 0.8253968253968256, 0.7460317460317465, 0.666666666666667, 0.587301587301587, 0.5079365079365079, 0.4285714285714288, 0.3492063492063493, 0.2698412698412698, 0.1904761904761907, 0.1111111111111116, 0.03174603174603208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 298 | float b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01587301587301582, 0.09523809523809534, 0.1746031746031744, 0.2539682539682535, 0.333333333333333, 0.412698412698413, 0.4920634920634921, 0.5714285714285712, 0.6507936507936507, 0.7301587301587302, 0.8095238095238093, 0.8888888888888884, 0.9682539682539679, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 299 | Mat X = linspace(0,1,64); 300 | this->_lut = ColorMap::linear_colormap(X, 301 | Mat(64,1, CV_32FC1, r).clone(), // red 302 | Mat(64,1, CV_32FC1, g).clone(), // green 303 | Mat(64,1, CV_32FC1, b).clone(), // blue 304 | n); // number of sample points 305 | } 306 | }; 307 | 308 | // Equals the GNU Octave colormap "ocean". 309 | class Ocean : public ColorMap { 310 | public: 311 | Ocean() : ColorMap() { 312 | init(256); 313 | } 314 | 315 | Ocean(int n) : ColorMap() { 316 | init(n); 317 | } 318 | 319 | void init(int n) { 320 | float r[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.04761904761904762, 0.09523809523809523, 0.1428571428571428, 0.1904761904761905, 0.2380952380952381, 0.2857142857142857, 0.3333333333333333, 0.3809523809523809, 0.4285714285714285, 0.4761904761904762, 0.5238095238095238, 0.5714285714285714, 0.6190476190476191, 0.6666666666666666, 0.7142857142857143, 0.7619047619047619, 0.8095238095238095, 0.8571428571428571, 0.9047619047619048, 0.9523809523809523, 1}; 321 | float g[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02380952380952381, 0.04761904761904762, 0.07142857142857142, 0.09523809523809523, 0.119047619047619, 0.1428571428571428, 0.1666666666666667, 0.1904761904761905, 0.2142857142857143, 0.2380952380952381, 0.2619047619047619, 0.2857142857142857, 0.3095238095238095, 0.3333333333333333, 0.3571428571428572, 0.3809523809523809, 0.4047619047619048, 0.4285714285714285, 0.4523809523809524, 0.4761904761904762, 0.5, 0.5238095238095238, 0.5476190476190477, 0.5714285714285714, 0.5952380952380952, 0.6190476190476191, 0.6428571428571429, 0.6666666666666666, 0.6904761904761905, 0.7142857142857143, 0.7380952380952381, 0.7619047619047619, 0.7857142857142857, 0.8095238095238095, 0.8333333333333334, 0.8571428571428571, 0.8809523809523809, 0.9047619047619048, 0.9285714285714286, 0.9523809523809523, 0.9761904761904762, 1}; 322 | float b[] = { 0, 0.01587301587301587, 0.03174603174603174, 0.04761904761904762, 0.06349206349206349, 0.07936507936507936, 0.09523809523809523, 0.1111111111111111, 0.126984126984127, 0.1428571428571428, 0.1587301587301587, 0.1746031746031746, 0.1904761904761905, 0.2063492063492063, 0.2222222222222222, 0.2380952380952381, 0.253968253968254, 0.2698412698412698, 0.2857142857142857, 0.3015873015873016, 0.3174603174603174, 0.3333333333333333, 0.3492063492063492, 0.3650793650793651, 0.3809523809523809, 0.3968253968253968, 0.4126984126984127, 0.4285714285714285, 0.4444444444444444, 0.4603174603174603, 0.4761904761904762, 0.492063492063492, 0.5079365079365079, 0.5238095238095238, 0.5396825396825397, 0.5555555555555556, 0.5714285714285714, 0.5873015873015873, 0.6031746031746031, 0.6190476190476191, 0.6349206349206349, 0.6507936507936508, 0.6666666666666666, 0.6825396825396826, 0.6984126984126984, 0.7142857142857143, 0.7301587301587301, 0.746031746031746, 0.7619047619047619, 0.7777777777777778, 0.7936507936507936, 0.8095238095238095, 0.8253968253968254, 0.8412698412698413, 0.8571428571428571, 0.873015873015873, 0.8888888888888888, 0.9047619047619048, 0.9206349206349206, 0.9365079365079365, 0.9523809523809523, 0.9682539682539683, 0.9841269841269841, 1}; 323 | Mat X = linspace(0,1,64); 324 | this->_lut = ColorMap::linear_colormap(X, 325 | Mat(64,1, CV_32FC1, r).clone(), // red 326 | Mat(64,1, CV_32FC1, g).clone(), // green 327 | Mat(64,1, CV_32FC1, b).clone(), // blue 328 | n); // number of sample points 329 | } 330 | }; 331 | 332 | // Equals the GNU Octave colormap "summer". 333 | class Summer : public ColorMap { 334 | public: 335 | Summer() : ColorMap() { 336 | init(256); 337 | } 338 | 339 | Summer(int n) : ColorMap() { 340 | init(n); 341 | } 342 | 343 | void init(int n) { 344 | float r[] = { 0, 0.01587301587301587, 0.03174603174603174, 0.04761904761904762, 0.06349206349206349, 0.07936507936507936, 0.09523809523809523, 0.1111111111111111, 0.126984126984127, 0.1428571428571428, 0.1587301587301587, 0.1746031746031746, 0.1904761904761905, 0.2063492063492063, 0.2222222222222222, 0.2380952380952381, 0.253968253968254, 0.2698412698412698, 0.2857142857142857, 0.3015873015873016, 0.3174603174603174, 0.3333333333333333, 0.3492063492063492, 0.3650793650793651, 0.3809523809523809, 0.3968253968253968, 0.4126984126984127, 0.4285714285714285, 0.4444444444444444, 0.4603174603174603, 0.4761904761904762, 0.492063492063492, 0.5079365079365079, 0.5238095238095238, 0.5396825396825397, 0.5555555555555556, 0.5714285714285714, 0.5873015873015873, 0.6031746031746031, 0.6190476190476191, 0.6349206349206349, 0.6507936507936508, 0.6666666666666666, 0.6825396825396826, 0.6984126984126984, 0.7142857142857143, 0.7301587301587301, 0.746031746031746, 0.7619047619047619, 0.7777777777777778, 0.7936507936507936, 0.8095238095238095, 0.8253968253968254, 0.8412698412698413, 0.8571428571428571, 0.873015873015873, 0.8888888888888888, 0.9047619047619048, 0.9206349206349206, 0.9365079365079365, 0.9523809523809523, 0.9682539682539683, 0.9841269841269841, 1}; 345 | float g[] = { 0.5, 0.5079365079365079, 0.5158730158730158, 0.5238095238095238, 0.5317460317460317, 0.5396825396825397, 0.5476190476190477, 0.5555555555555556, 0.5634920634920635, 0.5714285714285714, 0.5793650793650793, 0.5873015873015873, 0.5952380952380952, 0.6031746031746031, 0.6111111111111112, 0.6190476190476191, 0.626984126984127, 0.6349206349206349, 0.6428571428571428, 0.6507936507936508, 0.6587301587301587, 0.6666666666666666, 0.6746031746031746, 0.6825396825396826, 0.6904761904761905, 0.6984126984126984, 0.7063492063492063, 0.7142857142857143, 0.7222222222222222, 0.7301587301587301, 0.7380952380952381, 0.746031746031746, 0.753968253968254, 0.7619047619047619, 0.7698412698412698, 0.7777777777777778, 0.7857142857142857, 0.7936507936507937, 0.8015873015873016, 0.8095238095238095, 0.8174603174603174, 0.8253968253968254, 0.8333333333333333, 0.8412698412698413, 0.8492063492063492, 0.8571428571428572, 0.8650793650793651, 0.873015873015873, 0.8809523809523809, 0.8888888888888888, 0.8968253968253967, 0.9047619047619048, 0.9126984126984127, 0.9206349206349207, 0.9285714285714286, 0.9365079365079365, 0.9444444444444444, 0.9523809523809523, 0.9603174603174602, 0.9682539682539683, 0.9761904761904762, 0.9841269841269842, 0.9920634920634921, 1}; 346 | float b[] = { 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4}; 347 | Mat X = linspace(0,1,64); 348 | this->_lut = ColorMap::linear_colormap(X, 349 | Mat(64,1, CV_32FC1, r).clone(), // red 350 | Mat(64,1, CV_32FC1, g).clone(), // green 351 | Mat(64,1, CV_32FC1, b).clone(), // blue 352 | n); // number of sample points 353 | } 354 | }; 355 | 356 | // Equals the GNU Octave colormap "spring". 357 | class Spring : public ColorMap { 358 | public: 359 | Spring() : ColorMap() { 360 | init(256); 361 | } 362 | 363 | Spring(int n) : ColorMap() { 364 | init(n); 365 | } 366 | 367 | void init(int n) { 368 | float r[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 369 | float g[] = { 0, 0.01587301587301587, 0.03174603174603174, 0.04761904761904762, 0.06349206349206349, 0.07936507936507936, 0.09523809523809523, 0.1111111111111111, 0.126984126984127, 0.1428571428571428, 0.1587301587301587, 0.1746031746031746, 0.1904761904761905, 0.2063492063492063, 0.2222222222222222, 0.2380952380952381, 0.253968253968254, 0.2698412698412698, 0.2857142857142857, 0.3015873015873016, 0.3174603174603174, 0.3333333333333333, 0.3492063492063492, 0.3650793650793651, 0.3809523809523809, 0.3968253968253968, 0.4126984126984127, 0.4285714285714285, 0.4444444444444444, 0.4603174603174603, 0.4761904761904762, 0.492063492063492, 0.5079365079365079, 0.5238095238095238, 0.5396825396825397, 0.5555555555555556, 0.5714285714285714, 0.5873015873015873, 0.6031746031746031, 0.6190476190476191, 0.6349206349206349, 0.6507936507936508, 0.6666666666666666, 0.6825396825396826, 0.6984126984126984, 0.7142857142857143, 0.7301587301587301, 0.746031746031746, 0.7619047619047619, 0.7777777777777778, 0.7936507936507936, 0.8095238095238095, 0.8253968253968254, 0.8412698412698413, 0.8571428571428571, 0.873015873015873, 0.8888888888888888, 0.9047619047619048, 0.9206349206349206, 0.9365079365079365, 0.9523809523809523, 0.9682539682539683, 0.9841269841269841, 1}; 370 | float b[] = { 1, 0.9841269841269842, 0.9682539682539683, 0.9523809523809523, 0.9365079365079365, 0.9206349206349207, 0.9047619047619048, 0.8888888888888888, 0.873015873015873, 0.8571428571428572, 0.8412698412698413, 0.8253968253968254, 0.8095238095238095, 0.7936507936507937, 0.7777777777777778, 0.7619047619047619, 0.746031746031746, 0.7301587301587302, 0.7142857142857143, 0.6984126984126984, 0.6825396825396826, 0.6666666666666667, 0.6507936507936508, 0.6349206349206349, 0.6190476190476191, 0.6031746031746033, 0.5873015873015873, 0.5714285714285714, 0.5555555555555556, 0.5396825396825398, 0.5238095238095238, 0.5079365079365079, 0.4920634920634921, 0.4761904761904762, 0.4603174603174603, 0.4444444444444444, 0.4285714285714286, 0.4126984126984127, 0.3968253968253969, 0.3809523809523809, 0.3650793650793651, 0.3492063492063492, 0.3333333333333334, 0.3174603174603174, 0.3015873015873016, 0.2857142857142857, 0.2698412698412699, 0.253968253968254, 0.2380952380952381, 0.2222222222222222, 0.2063492063492064, 0.1904761904761905, 0.1746031746031746, 0.1587301587301587, 0.1428571428571429, 0.126984126984127, 0.1111111111111112, 0.09523809523809523, 0.07936507936507942, 0.06349206349206349, 0.04761904761904767, 0.03174603174603174, 0.01587301587301593, 0}; 371 | Mat X = linspace(0,1,64); 372 | this->_lut = ColorMap::linear_colormap(X, 373 | Mat(64,1, CV_32FC1, r).clone(), // red 374 | Mat(64,1, CV_32FC1, g).clone(), // green 375 | Mat(64,1, CV_32FC1, b).clone(), // blue 376 | n); // number of sample points 377 | } 378 | }; 379 | 380 | // Equals the GNU Octave colormap "cool". 381 | class Cool : public ColorMap { 382 | public: 383 | Cool() : ColorMap() { 384 | init(256); 385 | } 386 | 387 | Cool(int n) : ColorMap() { 388 | init(n); 389 | } 390 | 391 | void init(int n) { 392 | float r[] = { 0, 0.01587301587301587, 0.03174603174603174, 0.04761904761904762, 0.06349206349206349, 0.07936507936507936, 0.09523809523809523, 0.1111111111111111, 0.126984126984127, 0.1428571428571428, 0.1587301587301587, 0.1746031746031746, 0.1904761904761905, 0.2063492063492063, 0.2222222222222222, 0.2380952380952381, 0.253968253968254, 0.2698412698412698, 0.2857142857142857, 0.3015873015873016, 0.3174603174603174, 0.3333333333333333, 0.3492063492063492, 0.3650793650793651, 0.3809523809523809, 0.3968253968253968, 0.4126984126984127, 0.4285714285714285, 0.4444444444444444, 0.4603174603174603, 0.4761904761904762, 0.492063492063492, 0.5079365079365079, 0.5238095238095238, 0.5396825396825397, 0.5555555555555556, 0.5714285714285714, 0.5873015873015873, 0.6031746031746031, 0.6190476190476191, 0.6349206349206349, 0.6507936507936508, 0.6666666666666666, 0.6825396825396826, 0.6984126984126984, 0.7142857142857143, 0.7301587301587301, 0.746031746031746, 0.7619047619047619, 0.7777777777777778, 0.7936507936507936, 0.8095238095238095, 0.8253968253968254, 0.8412698412698413, 0.8571428571428571, 0.873015873015873, 0.8888888888888888, 0.9047619047619048, 0.9206349206349206, 0.9365079365079365, 0.9523809523809523, 0.9682539682539683, 0.9841269841269841, 1}; 393 | float g[] = { 1, 0.9841269841269842, 0.9682539682539683, 0.9523809523809523, 0.9365079365079365, 0.9206349206349207, 0.9047619047619048, 0.8888888888888888, 0.873015873015873, 0.8571428571428572, 0.8412698412698413, 0.8253968253968254, 0.8095238095238095, 0.7936507936507937, 0.7777777777777778, 0.7619047619047619, 0.746031746031746, 0.7301587301587302, 0.7142857142857143, 0.6984126984126984, 0.6825396825396826, 0.6666666666666667, 0.6507936507936508, 0.6349206349206349, 0.6190476190476191, 0.6031746031746033, 0.5873015873015873, 0.5714285714285714, 0.5555555555555556, 0.5396825396825398, 0.5238095238095238, 0.5079365079365079, 0.4920634920634921, 0.4761904761904762, 0.4603174603174603, 0.4444444444444444, 0.4285714285714286, 0.4126984126984127, 0.3968253968253969, 0.3809523809523809, 0.3650793650793651, 0.3492063492063492, 0.3333333333333334, 0.3174603174603174, 0.3015873015873016, 0.2857142857142857, 0.2698412698412699, 0.253968253968254, 0.2380952380952381, 0.2222222222222222, 0.2063492063492064, 0.1904761904761905, 0.1746031746031746, 0.1587301587301587, 0.1428571428571429, 0.126984126984127, 0.1111111111111112, 0.09523809523809523, 0.07936507936507942, 0.06349206349206349, 0.04761904761904767, 0.03174603174603174, 0.01587301587301593, 0}; 394 | float b[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 395 | Mat X = linspace(0,1,64); 396 | this->_lut = ColorMap::linear_colormap(X, 397 | Mat(64,1, CV_32FC1, r).clone(), // red 398 | Mat(64,1, CV_32FC1, g).clone(), // green 399 | Mat(64,1, CV_32FC1, b).clone(), // blue 400 | n); // number of sample points 401 | } 402 | }; 403 | 404 | // Equals the GNU Octave colormap "hsv". 405 | class HSV : public ColorMap { 406 | public: 407 | HSV() : ColorMap() { 408 | init(256); 409 | } 410 | 411 | HSV(int n) : ColorMap() { 412 | init(n); 413 | } 414 | 415 | void init(int n) { 416 | float r[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9523809523809526, 0.8571428571428568, 0.7619047619047614, 0.6666666666666665, 0.5714285714285716, 0.4761904761904763, 0.3809523809523805, 0.2857142857142856, 0.1904761904761907, 0.0952380952380949, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09523809523809557, 0.1904761904761905, 0.2857142857142854, 0.3809523809523809, 0.4761904761904765, 0.5714285714285714, 0.6666666666666663, 0.7619047619047619, 0.8571428571428574, 0.9523809523809523, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 417 | float g[] = { 0, 0.09523809523809523, 0.1904761904761905, 0.2857142857142857, 0.3809523809523809, 0.4761904761904762, 0.5714285714285714, 0.6666666666666666, 0.7619047619047619, 0.8571428571428571, 0.9523809523809523, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9523809523809526, 0.8571428571428577, 0.7619047619047619, 0.6666666666666665, 0.5714285714285716, 0.4761904761904767, 0.3809523809523814, 0.2857142857142856, 0.1904761904761907, 0.09523809523809579, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 418 | float b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09523809523809523, 0.1904761904761905, 0.2857142857142857, 0.3809523809523809, 0.4761904761904762, 0.5714285714285714, 0.6666666666666666, 0.7619047619047619, 0.8571428571428571, 0.9523809523809523, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9523809523809526, 0.8571428571428577, 0.7619047619047614, 0.6666666666666665, 0.5714285714285716, 0.4761904761904767, 0.3809523809523805, 0.2857142857142856, 0.1904761904761907, 0.09523809523809579, 0}; 419 | Mat X = linspace(0,1,64); 420 | this->_lut = ColorMap::linear_colormap(X, 421 | Mat(64,1, CV_32FC1, r).clone(), // red 422 | Mat(64,1, CV_32FC1, g).clone(), // green 423 | Mat(64,1, CV_32FC1, b).clone(), // blue 424 | n); // number of sample points 425 | } 426 | }; 427 | 428 | // Equals the GNU Octave colormap "pink". 429 | class Pink : public ColorMap { 430 | public: 431 | Pink() : ColorMap() { 432 | init(256); 433 | } 434 | 435 | Pink(int n) : ColorMap() { 436 | init(n); 437 | } 438 | 439 | void init(int n) { 440 | float r[] = { 0, 0.1571348402636772, 0.2222222222222222, 0.2721655269759087, 0.3142696805273544, 0.3513641844631533, 0.3849001794597505, 0.415739709641549, 0.4444444444444444, 0.4714045207910317, 0.4969039949999532, 0.5211573066470477, 0.5443310539518174, 0.5665577237325317, 0.5879447357921312, 0.6085806194501846, 0.6285393610547089, 0.6478835438717, 0.6666666666666666, 0.6849348892187751, 0.7027283689263065, 0.7200822998230956, 0.7370277311900888, 0.753592220347252, 0.7663560447348133, 0.7732293307186413, 0.7800420555749596, 0.7867957924694432, 0.7934920476158722, 0.8001322641986387, 0.8067178260046388, 0.8132500607904444, 0.8197302434079591, 0.8261595987094034, 0.8325393042503717, 0.8388704928078611, 0.8451542547285166, 0.8513916401208816, 0.8575836609041332, 0.8637312927246217, 0.8698354767504924, 0.8758971213537393, 0.8819171036881968, 0.8878962711712378, 0.8938354428762595, 0.8997354108424372, 0.9055969413076769, 0.9114207758701963, 0.9172076325837248, 0.9229582069908971, 0.9286731730990523, 0.9343531843023135, 0.9399988742535192, 0.9456108576893002, 0.9511897312113418, 0.9567360740266436, 0.9622504486493763, 0.9677334015667416, 0.9731854638710686, 0.9786071518602129, 0.9839989676081821, 0.9893613995077727, 0.9946949227868761, 1}; 441 | float g[] = { 0, 0.1028688999747279, 0.1454785934906616, 0.1781741612749496, 0.2057377999494559, 0.2300218531141181, 0.2519763153394848, 0.2721655269759087, 0.2909571869813232, 0.3086066999241838, 0.3253000243161777, 0.3411775438127727, 0.3563483225498992, 0.3708990935094579, 0.3849001794597505, 0.3984095364447979, 0.4114755998989117, 0.4241393401869012, 0.4364357804719847, 0.4483951394230328, 0.4600437062282361, 0.4714045207910317, 0.4824979096371639, 0.4933419132673033, 0.5091750772173156, 0.5328701692569688, 0.5555555555555556, 0.5773502691896257, 0.5983516452371671, 0.6186404847588913, 0.6382847385042254, 0.6573421981221795, 0.6758625033664688, 0.6938886664887108, 0.7114582486036499, 0.7286042804780002, 0.7453559924999299, 0.7617394000445604, 0.7777777777777778, 0.7934920476158723, 0.8089010988089465, 0.8240220541217402, 0.8388704928078611, 0.8534606386520677, 0.8678055195451838, 0.8819171036881968, 0.8958064164776166, 0.9094836413191612, 0.9172076325837248, 0.9229582069908971, 0.9286731730990523, 0.9343531843023135, 0.9399988742535192, 0.9456108576893002, 0.9511897312113418, 0.9567360740266436, 0.9622504486493763, 0.9677334015667416, 0.9731854638710686, 0.9786071518602129, 0.9839989676081821, 0.9893613995077727, 0.9946949227868761, 1}; 442 | float b[] = { 0, 0.1028688999747279, 0.1454785934906616, 0.1781741612749496, 0.2057377999494559, 0.2300218531141181, 0.2519763153394848, 0.2721655269759087, 0.2909571869813232, 0.3086066999241838, 0.3253000243161777, 0.3411775438127727, 0.3563483225498992, 0.3708990935094579, 0.3849001794597505, 0.3984095364447979, 0.4114755998989117, 0.4241393401869012, 0.4364357804719847, 0.4483951394230328, 0.4600437062282361, 0.4714045207910317, 0.4824979096371639, 0.4933419132673033, 0.5039526306789697, 0.5143444998736397, 0.5245305283129621, 0.5345224838248488, 0.5443310539518174, 0.5539659798925444, 0.563436169819011, 0.5727497953228163, 0.5819143739626463, 0.5909368402852788, 0.5998236072282915, 0.6085806194501846, 0.6172133998483676, 0.6257270902992705, 0.6341264874742278, 0.642416074439621, 0.6506000486323554, 0.6586823467062358, 0.6666666666666666, 0.6745564876468501, 0.6823550876255453, 0.6900655593423541, 0.6976908246297114, 0.7052336473499384, 0.7237468644557459, 0.7453559924999298, 0.7663560447348133, 0.7867957924694432, 0.8067178260046388, 0.8261595987094034, 0.8451542547285166, 0.8637312927246217, 0.8819171036881968, 0.8997354108424372, 0.9172076325837248, 0.9343531843023135, 0.9511897312113418, 0.9677334015667416, 0.9839989676081821, 1}; 443 | Mat X = linspace(0,1,64); 444 | this->_lut = ColorMap::linear_colormap(X, 445 | Mat(64,1, CV_32FC1, r).clone(), // red 446 | Mat(64,1, CV_32FC1, g).clone(), // green 447 | Mat(64,1, CV_32FC1, b).clone(), // blue 448 | n); // number of sample points 449 | } 450 | }; 451 | 452 | // Equals the GNU Octave colormap "hot". 453 | class Hot : public ColorMap { 454 | public: 455 | Hot() : ColorMap() { 456 | init(256); 457 | } 458 | 459 | Hot(int n) : ColorMap() { 460 | init(n); 461 | } 462 | 463 | void init(int n) { 464 | float r[] = { 0, 0.03968253968253968, 0.07936507936507936, 0.119047619047619, 0.1587301587301587, 0.1984126984126984, 0.2380952380952381, 0.2777777777777778, 0.3174603174603174, 0.3571428571428571, 0.3968253968253968, 0.4365079365079365, 0.4761904761904762, 0.5158730158730158, 0.5555555555555556, 0.5952380952380952, 0.6349206349206349, 0.6746031746031745, 0.7142857142857142, 0.753968253968254, 0.7936507936507936, 0.8333333333333333, 0.873015873015873, 0.9126984126984127, 0.9523809523809523, 0.992063492063492, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 465 | float g[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03174603174603163, 0.0714285714285714, 0.1111111111111112, 0.1507936507936507, 0.1904761904761905, 0.23015873015873, 0.2698412698412698, 0.3095238095238093, 0.3492063492063491, 0.3888888888888888, 0.4285714285714284, 0.4682539682539679, 0.5079365079365079, 0.5476190476190477, 0.5873015873015872, 0.6269841269841268, 0.6666666666666665, 0.7063492063492065, 0.746031746031746, 0.7857142857142856, 0.8253968253968254, 0.8650793650793651, 0.9047619047619047, 0.9444444444444442, 0.984126984126984, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 466 | float b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.04761904761904745, 0.1269841269841265, 0.2063492063492056, 0.2857142857142856, 0.3650793650793656, 0.4444444444444446, 0.5238095238095237, 0.6031746031746028, 0.6825396825396828, 0.7619047619047619, 0.8412698412698409, 0.92063492063492, 1}; 467 | Mat X = linspace(0,1,64); 468 | this->_lut = ColorMap::linear_colormap(X, 469 | Mat(64,1, CV_32FC1, r).clone(), // red 470 | Mat(64,1, CV_32FC1, g).clone(), // green 471 | Mat(64,1, CV_32FC1, b).clone(), // blue 472 | n); // number of sample points 473 | } 474 | }; 475 | 476 | void ColorMap::operator()(InputArray _src, OutputArray _dst) const 477 | { 478 | if(_lut.total() != 256) 479 | CV_Error(CV_StsAssert, "cv::LUT only supports tables of size 256."); 480 | Mat src = _src.getMat(); 481 | // Return original matrix if wrong type is given (is fail loud better here?) 482 | if(src.type() != CV_8UC1 && src.type() != CV_8UC3) 483 | { 484 | src.copyTo(_dst); 485 | return; 486 | } 487 | // Turn into a BGR matrix into its grayscale representation. 488 | if(src.type() == CV_8UC3) 489 | cvtColor(src.clone(), src, CV_BGR2GRAY); 490 | cvtColor(src.clone(), src, CV_GRAY2BGR); 491 | // Apply the ColorMap. 492 | LUT(src, _lut, _dst); 493 | } 494 | 495 | Mat ColorMap::linear_colormap(InputArray X, 496 | InputArray r, InputArray g, InputArray b, 497 | InputArray xi) { 498 | Mat lut; 499 | Mat planes[] = { 500 | interp1(X, b, xi), 501 | interp1(X, g, xi), 502 | interp1(X, r, xi)}; 503 | merge(planes, 3, lut); 504 | return lut; 505 | } 506 | 507 | } // namespace colormap 508 | 509 | void applyColorMap(InputArray src, OutputArray dst, int colormap) 510 | { 511 | colormap::ColorMap* cm = 512 | colormap == COLORMAP_AUTUMN ? (colormap::ColorMap*)(new colormap::Autumn) : 513 | colormap == COLORMAP_BONE ? (colormap::ColorMap*)(new colormap::Bone) : 514 | colormap == COLORMAP_COOL ? (colormap::ColorMap*)(new colormap::Cool) : 515 | colormap == COLORMAP_HOT ? (colormap::ColorMap*)(new colormap::Hot) : 516 | colormap == COLORMAP_HSV ? (colormap::ColorMap*)(new colormap::HSV) : 517 | colormap == COLORMAP_JET ? (colormap::ColorMap*)(new colormap::Jet) : 518 | colormap == COLORMAP_OCEAN ? (colormap::ColorMap*)(new colormap::Ocean) : 519 | colormap == COLORMAP_PINK ? (colormap::ColorMap*)(new colormap::Pink) : 520 | colormap == COLORMAP_RAINBOW ? (colormap::ColorMap*)(new colormap::Rainbow) : 521 | colormap == COLORMAP_SPRING ? (colormap::ColorMap*)(new colormap::Spring) : 522 | colormap == COLORMAP_SUMMER ? (colormap::ColorMap*)(new colormap::Summer) : 523 | colormap == COLORMAP_WINTER ? (colormap::ColorMap*)(new colormap::Winter) : 0; 524 | 525 | 526 | 527 | if( !cm ) { 528 | CV_Error( CV_StsBadArg, "Unknown colormap id; use one of COLORMAP_*"); 529 | } 530 | 531 | (*cm)(src, dst); 532 | 533 | delete cm; 534 | } 535 | } 536 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011. Philipp Wagner . 3 | * Released to public domain under terms of the BSD Simplified license. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the organization nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software 14 | * without specific prior written permission. 15 | * 16 | * See 17 | */ 18 | #include 19 | #include 20 | #include "colormap.hpp" 21 | 22 | using namespace cv; 23 | 24 | void save_image(const string filename, const Mat& src, int colormap); 25 | 26 | int main(int argc, const char *argv[]) { 27 | // Get the path to the image, if it was given 28 | // if no arguments were given. 29 | string filename; 30 | if (argc > 1) { 31 | filename = string(argv[1]); 32 | } 33 | 34 | // The following lines generate the color scales for 35 | // the available colormaps. Once you have added your 36 | // own colormap, please extend the demo here as well! 37 | Mat img1 = Mat::zeros(30, 256, CV_8UC1); 38 | for(int i = 0; i < 256; i++) { 39 | Mat roi = Mat(img1, Range::all(), Range(i,i+1)); 40 | roi += i; 41 | } 42 | 43 | string prefix("colorscale_"); 44 | save_image(prefix + string("autumn.jpg"), img1, COLORMAP_AUTUMN); 45 | save_image(prefix + string("bone.jpg"), img1, COLORMAP_BONE); 46 | save_image(prefix + string("jet.jpg"), img1, COLORMAP_JET); 47 | save_image(prefix + string("winter.jpg"), img1, COLORMAP_WINTER); 48 | save_image(prefix + string("rainbow.jpg"), img1, COLORMAP_RAINBOW); 49 | save_image(prefix + string("ocean.jpg"), img1, COLORMAP_OCEAN); 50 | save_image(prefix + string("summer.jpg"), img1, COLORMAP_SUMMER); 51 | save_image(prefix + string("spring.jpg"), img1, COLORMAP_SPRING); 52 | save_image(prefix + string("cool.jpg"), img1, COLORMAP_COOL); 53 | save_image(prefix + string("hsv.jpg"), img1, COLORMAP_HSV); 54 | save_image(prefix + string("pink.jpg"), img1, COLORMAP_PINK); 55 | save_image(prefix + string("hot.jpg"), img1, COLORMAP_HOT); 56 | 57 | // The following lines show how to apply a colormap on a given image 58 | // and show it with cv::imshow example with an image. An exception is 59 | // thrown if the path to the image is invalid. 60 | if(!filename.empty()) { 61 | Mat img0 = imread(filename); 62 | if(img0.empty()) { 63 | CV_Error(CV_StsBadArg, "Sample image is empty. Please adjust your path, so it points to a valid input image!"); 64 | } 65 | Mat cm_img0; 66 | applyColorMap(img0, cm_img0, COLORMAP_JET); 67 | imshow("cm_img0", cm_img0); 68 | 69 | waitKey(0); 70 | } 71 | 72 | return 0; // success! 73 | } 74 | 75 | 76 | void save_image(const string filename, const Mat& src, int colormap) { 77 | Mat cm_dst; 78 | applyColorMap(src, cm_dst, colormap); 79 | cm_dst.convertTo(cm_dst, CV_8UC3, 255.0); 80 | imwrite(filename, cm_dst); 81 | } 82 | --------------------------------------------------------------------------------