opencv/samples/cpp/falsecolor.cpp

110 lines
3.3 KiB
C++
Raw Normal View History

2017-11-01 04:10:29 +08:00
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
2016-12-16 05:55:59 +08:00
using namespace cv;
using namespace std;
2016-12-16 16:23:21 +08:00
enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
2016-12-16 05:55:59 +08:00
2017-11-01 04:10:29 +08:00
struct ParamColorMap {
2016-12-16 05:55:59 +08:00
int iColormap;
Mat img;
};
String winName="False color";
2017-11-01 04:10:29 +08:00
static const String ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer",
"Spring", "Cool", "HSV", "Pink", "Hot", "Parula", "User defined (random)"};
2016-12-16 05:55:59 +08:00
static void TrackColorMap(int x, void *r)
{
2017-11-01 04:10:29 +08:00
ParamColorMap *p = (ParamColorMap*)r;
2016-12-16 05:55:59 +08:00
Mat dst;
p->iColormap= x;
2017-11-01 04:10:29 +08:00
if (x == COLORMAP_PARULA + 1)
2016-12-16 05:55:59 +08:00
{
2017-11-01 04:10:29 +08:00
Mat lutRND(256, 1, CV_8UC3);
randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
applyColorMap(p->img, dst, lutRND);
2016-12-16 05:55:59 +08:00
}
else
applyColorMap(p->img,dst,p->iColormap);
2017-11-01 04:10:29 +08:00
putText(dst, "Colormap : "+ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
2016-12-16 05:55:59 +08:00
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;
}
2017-07-26 13:39:53 +08:00
int main(int argc, char** argv)
2016-12-16 05:55:59 +08:00
{
2017-11-01 04:10:29 +08:00
cout << "This program demonstrates the use of applyColorMap function.\n\n";
2016-12-16 05:55:59 +08:00
2017-11-01 04:10:29 +08:00
ParamColorMap p;
2017-07-26 13:39:53 +08:00
Mat img;
2017-11-01 04:10:29 +08:00
2017-07-26 13:39:53 +08:00
if (argc > 1)
2017-11-01 04:10:29 +08:00
img = imread(argv[1], IMREAD_GRAYSCALE);
2017-07-26 13:39:53 +08:00
else
img = DrawMyImage(2,256);
2017-11-01 04:10:29 +08:00
2016-12-16 05:55:59 +08:00
p.img=img;
p.iColormap=0;
imshow("Gray image",img);
namedWindow(winName);
createTrackbar("colormap", winName,&p.iColormap,1,TrackColorMap,(void*)&p);
2017-11-01 04:10:29 +08:00
setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
setTrackbarMax("colormap", winName, COLORMAP_PARULA+1);
2016-12-16 05:55:59 +08:00
setTrackbarPos("colormap", winName, -1);
2017-11-01 04:10:29 +08:00
TrackColorMap(0, (void*)&p);
cout << "Press a key to exit" << endl;
waitKey(0);
2016-12-16 05:55:59 +08:00
return 0;
2016-12-16 15:33:31 +08:00
}