-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageProcessing.hpp
More file actions
283 lines (259 loc) · 9.21 KB
/
Copy pathimageProcessing.hpp
File metadata and controls
283 lines (259 loc) · 9.21 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/** @file imageProcessing.hpp
*
* Copyright © 2022 Oregon State University
*
* Dominic W. Daprano
* Sheng Tse Tsai
* Moritz S. Schmid
* Christopher M. Sullivan
* Robert K. Cowen
*
* Hatfield Marine Science Center
* Center for Qualitative Life Sciences
* Oregon State University
* Corvallis, OR 97331
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This program is distributed under the GNU GPL v 2.0 or later license.
*
* Any User wishing to make commercial use of the Software must contact the authors
* or Oregon State University directly to arrange an appropriate license.
* Commercial use includes (1) use of the software for commercial purposes, including
* integrating or incorporating all or part of the source code into a product
* for sale or license by, or on behalf of, User to third parties, or (2) distribution
* of the binary or source code to third parties for use with a commercial
* product sold or licensed by, or on behalf of, User.
*
*/
#ifndef IMAGE_PROCESSING
#define IMAGE_PROCESSING
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp> // used for the video preprocessing
/**
* @class OverlapRects
*
* This is a modified version of the OverlapRectangles class that has different
* properties. Like the original, this class can be used to pass into the
* partition function in order to create a grouping of the rectangles
* accross the image.
*
* If eps < 1, then rectangles need to overlap more to be grouped
* If eps >= 1, then rectangles close to each other will be grouped
*
*/
class OverlapRects
{
public:
OverlapRects(double _eps) : eps(_eps) {}
inline bool operator()(const cv::Rect& r1, const cv::Rect& r2) const
{
double x1 = r1.x + r1.width * 0.5;
double x2 = r2.x + r2.width * 0.5;
double y1 = r1.y + r1.height * 0.5;
double y2 = r2.y + r2.height * 0.5;
double deltax = eps * (r1.width + r2.width) * 0.5;
double deltay = eps * (r1.height + r2.height) * 0.5;
return std::abs(x1 - x2) < deltax &&
std::abs(y1 - y2) < deltay;
}
double eps;
};
/**
* @struct Options
*
* Options struct allows parameters to be passed easily to each function.
*/
struct Options
{
std::string input;
std::string outputDirectory;
int numConcatenate;
int signalToNoise;
int minArea;
int maxArea;
float epsilon;
int delta;
int variation;
int threshold;
float outlierPercent;
bool fullOutput;
int top;
int bottom;
int left;
int right;
bool verboseMode;
bool verboseModePlus;
};
bool containExt(const std::string s, std::string arr[], int len);
bool isInt(std::string str);
/**
* Converts an integer to a string and adds padding 0's.
*
* @param number The number to add zeros.
* @param fill Add zeros at the beginning of the number until it is fill long.
*
* @return std::string object with concatenated 0's at the end.
*/
std::string convertInt(int number, int fill=4);
/**
* Rescales a cv::Rect around its' centroid.
*
* This function rescales each side of the rectangle by the scale factor around
* same centroid.
*
* @param rect The rectangle to scale
* @param scale The scale factor to scale the rectangle. A scale of 1 would
* leave the rectangle unchanged.
*
* @return The rescaled rectangle.
*/
cv::Rect rescaleRect(const cv::Rect& rect, float scale=.5);
/**
* Any pixel values above thresh is set to 255 (white).
*
* \begin{equation}
* dst =
* \begin{cases}
* 255 & \text{if } x < thresh \\
* src & \text{if } x > thresh
* \end{cases}
* \end{equation}
*
* @param src Input image.
* @param dst Output image.
* @param thresh Threshold to determine which pixels should be turned white
*
* @return A new cv::Rect that has the rescaled sides.
*/
void chopThreshold(const cv::Mat& src, cv::Mat& dst, int thresh);
/**
* Calculate the signal to noise ratio (SNR) for an image.
*
* \begin{equation}
* SNR = 20 * log(\frac{img_{signal}}{img_{noise}})
* \end{equation}
*
* @param img The image to calculate the SNR.
*
* @return SNR
*/
float SNR(const cv::Mat& img);
/**
* Performs flat fielding on the image.
*
* Using the outlier percentage determine the column-wise mean value. Divide
* the image by this columnwise mean value to reduce the overal noise of the image.
* Since the image is created from a linescan, this helps remove noise.
*
* @param src The image to perform flat fielding on.
* @param dst The destination image to save the results of flat fielding.
* @param percent The top and bottom percent to remove in the calculation of the mean.
* percent should be a float between 0 and 1.
*/
void flatField(const cv::Mat& src, cv::Mat& dst, float percent);
/**
* Fills the left and right columns of the input img with pixels with value fill.
* The original cv::Mat img is modified in place.
* This function was added for shadows that appeared on the side of isiis images.
*
* @param img The image to fill the left and right sides.
* @param left The number of columns on the left to fill.
* @param right The number of columns on the right to fill.
*
* @return If left and right are valid returns 0, if not returns 1.
*/
int fillSides(cv::Mat& img, int left, int right, int fill=255);
int fillTopBottom(cv::Mat& img, int top, int bottom, int fill=255);
/**
* Finds the verticle mean of each row of the iages.
*
* trimMean calculates the trimmed mean of each vertical strip of the image. trimMean
* will trim the top k brightest and darkest pixel values where k is given by
*
* \f[
* k = img.rows * \frac{percent}{2}
* \f]
*
* @param img The image to calculate the trimmed mean on.
* @param tMean A row vector containing the trimmed mean of each column of the image.
* @param percent The top and bottom percent to remove in the calculation of the mean.
* percent should be a float between 0 and 1.
*
*/
void trimMean(const cv::Mat& img, cv::Mat& tMean, float percent);
void preprocess(const cv::Mat& src, cv::Mat& dst, float erosion_size);
/**
* Different implementation of the OpenCV groupRectangles function.
*
* This function allows control over how much of the rectangles need to overlap
* in order to be joined into the same ROI
*
* @param rectList List of bounding bboxes to group
* @param groupThreshold The number of rectangles required in a grouping to make a grouped
* rectangle.
* @param eps The percent threshold to count boxes as overlapping.
*
*/
void groupRect(std::vector<cv::Rect>& rectList, int groupThreshold,
double eps);
/**
* Helper function to access the MSER method easier.
*
* @param img The source image to perform MSER on.
* @param bboxes The vector to store the bounding boxes in.
* @param delta Delta is the number of steps (changes in pixel brighness)
* MSER uses when comparing the size of connected regions.
* @param max_variation Maximum variation of the regions area between delta
* threshold.
* @param eps How much do the bounding boxes produced by MSER need to overlap
* so that they merge.
*
*/
void mser(const cv::Mat& img, std::vector<cv::Rect>& bboxes, int minArea=50,
int maxArea=400000, int delta=5, int max_variation=5, float eps=.3);
void contourBbox(const cv::Mat& img, std::vector<cv::Rect>& bboxes,
int threshold, int minArea, int maxArea, float eps);
/**
* Gets the next n frames from the video capture device capture device. Stores
* them in the Mat, img.
*
* @param cap The video capture device
* @param img The cv::Mat to store the n concatenated images in.
* @param n The number of frames to grab from the capture device.
* @param frameCounter REMOVE
*/
void getFrame(cv::VideoCapture cap, cv::Mat& img, int n);
/**
* Segment the image by performing fielding and preprocessing the image, then
* using the MSER algorithm create bounding boxes.
*
* @param img The cv::Mat image to segment.
* @param imgCorrect The cv::Mat to store the flatfielded and preprocessed image.
* @param bboxes The vector to store the bounding boxes produced by MSER.
* @param options The Options struct that is used to store all of the command line
* configurations.
*
*/
void segmentImage(const cv::Mat& img, cv::Mat& imgCorrect,
std::vector<cv::Rect>& bboxes, Options options);
/**
* Saves the segments produced by the segment image function.
*
* Crops the rectanbles, bboxes, out of the img and imgCorrect cv::Mat objects.
* saveCrops will then save these crops in the imgDir.
*
* @param img The original image.
* @param imgCorrect The flatfielded and preprocessed image.
* @param bboxes The bounding boxes generated by the MSER algorithm.
* @param imgDir The directory string to store the images.
* @param imgName The name to store the image under in the measure file and imgDir.
* @param measurePtr A ptr to the measure file. For example std::ofstream measurePtr(measureFile).
* @param options The Options struct that is used to store all of the command line
* configurations.
*/
void saveCrops(const cv::Mat& img, const cv::Mat& imgCorrect,
std::vector<cv::Rect>& bboxes, std::string imgDir,
std::string imgName, std::ofstream& measurePtr, Options options, std::ofstream& bboxPtr, std::ofstream& yoloPtr);
#endif