utility.cpp 8.38 KB
Newer Older
Matteo's avatar
Matteo committed
1
2
#include "utility.h"

3
4
5
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
Matteo's avatar
update  
Matteo committed
6
7
8

using namespace cv;
using namespace std;
Matteo's avatar
Matteo committed
9
namespace fs = std::filesystem;
Matteo's avatar
update  
Matteo committed
10

11
12
using json = nlohmann::json;

Matteo's avatar
update    
Matteo committed
13
14
15
16
17
18
// Constructors
utility::Frame::Frame() : Mat() {}
utility::Frame::Frame(const Mat& m) : Mat(m) {}
utility::Frame::Frame(const Frame& f) : Mat(f) {}
// Operators
utility::Frame& utility::Frame::operator=(const Mat& m) {
Matteo's avatar
Matteo committed
19
20
    Mat::operator=(m);
    return *this;
Matteo's avatar
update    
Matteo committed
21
22
}
utility::Frame& utility::Frame::operator=(const Frame& f) {
Matteo's avatar
Matteo committed
23
24
    Mat::operator=(f);
    return *this;
Matteo's avatar
update    
Matteo committed
25
26
}
// Methods
Matteo's avatar
Matteo committed
27
utility::Frame utility::Frame::clone() const { return utility::Frame(Mat::clone()); }
Matteo's avatar
update    
Matteo committed
28
utility::Frame& utility::Frame::downsample(int factor) {
Matteo's avatar
Matteo committed
29
30
    pyrDown(*this, *this, Size(size().width / factor, size().height / factor));
    return *this;
Matteo's avatar
update    
Matteo committed
31
32
}
utility::Frame& utility::Frame::convertColor(int code) {
Matteo's avatar
Matteo committed
33
34
    cvtColor(*this, *this, code);
    return *this;
Matteo's avatar
Matteo committed
35
}
Matteo's avatar
Matteo committed
36
utility::Frame utility::Frame::difference(Frame& f) { return Frame(utility::difference(*this, f)); }
Matteo's avatar
Matteo committed
37
utility::Frame& utility::Frame::crop(Size rect_size, Point2f center) {
Matteo's avatar
Matteo committed
38
39
    cv::getRectSubPix(*this, rect_size, center, *this);
    return *this;
Matteo's avatar
Matteo committed
40
41
}
utility::Frame& utility::Frame::warp(cv::Mat rotationMatrix) {
Matteo's avatar
Matteo committed
42
43
    cv::warpAffine(*this, *this, rotationMatrix, this->size(), INTER_CUBIC);
    return *this;
Matteo's avatar
Matteo committed
44
}
45
pair<utility::Frame, utility::Frame> utility::Frame::deinterlace() const {
Matteo's avatar
Matteo committed
46
47
    Frame odd_frame(cv::Mat(this->rows / 2, this->cols, CV_8UC3));
    Frame even_frame(cv::Mat(this->rows / 2, this->cols, CV_8UC3));
48

Matteo's avatar
Matteo committed
49
    utility::separateFrame(*this, odd_frame, even_frame);
50

Matteo's avatar
Matteo committed
51
    return make_pair(odd_frame, even_frame);
52
}
Matteo's avatar
update    
Matteo committed
53

