summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt/module/frame.cpp
blob: 8549e5fd1b925d2698925a458ae1c3266b6bceea (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
#include "frame.hpp"

#include "compat/math.hpp"

#include <opencv2/imgproc.hpp>

namespace pt_module {

void Preview::set_last_frame(const pt_frame& frame_)
{
    const cv::Mat& frame = frame_.as_const<const Frame>()->mat;
    const bool need_resize = frame.size != frame_copy.size;

    if (frame.channels() == 1)
    {
        if (need_resize)
        {
            frame_tmp.create(frame.size(), CV_8UC3);
            cv::cvtColor(frame, frame_tmp, cv::COLOR_GRAY2BGR);
            cv::resize(frame_tmp, frame_copy, frame_copy.size(), 0, 0, cv::INTER_NEAREST);
        }
        else
            cv::cvtColor(frame, frame_copy, cv::COLOR_GRAY2BGR);
    }
    else if (frame.channels() == 3)
    {
        if (need_resize)
            cv::resize(frame, frame_copy, frame_copy.size(), 0, 0, cv::INTER_NEAREST);
        else
            frame.copyTo(frame_copy);
    }
    else
    {
        eval_once(qDebug() << "tracker/pt: camera frame depth" << frame.channels() << "!= 3");
        frame_copy.create(frame_copy.size(), CV_8UC3);
        frame_copy.setTo({0});
    }
}

Preview::Preview(int w, int h)
{
    frame_out.create(h, w, CV_8UC4);
    frame_copy.create(h, w, CV_8UC3);
    frame_copy.setTo({0});
}

QImage Preview::get_bitmap()
{
    int stride = (int)frame_out.step.p[0];

    if (stride < frame_out.cols * 4)
    {
        eval_once(qDebug() << "bad stride" << stride
                           << "for bitmap size" << frame_copy.cols << frame_copy.rows);
        return QImage();
    }

    cv::cvtColor(frame_copy, frame_out, cv::COLOR_BGR2BGRA);

    return QImage((const unsigned char*) frame_out.data,
                  frame_out.cols, frame_out.rows,
                  stride,
                  QImage::Format_ARGB32);
}

void Preview::draw_head_center(f x, f y)
{
    auto [px_, py_] = to_pixel_pos(x, y, frame_copy.cols, frame_copy.rows);

    int px = iround(px_), py = iround(py_);

    constexpr int len = 9;

    static const cv::Scalar color(0, 255, 255);
    cv::line(frame_copy,
             cv::Point(px - len, py),
             cv::Point(px + len, py),
             color, 1);
    cv::line(frame_copy,
             cv::Point(px, py - len),
             cv::Point(px, py + len),
             color, 1);
}

} // ns pt_module