utility.h 6.78 KB
Newer Older
Matteo's avatar
Matteo committed
1
2
#ifndef UTILITY_H
#define UTILITY_H
Matteo's avatar
Matteo committed
3
#include <filesystem>
Matteo's avatar
update    
Matteo committed
4
5
6
7
8
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
9
using namespace cv;
10
using namespace std;
11
namespace fs = std::filesystem;
12

Matteo's avatar
Matteo committed
13
14
15
/**
 * @brief Namespace containing a set of utility functions used in the project.
 * The functions are mainly used to perform operations on images.
Matteo's avatar
Matteo committed
16
 *
Matteo's avatar
Matteo committed
17
 */
Matteo's avatar
update    
Matteo committed
18
namespace utility {
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
19

Matteo's avatar
Matteo committed
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
/**
 * @class Frame
 * @brief Class that extends the OpenCV Mat class, adding some useful methods
 * frequently used in the project.
 *
 */
class Frame : public Mat {
   public:
    Frame();
    Frame(const Mat& m);
    Frame(const Frame& f);
    Frame& operator=(const Mat& m);
    Frame& operator=(const Frame& f);
    Frame clone() const;
    /**
     * @brief Downsample the image by a given factor.
     *
     * @param factor The factor by which the image will be downsampled.
     * @return Frame& The downsampled image.
     */
    Frame& downsample(int factor);
    /**
     * @brief Convert the image to a given color space.
     *
     * @param code The code of the color space to which the image will be
     * converted.
     * @return Frame& The converted image.
     */
    Frame& convertColor(int code);
    Frame difference(Frame& f);
    /**
     * @brief Crop the image to a given size, centered in a given point.
     *
     * @param rect_size The size of the cropped image.
     * @param center The center of the cropped image.
     * @return Frame& The cropped image.
     */
    Frame& crop(Size rect_size, Point2f center);
    /**
     * @brief Warp the image using a given rotation matrix.
     *
     * @param rotationMatrix The rotation matrix used to warp the image.
     * @return Frame& The warped image.
     */
    Frame& warp(cv::Mat rotationMatrix);
    /**
     * @brief Deinterlace the image, returning two images, one containing the
     * odd lines and the other containing the even lines.
     *
     * @return std::pair<Frame, Frame> The two images containing the odd and
     * even lines.
     */
    std::pair<Frame, Frame> deinterlace() const;
};
Matteo's avatar
update    
Matteo committed
74

Matteo's avatar
Matteo committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * @fn void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int
 * posThresh, vector<Vec4f> &positivePositions, Mat &positiveVotes,
 * vector<Vec4f> &negativePositions, Mat &negativeVotes, Mat processingArea)
 * @brief Detects a given shape in an image, using a the OpenCV algorithm
 * GeneralizedHoughGuil.
 *
 * @param[in] alg the algorithm instance;
 * @param[in] templateShape the shape to detect;
 * @param[in] posThresh the position votes threshold;
 * @param[out] positivePositions vector representing the position assigned to
 * each found rectangle for positive angles;
 * @param[out] positiveVotes vector representing the vote assigned to each found
 * rectangle for positive angles;
 * @param[out] negativePositions vector representing the position assigned to
 * each found rectangle for negative angles;
 * @param[out] negativeVotes vector representing the vote assigned to each found
 * rectangle for negative angles;
 * @param[in] processingArea the image to be processed.
 */
void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh, vector<Vec4f>& positivePositions,
                 Mat& positiveVotes, vector<Vec4f>& negativePositions, Mat& negativeVotes, Mat processingArea);
Matteo's avatar
update    
Matteo committed
97

Matteo's avatar
Matteo committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * @fn RotatedRect drawShapes(Mat frame, Vec4f &positions, Scalar color, int
 * width, int height, int offsetX, int offsetY, float processingScale)
 * @brief Draw rectangles on an image.
 *
 * @param frame Frame on which the rectangles will be drawn;
 * @param positions The position of the rectangle;
 * @param color The color of the rectangle;
 * @param width The width of the rectangle;
 * @param height The height of the rectangle;
 * @param offsetX X offset on the position of the rectangle;
 * @param offsetY Y offset on the position of the rectangle;
 * @param processingScale Scaling factor, useful for downsizing.
 * @return RotatedRect Object representing the drawn rectangle.
 */
