From 61b94841556d6615545f6d52d082de8578e73fd0 Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Thu, 15 Dec 2016 10:17:05 +0100 Subject: [PATCH 1/8] 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; + } + } From 4826d976d6de4c0d652b134c1e7e582e459c69f9 Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Thu, 15 Dec 2016 10:51:08 +0100 Subject: [PATCH 2/8] Suppress warning unused parameter --- modules/imgproc/src/colormap.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgproc/src/colormap.cpp b/modules/imgproc/src/colormap.cpp index 171135e247..8d257a818f 100644 --- a/modules/imgproc/src/colormap.cpp +++ b/modules/imgproc/src/colormap.cpp @@ -502,7 +502,7 @@ namespace colormap this->_lut = c; } void init(int n) { - CV_Error(Error::StsAssert, "unused method in UserColormap."); + CV_Error(Error::StsAssert, format("unused method in UserColormap init(%d).",n)); } }; From d8fdf9321da43b5292662da9ff5d3c503e0c168a Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Thu, 15 Dec 2016 22:49:37 +0100 Subject: [PATCH 3/8] mend --- modules/imgproc/src/colormap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/src/colormap.cpp b/modules/imgproc/src/colormap.cpp index 8d257a818f..c59d68f45a 100644 --- a/modules/imgproc/src/colormap.cpp +++ b/modules/imgproc/src/colormap.cpp @@ -565,14 +565,14 @@ namespace colormap void applyColorMap(InputArray src, OutputArray dst, InputArray userColor) { - if (userColor.total() != 256) + if (userColor.size() != Size(1,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_*"); + CV_Error(Error::StsBadArg, "Unknown inputArray"); (*cm)(src, dst); From 5ad02d7a9a5f0790fea5243482132824d8e1643e Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Thu, 15 Dec 2016 22:55:59 +0100 Subject: [PATCH 4/8] Add sample --- samples/cpp/falsecolor.cpp | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 samples/cpp/falsecolor.cpp diff --git a/samples/cpp/falsecolor.cpp b/samples/cpp/falsecolor.cpp new file mode 100644 index 0000000000..40890edf12 --- /dev/null +++ b/samples/cpp/falsecolor.cpp @@ -0,0 +1,146 @@ +#include + +using namespace cv; +using namespace std; + +enum {MyCIRCLE=0,MyRECTANGLE,MyELLIPSE}MyShape; + +struct ParamColorMar { + int iColormap; + Mat img; +}; + +Ptr lutRND; +String winName="False color"; + +static void TrackColorMap(int x, void *r) +{ + ParamColorMar *p = (ParamColorMar*)r; + Mat dst; + p->iColormap= x; + if (x == cv::COLORMAP_PARULA + 1) + { + if (!lutRND) + { + RNG r; + Mat *palette = new Mat(256, 1, CV_8UC3); + r.fill(*palette, RNG::UNIFORM, 0, 256); + lutRND = Ptr(palette); + } + applyColorMap(p->img, dst, *lutRND.get()); + } + else + applyColorMap(p->img,dst,p->iColormap); + String colorMapName; + + switch (p->iColormap) { + case COLORMAP_AUTUMN : + colorMapName = "Colormap : Autumn"; + break; + case COLORMAP_BONE : + colorMapName = "Colormap : Bone"; + break; + case COLORMAP_JET : + colorMapName = "Colormap : Jet"; + break; + case COLORMAP_WINTER : + colorMapName = "Colormap : Winter"; + break; + case COLORMAP_RAINBOW : + colorMapName = "Colormap : Rainbow"; + break; + case COLORMAP_OCEAN : + colorMapName = "Colormap : Ocean"; + break; + case COLORMAP_SUMMER : + colorMapName = "Colormap : Summer"; + break; + case COLORMAP_SPRING : + colorMapName = "Colormap : Spring"; + break; + case COLORMAP_COOL : + colorMapName = "Colormap : Cool"; + break; + case COLORMAP_HSV : + colorMapName = "Colormap : HSV"; + break; + case COLORMAP_PINK : + colorMapName = "Colormap : Pink"; + break; + case COLORMAP_HOT : + colorMapName = "Colormap : Hot"; + break; + case COLORMAP_PARULA : + colorMapName = "Colormap : Parula"; + break; + default: + colorMapName = "User colormap : random"; + break; + } + putText(dst, colorMapName, Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255)); + imshow(winName, dst); +} + + +static Mat DrawMyImage(int thickness,int nbShape) +{ + Mat img=Mat::zeros(500,256*thickness+100,CV_8UC1); + int offsetx = 50, offsety = 25; + int lineLenght = 50; + + for (int i=0;i<256;i++) + line(img,Point(thickness*i+ offsetx, offsety),Point(thickness*i+ offsetx, offsety+ lineLenght),Scalar(i), thickness); + RNG r; + Point center; + int radius; + int width,height; + int angle; + Rect rc; + + for (int i=1;i<=nbShape;i++) + { + int typeShape = r.uniform(MyCIRCLE, MyELLIPSE+1); + switch (typeShape) { + case MyCIRCLE: + center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLenght, img.rows - offsety)); + radius = r.uniform(1, min(offsetx, offsety)); + circle(img,center,radius,Scalar(i),-1); + break; + case MyRECTANGLE: + center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLenght, img.rows - offsety)); + width = r.uniform(1, min(offsetx, offsety)); + height = r.uniform(1, min(offsetx, offsety)); + rc = Rect(center-Point(width ,height )/2, center + Point(width , height )/2); + rectangle(img,rc, Scalar(i), -1); + break; + case MyELLIPSE: + center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLenght, img.rows - offsety)); + width = r.uniform(1, min(offsetx, offsety)); + height = r.uniform(1, min(offsetx, offsety)); + angle = r.uniform(0, 180); + ellipse(img, center,Size(width/2,height/2),angle,0,360, Scalar(i), -1); + break; + } + } + return img; +} + +int main(int argc, const char *argv[]) +{ + ParamColorMar p; + + Mat img= DrawMyImage(2,256); + p.img=img; + p.iColormap=0; + + imshow("Gray image",img); + namedWindow(winName); + createTrackbar("colormap", winName,&p.iColormap,1,TrackColorMap,(void*)&p); + setTrackbarMin("colormap", winName, cv::COLORMAP_AUTUMN); + setTrackbarMax("colormap", winName, cv::COLORMAP_PARULA+1); + setTrackbarPos("colormap", winName, -1); + + TrackColorMap(0,(void*)&p); + waitKey(0); + return 0; +} \ No newline at end of file From 1f724e2e8a2a54506cefdee12efeeb7e95835cf9 Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Fri, 16 Dec 2016 08:33:31 +0100 Subject: [PATCH 5/8] warnings --- samples/cpp/falsecolor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/cpp/falsecolor.cpp b/samples/cpp/falsecolor.cpp index 40890edf12..f96bdcc99d 100644 --- a/samples/cpp/falsecolor.cpp +++ b/samples/cpp/falsecolor.cpp @@ -125,7 +125,7 @@ static Mat DrawMyImage(int thickness,int nbShape) return img; } -int main(int argc, const char *argv[]) +int main(void) { ParamColorMar p; @@ -143,4 +143,4 @@ int main(int argc, const char *argv[]) TrackColorMap(0,(void*)&p); waitKey(0); return 0; -} \ No newline at end of file +} From f92c9ddb437e8a41324ca0a45f00f029d65f3713 Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Fri, 16 Dec 2016 09:09:39 +0100 Subject: [PATCH 6/8] warning 2 --- samples/cpp/falsecolor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/cpp/falsecolor.cpp b/samples/cpp/falsecolor.cpp index f96bdcc99d..84f81cc84f 100644 --- a/samples/cpp/falsecolor.cpp +++ b/samples/cpp/falsecolor.cpp @@ -22,9 +22,9 @@ static void TrackColorMap(int x, void *r) { if (!lutRND) { - RNG r; + RNG ra; Mat *palette = new Mat(256, 1, CV_8UC3); - r.fill(*palette, RNG::UNIFORM, 0, 256); + ra.fill(*palette, RNG::UNIFORM, 0, 256); lutRND = Ptr(palette); } applyColorMap(p->img, dst, *lutRND.get()); From 8415b907bbb3b1da10aa30efc0770cfbfaca9b3b Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Fri, 16 Dec 2016 09:23:21 +0100 Subject: [PATCH 7/8] warnings 2 --- samples/cpp/falsecolor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/falsecolor.cpp b/samples/cpp/falsecolor.cpp index 84f81cc84f..a72711c1e4 100644 --- a/samples/cpp/falsecolor.cpp +++ b/samples/cpp/falsecolor.cpp @@ -3,7 +3,7 @@ using namespace cv; using namespace std; -enum {MyCIRCLE=0,MyRECTANGLE,MyELLIPSE}MyShape; +enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE}; struct ParamColorMar { int iColormap; From 587e9a554ed887e7d4b2d3e0d148b0bf015ad63a Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Wed, 11 Jan 2017 16:32:14 +0100 Subject: [PATCH 8/8] remove new operator --- modules/imgproc/src/colormap.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/modules/imgproc/src/colormap.cpp b/modules/imgproc/src/colormap.cpp index c59d68f45a..f8d5d1b36d 100644 --- a/modules/imgproc/src/colormap.cpp +++ b/modules/imgproc/src/colormap.cpp @@ -569,14 +569,9 @@ namespace colormap 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())); + colormap::UserColorMap cm(userColor.getMat()); - if (!cm) - CV_Error(Error::StsBadArg, "Unknown inputArray"); - - (*cm)(src, dst); - - delete cm; + (cm)(src, dst); } }