drawing.cpp 5.33 KB
Newer Older
1 2 3
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
4
#include <stdio.h>
5

6 7
using namespace cv;

8
static void help()
Gary Bradski's avatar
Gary Bradski committed
9
{
10 11 12
    printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"
    "Usage:\n"
    "   ./drawing\n");
Gary Bradski's avatar
Gary Bradski committed
13
}
14 15 16 17 18 19
static Scalar randomColor(RNG& rng)
{
    int icolor = (unsigned)rng;
    return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
}

20
int main()
21
{
22
    help();
23
    char wndname[] = "Drawing Demo";
24 25
    const int NUMBER = 100;
    const int DELAY = 5;
26
    int lineType = LINE_AA; // change it to LINE_8 to see non-antialiased graphics
27 28
    int i, width = 1000, height = 700;
    int x1 = -width/2, x2 = width*3/2, y1 = -height/2, y2 = height*3/2;
29
    RNG rng(0xFFFFFFFF);
30 31 32 33 34

    Mat image = Mat::zeros(height, width, CV_8UC3);
    imshow(wndname, image);
    waitKey(DELAY);

35
    for (i = 0; i < NUMBER * 2; i++)
36 37 38 39 40 41 42
    {
        Point pt1, pt2;
        pt1.x = rng.uniform(x1, x2);
        pt1.y = rng.uniform(y1, y2);
        pt2.x = rng.uniform(x1, x2);
        pt2.y = rng.uniform(y1, y2);

43 44 45 46 47 48
        int arrowed = rng.uniform(0, 6);

        if( arrowed < 3 )
            line( image, pt1, pt2, randomColor(rng), rng.uniform(1,10), lineType );
        else
            arrowedLine(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), lineType);
49

50 51 52 53 54
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

55
    for (i = 0; i < NUMBER * 2; i++)
56 57 58 59 60 61 62
    {
        Point pt1, pt2;
        pt1.x = rng.uniform(x1, x2);
        pt1.y = rng.uniform(y1, y2);
        pt2.x = rng.uniform(x1, x2);
        pt2.y = rng.uniform(y1, y2);
        int thickness = rng.uniform(-3, 10);
63 64
        int marker = rng.uniform(0, 10);
        int marker_size = rng.uniform(30, 80);
65

66 67 68 69
        if (marker > 5)
            rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);
        else
            drawMarker(image, pt1, randomColor(rng), marker, marker_size );
70

71 72 73 74
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }
75

76 77 78 79 80 81 82 83 84 85 86 87
    for (i = 0; i < NUMBER; i++)
    {
        Point center;
        center.x = rng.uniform(x1, x2);
        center.y = rng.uniform(y1, y2);
        Size axes;
        axes.width = rng.uniform(0, 200);
        axes.height = rng.uniform(0, 200);
        double angle = rng.uniform(0, 180);

        ellipse( image, center, axes, angle, angle - 100, angle + 200,
                 randomColor(rng), rng.uniform(-1,9), lineType );
88

89 90 91 92 93 94 95 96 97 98
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

    for (i = 0; i< NUMBER; i++)
    {
        Point pt[2][3];
        pt[0][0].x = rng.uniform(x1, x2);
        pt[0][0].y = rng.uniform(y1, y2);
99 100
        pt[0][1].x = rng.uniform(x1, x2);
        pt[0][1].y = rng.uniform(y1, y2);
101 102
        pt[0][2].x = rng.uniform(x1, x2);
        pt[0][2].y = rng.uniform(y1, y2);
103
        pt[1][0].x = rng.uniform(x1, x2);
104
        pt[1][0].y = rng.uniform(y1, y2);
105
        pt[1][1].x = rng.uniform(x1, x2);
106
        pt[1][1].y = rng.uniform(y1, y2);
107
        pt[1][2].x = rng.uniform(x1, x2);
108 109 110
        pt[1][2].y = rng.uniform(y1, y2);
        const Point* ppt[2] = {pt[0], pt[1]};
        int npt[] = {3, 3};
111

112
        polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);
113

114 115 116 117
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }
118

119 120 121 122 123
    for (i = 0; i< NUMBER; i++)
    {
        Point pt[2][3];
        pt[0][0].x = rng.uniform(x1, x2);
        pt[0][0].y = rng.uniform(y1, y2);
124 125
        pt[0][1].x = rng.uniform(x1, x2);
        pt[0][1].y = rng.uniform(y1, y2);
126 127
        pt[0][2].x = rng.uniform(x1, x2);
        pt[0][2].y = rng.uniform(y1, y2);
128
        pt[1][0].x = rng.uniform(x1, x2);
129
        pt[1][0].y = rng.uniform(y1, y2);
130
        pt[1][1].x = rng.uniform(x1, x2);
131
        pt[1][1].y = rng.uniform(y1, y2);
132
        pt[1][2].x = rng.uniform(x1, x2);
133 134 135
        pt[1][2].y = rng.uniform(y1, y2);
        const Point* ppt[2] = {pt[0], pt[1]};
        int npt[] = {3, 3};
136

137
        fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
138

139 140 141 142 143 144 145 146 147 148
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

    for (i = 0; i < NUMBER; i++)
    {
        Point center;
        center.x = rng.uniform(x1, x2);
        center.y = rng.uniform(y1, y2);
149

150 151
        circle(image, center, rng.uniform(0, 300), randomColor(rng),
               rng.uniform(-1, 9), lineType);
152

153 154 155 156 157 158 159 160 161 162 163 164 165
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

    for (i = 1; i < NUMBER; i++)
    {
        Point org;
        org.x = rng.uniform(x1, x2);
        org.y = rng.uniform(y1, y2);

        putText(image, "Testing text rendering", org, rng.uniform(0,8),
                rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
166

167 168 169 170 171
        imshow(wndname, image);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

172
    Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
173
    Point org((width - textsize.width)/2, (height - textsize.height)/2);
174

175 176 177 178
    Mat image2;
    for( i = 0; i < 255; i += 2 )
    {
        image2 = image - Scalar::all(i);
179
        putText(image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
180
                Scalar(i, i, 255), 5, lineType);
181

182 183 184 185 186 187 188 189
        imshow(wndname, image2);
        if(waitKey(DELAY) >= 0)
            return 0;
    }

    waitKey();
    return 0;
}