RotatedRect drawShapes(Mat frame, Vec4f& positions, Scalar color, int width, int height, int offsetX, int offsetY,
                       float processingScale);
Matteo's avatar
update    
Matteo committed
115

Matteo's avatar
Matteo committed
116
117
118
119
120
121
122
123
124
125
/**
 * @fn void separateFrame(cv::Mat frame, cv::Mat &odd_frame, cv::Mat
 * &even_frame)
 * @brief Function to deinterlace the current image.
 *
 * @param[in] frame image to be processed;
 * @param[out] odd_frame odd plane;
 * @param[out] even_frame even plane.
 */
void separateFrame(const cv::Mat frame, cv::Mat& odd_frame, cv::Mat& even_frame);
Matteo's avatar
update    
Matteo committed
126

Matteo's avatar
Matteo committed
127
128
129
130
131
132
133
134
135
136
137
138
/**
 * @fn void separateFrame(cv::Mat frame, cv::Mat &odd_frame, cv::Mat
 * &even_frame)
 * @brief Compute the number of different pixels between two frames.
 *
 * @param prevFrame the first frame;
 * @param currentFrame the second frame.
 * @return cv::Mat A black and white frame, where black pixels represent a
 * difference, while white pixels represent an equality.
 */
cv::Mat difference(cv::Mat& prevFrame, cv::Mat& currentFrame);
}  // namespace utility
139

Matteo's avatar
Matteo committed
140
141
142
/**
 * @struct Threshold
 * @brief Struct containing the threshold values used to detect a shape.
Matteo's avatar
Matteo committed
143
 *
Matteo's avatar
Matteo committed
144
 */
145
struct Threshold {
Matteo's avatar
Matteo committed
146
147
148
149
150
151
152
    float percentual; /**< The minimum percentage of different pixels for
                         considering the current frame under the ROI as a
                         potential Irregularity */
    int angle;        /**< The angle votes threshold for the detection of the object */
    int scale;        /**< The scale votes threshold for the detection of the object */
    int pos;          /**< The position votes threshold for the detection of the object
                       */
Matteo's avatar
Matteo committed
153
154
155
156
157
158
159

    Threshold(){};
    /**
     * @brief Construct a new Threshold object
     * @throws std::invalid_argument if the percentual is not in the range [0, 1]
     */
    Threshold(float percentual, int angle, int scale, int pos);
160
161
};

Matteo's avatar
Matteo committed
162
/**
Matteo's avatar
Matteo committed
163
 * @enum ROI
Matteo's avatar
Matteo committed
164
 * @brief Enum containing the possible objects to detect.
Matteo's avatar
Matteo committed
165
 *
Matteo's avatar
Matteo committed
166
 */
Matteo's avatar
Matteo committed
167
enum ROI { TAPE, CAPSTAN };
168

Matteo's avatar
Matteo committed
169
170
/**
 * @struct SceneObject
Matteo's avatar
Matteo committed
171
172
 * @brief A scene object is an object that can be detected in a scene, such as a
 * tape or a capstan.
173
 *
Matteo's avatar
Matteo committed
174
 */
175
struct SceneObject {
Matteo's avatar
Matteo committed
176
177
178
    int minDist;         /**< The minimum distance between the centers of the detected
                            objects for the detection of the reading head */
    Threshold threshold; /**<  the threshold values used to detect the object */
179

Matteo's avatar
Matteo committed
180
    SceneObject(){};
Matteo's avatar
Matteo committed
181
182
183
184
185
186
187
188
189
190
191
    SceneObject(int minDist, Threshold threshold);
    ~SceneObject() = default;
    /**
     * @fn static SceneObject from_file(fs::path path, Object obj)
     * @brief Create a SceneObject from a given file.
     *
     * @param path The path of the file containing the object.
     * @note The file must be a JSON file.
     * @param obj The object to detect.
     * @return SceneObject The SceneObject created from the file.
     */
Matteo's avatar
Matteo committed
192
    static SceneObject from_file(fs::path path, ROI obj);
193
};
Matteo's avatar
Matteo committed
194
#endif  // UTILITY_H