Matteo's avatar
Matteo committed
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
void utility::detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh,
                          vector<Vec4f>& positivePositions, Mat& positiveVotes, vector<Vec4f>& negativePositions,
                          Mat& negativeVotes, Mat processingArea) {
    alg->setPosThresh(posThresh);
    alg->setTemplate(templateShape);

    int oldSizePositive = 0;
    int i = 0;
    int maxVote = 0;

    // Process shapes with positive angles
    alg->setMinAngle(0);
    alg->setMaxAngle(3);
    while (true) {
        alg->detect(processingArea, positivePositions, positiveVotes);
        int currentSize = positivePositions.size();
        if (currentSize == 1) {
            // We detected the most interesting shape
            break;
        } else if (currentSize == 0 && oldSizePositive > 0) {
            // It is not possible to detect only one shape with the current
            // parameters
            alg->setPosThresh(posThresh + i - 1);  // Decrease position value
            alg->detect(processingArea, positivePositions,
                        positiveVotes);  // Detect all available shapes
            break;
        } else if (currentSize == 0 && oldSizePositive == 0) {
            // Impossible to find with these parameters
            break;
        }
        oldSizePositive = currentSize;
        // Find maximum vote
        for (int j = 0; j < positiveVotes.cols / 3; j++) {
            if (positiveVotes.at<int>(3 * j) > maxVote) maxVote = positiveVotes.at<int>(3 * j);
        }

        if (currentSize > 10) {
            i += 5;  // To speed up computation when there are too many matches
        } else if (maxVote - (posThresh + i) > 100) {
            i += 100;  // To speed up computation when there are few super high
                       // matches
        } else {
            i++;
        }
        alg->setPosThresh(posThresh + i);
    }

    int oldSizeNegative = 0;
    // Reset incremental position value
    i = 0;
    maxVote = 0;
    // Process shapes with negative angles
    alg->setMinAngle(357);
    alg->setMaxAngle(360);
    while (true) {
        alg->detect(processingArea, negativePositions, negativeVotes);
        int currentSize = negativePositions.size();
        if (currentSize == 1) {
            // We detected the most interesting shape
            break;
        } else if (currentSize == 0 && oldSizeNegative > 0) {
            // It is not possible to detect only one shape with the current
            // parameters
            alg->setPosThresh(posThresh + i - 1);  // Decrease position value
            alg->detect(processingArea, negativePositions,
                        negativeVotes);  // Detect all available shapes
            break;
        } else if (currentSize == 0 && oldSizeNegative == 0) {
            // Impossible to found with these parameters
            break;
        }
        oldSizeNegative = currentSize;

        // Find maximum vote
        for (int j = 0; j < positiveVotes.cols / 3; j++) {
            if (positiveVotes.at<int>(3 * j) > maxVote) maxVote = positiveVotes.at<int>(3 * j);
        }

        if (currentSize > 10) {
            i += 5;  // To speed up computation when there are too many matches
        } else if (maxVote - (posThresh + i) > 100) {
            i += 100;  // To speed up computation when there are few super high
                       // matches
        } else {
            i++;
        }
        alg->setPosThresh(posThresh + i);
    }
Matteo's avatar
update  
Matteo committed
142
143
}

Matteo's avatar
Matteo committed
144
RotatedRect utility::drawShapes(Mat frame, const Vec4f& positions, Scalar color, int width, int height, int offsetX,
Matteo's avatar
Matteo committed
145
                                int offsetY, float processingScale) {
Matteo's avatar
update  
Matteo committed
146
147
148
    RotatedRect rr;
    Point2f rrpts[4];

Matteo's avatar
Matteo committed
149
    Point2f pos(positions[0] + offsetX, positions[1] + offsetY);
Matteo's avatar
update  
Matteo committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    float scale = positions[2];
    float angle = positions[3];

    rr.center = pos * processingScale;
    rr.size = Size2f(width * scale * processingScale, height * scale * processingScale);
    rr.angle = angle;

    rr.points(rrpts);

    line(frame, rrpts[0], rrpts[1], color, 2);
    line(frame, rrpts[1], rrpts[2], color, 2);
    line(frame, rrpts[2], rrpts[3], color, 2);
    line(frame, rrpts[3], rrpts[0], color, 2);

    return rr;
}

