From 61b94841556d6615545f6d52d082de8578e73fd0 Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Thu, 15 Dec 2016 10:17:05 +0100 Subject: [PATCH] ApplyColorMap can be used with a user colormap --- modules/imgproc/include/opencv2/imgproc.hpp | 10 ++++++- modules/imgproc/src/colormap.cpp | 33 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 007a238d67..a196d928f8 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -4097,9 +4097,17 @@ enum ColormapTypes @param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3. @param dst The result is the colormapped source image. Note: Mat::create is called on dst. @param colormap The colormap to apply, see cv::ColormapTypes - */ +*/ CV_EXPORTS_W void applyColorMap(InputArray src, OutputArray dst, int colormap); +/** @brief Applies a user colormap on a given image. + +@param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3. +@param dst The result is the colormapped source image. Note: Mat::create is called on dst. +@param userColor The colormap to apply of type CV_8UC1 or CV_8UC3 and size 256 +*/ +CV_EXPORTS_W void applyColorMap(InputArray src, OutputArray dst, InputArray userColor); + //! @} imgproc_colormap //! @addtogroup imgproc_draw diff --git a/modules/imgproc/src/colormap.cpp b/modules/imgproc/src/colormap.cpp index 3fc9755403..171135e247 100644 --- a/modules/imgproc/src/colormap.cpp +++ b/modules/imgproc/src/colormap.cpp @@ -490,6 +490,22 @@ namespace colormap } }; + // UserColormap . + class UserColorMap : public ColorMap { + public: + + UserColorMap(Mat c) : ColorMap() { + init(c); + } + + void init(Mat c) { + this->_lut = c; + } + void init(int n) { + CV_Error(Error::StsAssert, "unused method in UserColormap."); + } + }; + void ColorMap::operator()(InputArray _src, OutputArray _dst) const { CV_INSTRUMENT_REGION() @@ -546,4 +562,21 @@ namespace colormap delete cm; } + + void applyColorMap(InputArray src, OutputArray dst, InputArray userColor) + { + if (userColor.total() != 256) + CV_Error(Error::StsAssert, "cv::LUT only supports tables of size 256."); + if (userColor.type() != CV_8UC1 && userColor.type() != CV_8UC3) + CV_Error(Error::StsAssert, "cv::LUT only supports tables CV_8UC1 or CV_8UC3."); + colormap::ColorMap* cm = (colormap::ColorMap*) (new colormap::UserColorMap(userColor.getMat())); + + if (!cm) + CV_Error(Error::StsBadArg, "Unknown colormap id; use one of COLORMAP_*"); + + (*cm)(src, dst); + + delete cm; + } + }