Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/falsecolor.cpp
16337 views
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",
18
"Spring", "Cool", "HSV", "Pink", "Hot", "Parula", "User defined (random)"};
19
20
static void TrackColorMap(int x, void *r)
21
{
22
ParamColorMap *p = (ParamColorMap*)r;
23
Mat dst;
24
p->iColormap= x;
25
if (x == COLORMAP_PARULA + 1)
26
{
27
Mat lutRND(256, 1, CV_8UC3);
28
randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
29
applyColorMap(p->img, dst, lutRND);
30
}
31
else
32
applyColorMap(p->img,dst,p->iColormap);
33
34
putText(dst, "Colormap : "+ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
35
imshow(winName, dst);
36
}
37
38
39
static Mat DrawMyImage(int thickness,int nbShape)
40
{
41
Mat img=Mat::zeros(500,256*thickness+100,CV_8UC1);
42
int offsetx = 50, offsety = 25;
43
int lineLenght = 50;
44
45
for (int i=0;i<256;i++)
46
line(img,Point(thickness*i+ offsetx, offsety),Point(thickness*i+ offsetx, offsety+ lineLenght),Scalar(i), thickness);
47
RNG r;
48
Point center;
49
int radius;
50
int width,height;
51
int angle;
52
Rect rc;
53
54
for (int i=1;i<=nbShape;i++)
55
{
56
int typeShape = r.uniform(MyCIRCLE, MyELLIPSE+1);
57
switch (typeShape) {
58
case MyCIRCLE:
59
center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLenght, img.rows - offsety));
60
radius = r.uniform(1, min(offsetx, offsety));
61
circle(img,center,radius,Scalar(i),-1);
62
break;
63
case MyRECTANGLE:
64
center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLenght, img.rows - offsety));
65
width = r.uniform(1, min(offsetx, offsety));
66
height = r.uniform(1, min(offsetx, offsety));
67
rc = Rect(center-Point(width ,height )/2, center + Point(width , height )/2);
68
rectangle(img,rc, Scalar(i), -1);
69
break;
70
case MyELLIPSE:
71
center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLenght, img.rows - offsety));
72
width = r.uniform(1, min(offsetx, offsety));
73
height = r.uniform(1, min(offsetx, offsety));
74
angle = r.uniform(0, 180);
75
ellipse(img, center,Size(width/2,height/2),angle,0,360, Scalar(i), -1);
76
break;
77
}
78
}
79
return img;
80
}
81
82
int main(int argc, char** argv)
83
{
84
cout << "This program demonstrates the use of applyColorMap function.\n\n";
85
86
ParamColorMap p;
87
Mat img;
88
89
if (argc > 1)
90
img = imread(argv[1], IMREAD_GRAYSCALE);
91
else
92
img = DrawMyImage(2,256);
93
94
p.img=img;
95
p.iColormap=0;
96
97
imshow("Gray image",img);
98
namedWindow(winName);
99
createTrackbar("colormap", winName,&p.iColormap,1,TrackColorMap,(void*)&p);
100
setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
101
setTrackbarMax("colormap", winName, COLORMAP_PARULA+1);
102
setTrackbarPos("colormap", winName, -1);
103
104
TrackColorMap(0, (void*)&p);
105
106
cout << "Press a key to exit" << endl;
107
waitKey(0);
108
return 0;
109
}
110
111