Matteo's avatar
Matteo committed
167
void utility::separateFrame(const cv::Mat frame, cv::Mat& odd_frame, cv::Mat& even_frame) {
Matteo's avatar
update  
Matteo committed
168
169
170
171
172
173
    int i_odd_frame = 0;
    int i_even_frame = 0;

    for (int i = 0; i < frame.rows; i++) {
        for (int j = 0; j < frame.cols; j++) {
            if (i % 2 == 0) {
Matteo's avatar
Matteo committed
174
175
176
                even_frame.at<cv::Vec3b>(i_even_frame, j)[0] = frame.at<cv::Vec3b>(i, j)[0];
                even_frame.at<cv::Vec3b>(i_even_frame, j)[1] = frame.at<cv::Vec3b>(i, j)[1];
                even_frame.at<cv::Vec3b>(i_even_frame, j)[2] = frame.at<cv::Vec3b>(i, j)[2];
Matteo's avatar
update  
Matteo committed
177
            } else {
Matteo's avatar
Matteo committed
178
179
180
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[0] = frame.at<cv::Vec3b>(i, j)[0];
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[1] = frame.at<cv::Vec3b>(i, j)[1];
                odd_frame.at<cv::Vec3b>(i_odd_frame, j)[2] = frame.at<cv::Vec3b>(i, j)[2];
Matteo's avatar
update  
Matteo committed
181
182
183
184
185
186
187
188
189
190
191
192
193
            }
        }

        if (i % 2 == 0) {
            i_even_frame++;
        } else {
            i_odd_frame++;
        }
    }

    return;
}

Matteo's avatar
Matteo committed
194
cv::Mat utility::difference(cv::Mat& prevFrame, cv::Mat& currentFrame) {
Matteo's avatar
update  
Matteo committed
195
196
197
    cv::Mat diff = currentFrame.clone();
    for (int i = 0; i < currentFrame.rows; i++) {
        for (int j = 0; j < currentFrame.cols; j++) {
Matteo's avatar
Matteo committed
198
199
200
            if (prevFrame.at<cv::Vec3b>(i, j)[0] != currentFrame.at<cv::Vec3b>(i, j)[0] ||
                prevFrame.at<cv::Vec3b>(i, j)[1] != currentFrame.at<cv::Vec3b>(i, j)[1] ||
                prevFrame.at<cv::Vec3b>(i, j)[2] != currentFrame.at<cv::Vec3b>(i, j)[2]) {
Matteo's avatar
update  
Matteo committed
201
202
203
204
205
206
207
208
209
                // Different pixels
                diff.at<cv::Vec3b>(i, j)[0] = 0;
            } else {
                // Identical pixels
                diff.at<cv::Vec3b>(i, j)[0] = 255;
            }
        }
    }
    return diff;
210
211
}

Matteo's avatar
Matteo committed
212
213
214
215
216
217
218
219
220
Threshold::Threshold(float percentual, int angle, int scale, int pos) {
    if (percentual < 0 || percentual > 100) throw std::invalid_argument("Percentual must be between 0 and 100");

    this->percentual = percentual;
    this->angle = angle;
    this->scale = scale;
    this->pos = pos;
}

221
SceneObject::SceneObject(int minDist, Threshold threshold) {
Matteo's avatar
Matteo committed
222
223
    this->minDist = minDist;
    this->threshold = threshold;
224
225
}

Matteo's avatar
Matteo committed
226
SceneObject SceneObject::from_file(fs::path path, ROI obj) {
Matteo's avatar
Matteo committed
227
228
229
230
    ifstream iConfig(path);
    json j;
    iConfig >> j;

Matteo's avatar
Matteo committed
231
    if (obj == ROI::TAPE) {
Matteo's avatar
Matteo committed
232
        return SceneObject(j["MinDist"],
Matteo's avatar
Matteo committed
233
                           Threshold(j["TapeThresholdPercentual"], j["AngleThresh"], j["ScaleThresh"], j["PosThresh"]));
Matteo's avatar
Matteo committed
234
    } else {
Matteo's avatar
Matteo committed
235
236
        return SceneObject(j["MinDistCapstan"], Threshold(j["CapstanThresholdPercentual"], j["AngleThreshCapstan"],
                                                          j["ScaleThreshCapstan"], j["PosThreshCapstan"]));
Matteo's avatar
Matteo committed
237
    }
Matteo's avatar
update  
Matteo committed
238
}