1 #include "opencv2/imgproc.hpp"
2 #include "opencv2/imgcodecs.hpp"
3 #include "opencv2/highgui.hpp"
4 #include <iostream>
5
6 using namespace cv;
7 using namespace std;
8
9 enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
10
11 struct ParamColorMap {
12 int iColormap;
13 Mat img;
14 };
15
16 String winName="False color";
17 static const String ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer", "Spring",
18 "Cool", "HSV", "Pink", "Hot", "Parula", "Magma", "Inferno", "Plasma", "Viridis",
19 "Cividis", "Twilight", "Twilight Shifted", "Turbo", "User defined (random)" };
20
TrackColorMap(int x,void * r)21 static void TrackColorMap(int x, void *r)
22 {
23 ParamColorMap *p = (ParamColorMap*)r;
24 Mat dst;
25 p->iColormap= x;
26 if (x == COLORMAP_TURBO + 1)
27 {
28 Mat lutRND(256, 1, CV_8UC3);
29 randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
30 applyColorMap(p->img, dst, lutRND);
31 }
32 else
33 applyColorMap(p->img,dst,p->iColormap);
34
35 putText(dst, "Colormap : "+ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
36 imshow(winName, dst);
37 }
38
39
DrawMyImage(int thickness,int nbShape)40 static Mat DrawMyImage(int thickness,int nbShape)
41 {
42 Mat img=Mat::zeros(500,256*thickness+100,CV_8UC1);
43 int offsetx = 50, offsety = 25;
44 int lineLength = 50;
45
46 for (int i=0;i<256;i++)
47 line(img,Point(thickness*i+ offsetx, offsety),Point(thickness*i+ offsetx, offsety+ lineLength),Scalar(i), thickness);
48 RNG r;
49 Point center;
50 int radius;
51 int width,height;
52 int angle;
53 Rect rc;
54
55 for (int i=1;i<=nbShape;i++)
56 {
57 int typeShape = r.uniform(MyCIRCLE, MyELLIPSE+1);
58 switch (typeShape) {
59 case MyCIRCLE:
60 center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
61 radius = r.uniform(1, min(offsetx, offsety));
62 circle(img,center,radius,Scalar(i),-1);
63 break;
64 case MyRECTANGLE:
65 center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
66 width = r.uniform(1, min(offsetx, offsety));
67 height = r.uniform(1, min(offsetx, offsety));
68 rc = Rect(center-Point(width ,height )/2, center + Point(width , height )/2);
69 rectangle(img,rc, Scalar(i), -1);
70 break;
71 case MyELLIPSE:
72 center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
73 width = r.uniform(1, min(offsetx, offsety));
74 height = r.uniform(1, min(offsetx, offsety));
75 angle = r.uniform(0, 180);
76 ellipse(img, center,Size(width/2,height/2),angle,0,360, Scalar(i), -1);
77 break;
78 }
79 }
80 return img;
81 }
82
main(int argc,char ** argv)83 int main(int argc, char** argv)
84 {
85 cout << "This program demonstrates the use of applyColorMap function.\n\n";
86
87 ParamColorMap p;
88 Mat img;
89
90 if (argc > 1)
91 img = imread(samples::findFile(argv[1]), IMREAD_GRAYSCALE);
92 else
93 img = DrawMyImage(2,256);
94
95 p.img=img;
96 p.iColormap=0;
97
98 imshow("Gray image",img);
99 namedWindow(winName);
100 createTrackbar("colormap", winName,&p.iColormap,1,TrackColorMap,(void*)&p);
101 setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
102 setTrackbarMax("colormap", winName, COLORMAP_TURBO+1);
103 setTrackbarPos("colormap", winName, -1);
104
105 TrackColorMap(0, (void*)&p);
106
107 cout << "Press a key to exit" << endl;
108 waitKey(0);
109 return 0;
110 }
111