core.cpp 2.69 KB
Newer Older
Matteo's avatar
Matteo committed
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
#include "core.hpp"

namespace videoanalyser {
namespace core {

Frame::Frame() : cv::Mat() {}
Frame::Frame(const cv::Mat& m) : cv::Mat(m) {}
Frame::Frame(const Frame& f) : cv::Mat(f) {}
Frame& Frame::operator=(const Mat& m) {
    Mat::operator=(m);
    return *this;
}
Frame& Frame::operator=(const Frame& f) {
    Mat::operator=(f);
    return *this;
}

Frame Frame::clone() const { return Frame(cv::Mat::clone()); }
Frame& Frame::downsample(int factor) {
    cv::pyrDown(*this, *this, cv::Size(size().width / factor, size().height / factor));
    return *this;
}
Frame& Frame::convert_color(int code) {
    cv::cvtColor(*this, *this, code);
    return *this;
}
Frame Frame::difference(Frame& f) {
    Frame diff = this->clone();
    for (int i = 0; i < this->rows; i++) {
        for (int j = 0; j < this->cols; j++) {
            if (f.at<cv::Vec3b>(i, j)[0] != this->at<cv::Vec3b>(i, j)[0] ||
                f.at<cv::Vec3b>(i, j)[1] != this->at<cv::Vec3b>(i, j)[1] ||
                f.at<cv::Vec3b>(i, j)[2] != this->at<cv::Vec3b>(i, j)[2]) {
                // Different pixels
                diff.at<cv::Vec3b>(i, j)[0] = 0;
            } else {
                // Identical pixels
                diff.at<cv::Vec3b>(i, j)[0] = 255;
            }
        }
    }
    return diff;
}
Frame& Frame::crop(cv::Size rect_size, cv::Point2f center) {
    cv::getRectSubPix(*this, rect_size, center, *this);
    return *this;
}
Frame& Frame::warp(cv::Mat rotationMatrix) {
    cv::warpAffine(*this, *this, rotationMatrix, this->size(), cv::INTER_CUBIC);
    return *this;
}
std::pair<Frame, Frame> Frame::deinterlace() const {
    Frame odd_frame(cv::Mat(this->rows / 2, this->cols, CV_8UC3));
    Frame even_frame(cv::Mat(this->rows / 2, this->cols, CV_8UC3));

    int i_odd_frame = 0;
    int i_even_frame = 0;

    for (int i = 0; i < this->rows; i++) {
        for (int j = 0; j < this->cols; j++) {
            if (i % 2 == 0) {
                even_frame.at<cv::Vec3b>(i_even_frame, j)[0] = this->at<cv::Vec3b>(i, j)[0];
                even_frame.at<cv::Vec3b>(i_even_frame, j)[1] = this->at<cv::Vec3b>(i, j)[1];
                even_frame.at<cv::Vec3b>(i_even_frame, j)[2] = this->at<cv::Vec3b>(i, j)[2];
            } else {
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[0] = this->at<cv::Vec3b>(i, j)[0];
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[1] = this->at<cv::Vec3b>(i, j)[1];
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[2] = this->at<cv::Vec3b>(i, j)[2];
            }
        }

        if (i % 2 == 0) {
            i_even_frame++;
        } else {
            i_odd_frame++;
        }
    }
    return std::make_pair(odd_frame, even_frame);
}
}  // namespace core
}  // namespace videoanalyser