File: utils.h

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (114 lines) | stat: -rw-r--r-- 3,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef UTILS_H
#define UTILS_H

#include <opencv2/core.hpp>
#include <vector>
#include "stats.h"

using namespace std;
using namespace cv;

void drawBoundingBox(Mat image, vector<Point2f> bb);
void drawStatistics(Mat image, const Stats& stats);
void printStatistics(string name, Stats stats);
vector<Point2f> Points(vector<KeyPoint> keypoints);
Rect2d selectROI(const String &video_name, const Mat &frame);

void drawBoundingBox(Mat image, vector<Point2f> bb)
{
    for(unsigned i = 0; i < bb.size() - 1; i++) {
        line(image, bb[i], bb[i + 1], Scalar(0, 0, 255), 2);
    }
    line(image, bb[bb.size() - 1], bb[0], Scalar(0, 0, 255), 2);
}

void drawStatistics(Mat image, const Stats& stats)
{
    static const int font = FONT_HERSHEY_PLAIN;
    stringstream str1, str2, str3;

    str1 << "Matches: " << stats.matches;
    str2 << "Inliers: " << stats.inliers;
    str3 << "Inlier ratio: " << setprecision(2) << stats.ratio;

    putText(image, str1.str(), Point(0, image.rows - 90), font, 2, Scalar::all(255), 3);
    putText(image, str2.str(), Point(0, image.rows - 60), font, 2, Scalar::all(255), 3);
    putText(image, str3.str(), Point(0, image.rows - 30), font, 2, Scalar::all(255), 3);
}

void printStatistics(string name, Stats stats)
{
    cout << name << endl;
    cout << "----------" << endl;

    cout << "Matches " << stats.matches << endl;
    cout << "Inliers " << stats.inliers << endl;
    cout << "Inlier ratio " << setprecision(2) << stats.ratio << endl;
    cout << "Keypoints " << stats.keypoints << endl;
    cout << endl;
}

vector<Point2f> Points(vector<KeyPoint> keypoints)
{
    vector<Point2f> res;
    for(unsigned i = 0; i < keypoints.size(); i++) {
        res.push_back(keypoints[i].pt);
    }
    return res;
}

Rect2d selectROI(const String &video_name, const Mat &frame)
{
    struct Data
    {
        Point center;
        Rect2d box;

        static void mouseHandler(int event, int x, int y, int flags, void *param)
        {
            Data *data = (Data*)param;
            switch( event )
            {
            // start to select the bounding box
            case EVENT_LBUTTONDOWN:
                data->box = cvRect( x, y, 0, 0 );
                data->center = Point2f((float)x,(float)y);
                break;
            // update the selected bounding box
            case EVENT_MOUSEMOVE:
                if(flags == 1)
                {
                    data->box.width  = 2 * (x - data->center.x);
                    data->box.height = 2 * (y - data->center.y);
                    data->box.x = data->center.x - data->box.width / 2.0;
                    data->box.y = data->center.y - data->box.height / 2.0;
                }
                break;
            // cleaning up the selected bounding box
            case EVENT_LBUTTONUP:
                if( data->box.width < 0 )
                {
                    data->box.x += data->box.width;
                    data->box.width *= -1;
                }
                if( data->box.height < 0 )
                {
                    data->box.y += data->box.height;
                    data->box.height *= -1;
                }
                break;
            }
        }
    } data;

    setMouseCallback(video_name, Data::mouseHandler, &data);
    while(waitKey(1) < 0)
    {
        Mat draw = frame.clone();
        rectangle(draw, data.box, Scalar(255,0,0), 2, 1);
        imshow(video_name, draw);
    }
    return data.box;
}

#endif // UTILS_H