falsecolor.cpp 3.5 KB
Newer Older
1 2 3 4
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
L
LaurentBerger 已提交
5 6 7 8

using namespace cv;
using namespace std;

L
LaurentBerger 已提交
9
enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
L
LaurentBerger 已提交
10

11
struct ParamColorMap {
L
LaurentBerger 已提交
12 13 14 15 16
    int iColormap;
    Mat img;
};

String winName="False color";
S
Suleyman TURKMEN 已提交
17 18
static const String ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer", "Spring",
                                    "Cool", "HSV", "Pink", "Hot", "Parula", "Magma", "Inferno", "Plasma", "Viridis",
19
                                    "Cividis", "Twilight", "Twilight Shifted", "Turbo", "Deep Green", "User defined (random)" };
L
LaurentBerger 已提交
20 21 22

static void TrackColorMap(int x, void *r)
{
23
    ParamColorMap *p = (ParamColorMap*)r;
L
LaurentBerger 已提交
24 25
    Mat dst;
    p->iColormap= x;
26
    if (x == COLORMAP_DEEPGREEN + 1)
L
LaurentBerger 已提交
27
    {
28 29 30
        Mat lutRND(256, 1, CV_8UC3);
        randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
        applyColorMap(p->img, dst, lutRND);
L
LaurentBerger 已提交
31 32 33
    }
    else
        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);
L
LaurentBerger 已提交
36 37 38 39 40 41 42 43
    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;
44
    int lineLength = 50;
L
LaurentBerger 已提交
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);
L
LaurentBerger 已提交
48 49 50 51 52 53 54 55 56 57 58 59
    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:
60
            center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
L
LaurentBerger 已提交
61 62 63 64
            radius = r.uniform(1, min(offsetx, offsety));
            circle(img,center,radius,Scalar(i),-1);
            break;
        case MyRECTANGLE:
65
            center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
L
LaurentBerger 已提交
66 67 68 69 70 71
            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:
72
            center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
L
LaurentBerger 已提交
73 74 75 76 77 78 79 80 81 82
            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;
}

83
int main(int argc, char** argv)
L
LaurentBerger 已提交
84
{
85
    cout << "This program demonstrates the use of applyColorMap function.\n\n";
L
LaurentBerger 已提交
86

87
    ParamColorMap  p;
88
    Mat img;
89

90
    if (argc > 1)
91
        img = imread(samples::findFile(argv[1]), IMREAD_GRAYSCALE);
92 93
    else
        img = DrawMyImage(2,256);
94

L
LaurentBerger 已提交
95 96 97 98 99
    p.img=img;
    p.iColormap=0;

    imshow("Gray image",img);
    namedWindow(winName);
100
    createTrackbar("colormap", winName, NULL, COLORMAP_DEEPGREEN + 1, TrackColorMap, (void*)&p);
101
    setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
102 103
    setTrackbarMax("colormap", winName, COLORMAP_DEEPGREEN + 1);
    setTrackbarPos("colormap", winName, COLORMAP_AUTUMN);
L
LaurentBerger 已提交
104

105 106 107 108
    TrackColorMap(0, (void*)&p);

    cout << "Press a key to exit" << endl;
    waitKey(0);
L
LaurentBerger 已提交
109
    return 0;
L
LaurentBerger 已提交
110
}