summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt/camera.cpp
blob: 89854cc7213d1798fd0b8ba9cf6190a7888dada0 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (c) 2012 Patrick Ruoff
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include "camera.h"
#include "compat/sleep.hpp"

namespace impl {

QString Camera::get_desired_name() const
{
    return desired_name;
}

QString Camera::get_active_name() const
{
    return active_name;
}

void CamInfo::get_focal_length(f& fx) const
{
    using std::tan;
    using std::atan;
    using std::sqrt;

    const double diag_len = sqrt(double(res_x*res_x + res_y*res_y));
    const double aspect_x = res_x / diag_len;
    //const double aspect_y = res_y / diag_len;
    const double diag_fov = fov * M_PI/180;
    const double fov_x = 2*atan(tan(diag_fov*.5) * aspect_x);
    //const double fov_y = 2*atan(tan(diag_fov*.5) * aspect_y);
    fx = .5 / tan(fov_x * .5);
    //fy = .5 / tan(fov_y * .5);
    //static bool once = false; if (!once) { once = true; qDebug() << "f" << ret << "fov" << (fov * 180/M_PI); }
}

DEFUN_WARN_UNUSED bool Camera::get_info(CamInfo& ret) const
{
    if (cam_info.res_x == 0 || cam_info.res_y == 0)
        return false;
    ret = cam_info;
    return true;
}

DEFUN_WARN_UNUSED bool Camera::get_frame(double dt, cv::Mat& frame, CamInfo& info)
{
    bool new_frame = _get_frame(frame);

    // measure fps of valid frames
    static constexpr double RC = .1; // seconds
    const double alpha = dt/(dt + RC);
    dt_valid += dt;

    if (new_frame)
    {
        if (dt_mean < 2e-3)
            dt_mean = dt;
        else
            dt_mean = (1-alpha) * dt_mean + alpha * dt_valid;

        cam_info.fps = dt_mean > 2e-3 ? int(1 / dt_mean + .65) : 0;
        cam_info.res_x = frame.cols;
        cam_info.res_y = frame.rows;
        cam_info.fov = s.fov;

        info = cam_info;

        dt_valid = 0;
    }
    else
        qDebug() << "pt camera: can't get frame";
    return new_frame;
}

DEFUN_WARN_UNUSED bool Camera::start(int idx, int fps, int res_x, int res_y)
{
    if (idx >= 0 && fps >= 0 && res_x > 0 && res_y > 0)
    {
        if (!cap || cap->isOpened() ||
            cam_desired.idx != idx ||
            cam_desired.fps != fps ||
            cam_desired.res_x != res_x ||
            cam_desired.res_y != res_y)
        {
            qDebug() << "pt: opening camera";

            cam_desired.idx = idx;
            cam_desired.fps = fps;
            cam_desired.res_x = res_x;
            cam_desired.res_y = res_y;

            cap = camera_ptr(new cv::VideoCapture(cam_desired.idx));

            cap->set(cv::CAP_PROP_FRAME_WIDTH,  cam_desired.res_x);
            cap->set(cv::CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
            cap->set(cv::CAP_PROP_FPS, cam_desired.fps);

            if (cap->isOpened())
            {
                cam_info = CamInfo();
                cam_info.idx = cam_desired.idx;
                active_name = desired_name;

                return true;
            }
        }
    }

    return stop(), false;
}

void Camera::stop()
{
    cap = nullptr;
    desired_name = QString();
    active_name = QString();
    cam_info = CamInfo();
    cam_desired = CamInfo();
}

DEFUN_WARN_UNUSED bool Camera::_get_frame(cv::Mat& frame)
{
    if (cap && cap->isOpened())
    {
        for (int i = 0; i < 5 && !cap->read(frame); i++)
            portable::sleep(100);

        if (frame.empty())
            return false;

        return true;
    }
    return false;
}

void Camera::camera_deleter::operator()(cv::VideoCapture* cap)
{
    if (cap)
    {
        if (cap->isOpened())
            cap->release();
        std::default_delete<cv::VideoCapture>()(cap);
    }
}

} // ns impl