Skip to content

Commit 08699f1

Browse files
committed
Merge pull request #106 from mtamburrano/adas_fix
adas fix
2 parents 2ce3606 + 7f97464 commit 08699f1

File tree

10 files changed

+467
-120
lines changed

10 files changed

+467
-120
lines changed

modules/adas/tools/fcw_detect/fcw_detect.cpp

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,29 @@ static Mat visualize(const Mat &image, const vector<Rect> &objects)
4848
}
4949
return img;
5050
}
51+
static bool read_window_size(const char *str, int *rows, int *cols)
52+
{
53+
int pos = 0;
54+
if( sscanf(str, "%dx%d%n", rows, cols, &pos) != 2 || str[pos] != '\0' ||
55+
*rows <= 0 || *cols <= 0)
56+
{
57+
return false;
58+
}
59+
return true;
60+
}
5161

5262
int main(int argc, char *argv[])
5363
{
5464
const string keys =
55-
"{help | | print this message}"
56-
"{model_filename | model.xml | filename for reading model}"
57-
"{image_path | test.png | path to image for detection}"
58-
"{out_image_path | out.png | path to image for output}"
59-
"{threshold | 0.0 | threshold for cascade}"
65+
"{help | | print this message}"
66+
"{model_filename | model.xml | filename for reading model}"
67+
"{image_path | test.png | path to image for detection}"
68+
"{out_image_path | out.png | path to image for output}"
69+
"{threshold | 0.0 | threshold for cascade}"
70+
"{step | 8 | sliding window step}"
71+
"{min_window_size | 40x40 | min window size in pixels}"
72+
"{max_window_size | 300x300 | max window size in pixels}"
73+
"{is_grayscale | false | read the image as grayscale}"
6074
;
6175

6276
CommandLineParser parser(argc, argv, keys);
@@ -71,7 +85,31 @@ int main(int argc, char *argv[])
7185
string model_filename = parser.get<string>("model_filename");
7286
string image_path = parser.get<string>("image_path");
7387
string out_image_path = parser.get<string>("out_image_path");
88+
bool is_grayscale = parser.get<bool>("is_grayscale");
7489
float threshold = parser.get<float>("threshold");
90+
int step = parser.get<int>("step");
91+
92+
int min_rows, min_cols, max_rows, max_cols;
93+
string min_window_size = parser.get<string>("min_window_size");
94+
if( !read_window_size(min_window_size.c_str(), &min_rows,
95+
&min_cols) )
96+
{
97+
cerr << "Error reading min window size from `" << min_window_size << "`" << endl;
98+
return 1;
99+
}
100+
string max_window_size = parser.get<string>("max_window_size");
101+
if( !read_window_size(max_window_size.c_str(), &max_rows,
102+
&max_cols) )
103+
{
104+
cerr << "Error reading max window size from `" << max_window_size << "`" << endl;
105+
return 1;
106+
}
107+
108+
int color;
109+
if(is_grayscale == false)
110+
color = cv::IMREAD_COLOR;
111+
else
112+
color = cv::IMREAD_GRAYSCALE;
75113

76114

77115
if( !parser.check() )
@@ -85,8 +123,10 @@ int main(int argc, char *argv[])
85123
detector.read(fs["icfdetector"]);
86124
fs.release();
87125
vector<Rect> objects;
88-
Mat img = imread(image_path);
89-
detector.detect(img, objects, 1.1f, Size(40, 40),
90-
Size(300, 300), threshold);
126+
Mat img = imread(image_path, color);
127+
std::vector<float> values;
128+
detector.detect(img, objects, 1.1f, Size(min_cols, min_rows), Size(max_cols, max_rows), threshold, step, values);
91129
imwrite(out_image_path, visualize(img, objects));
130+
131+
92132
}

modules/adas/tools/fcw_train/fcw_train.cpp

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ using cv::imread;
2626
#include <opencv2/core/utility.hpp>
2727
using cv::CommandLineParser;
2828
using cv::FileStorage;
29+
#include <opencv2/core/utility.hpp>
30+
31+
#include <ctime> // std::time
32+
#include <cstdlib> // std::rand, std::srand
2933

3034
#include <opencv2/xobjdetect.hpp>
3135

36+
3237
using cv::xobjdetect::ICFDetectorParams;
3338
using cv::xobjdetect::ICFDetector;
3439
using cv::xobjdetect::WaldBoost;
@@ -46,8 +51,11 @@ static bool read_model_size(const char *str, int *rows, int *cols)
4651
return true;
4752
}
4853

54+
static int randomPred (int i) { return std::rand()%i;}
55+
4956
int main(int argc, char *argv[])
5057
{
58+
5159
const string keys =
5260
"{help | | print this message}"
5361
"{pos_path | pos | path to training object samples}"
@@ -57,8 +65,15 @@ int main(int argc, char *argv[])
5765
"{weak_count | 100 | number of weak classifiers in cascade}"
5866
"{model_size | 40x40 | model size in pixels}"
5967
"{model_filename | model.xml | filename for saving model}"
68+
"{features_type | icf | features type, \"icf\" or \"acf\"}"
69+
"{alpha | 0.02 | alpha value}"
70+
"{is_grayscale | false | read the image as grayscale}"
71+
"{use_fast_log | false | use fast log function}"
72+
"{limit_ps | -1 | limit to positive samples (-1 means all)}"
73+
"{limit_bg | -1 | limit to negative samples (-1 means all)}"
6074
;
6175

76+
6277
CommandLineParser parser(argc, argv, keys);
6378
parser.about("FCW trainer");
6479

@@ -76,15 +91,22 @@ int main(int argc, char *argv[])
7691
params.feature_count = parser.get<int>("feature_count");
7792
params.weak_count = parser.get<int>("weak_count");
7893
params.bg_per_image = parser.get<int>("bg_per_image");
79-
94+
params.features_type = parser.get<string>("features_type");
95+
params.alpha = parser.get<float>("alpha");
96+
params.is_grayscale = parser.get<bool>("is_grayscale");
97+
params.use_fast_log = parser.get<bool>("use_fast_log");
98+
99+
int limit_ps = parser.get<int>("limit_ps");
100+
int limit_bg = parser.get<int>("limit_bg");
101+
80102
string model_size = parser.get<string>("model_size");
81103
if( !read_model_size(model_size.c_str(), &params.model_n_rows,
82104
&params.model_n_cols) )
83105
{
84106
cerr << "Error reading model size from `" << model_size << "`" << endl;
85107
return 1;
86108
}
87-
109+
88110
if( params.feature_count <= 0 )
89111
{
90112
cerr << "feature_count must be positive number" << endl;
@@ -97,20 +119,67 @@ int main(int argc, char *argv[])
97119
return 1;
98120
}
99121

100-
if( params.bg_per_image <= 0 )
122+
if( params.features_type != "icf" && params.features_type != "acf" )
101123
{
102-
cerr << "bg_per_image must be positive number" << endl;
124+
cerr << "features_type must be \"icf\" or \"acf\"" << endl;
125+
return 1;
126+
}
127+
if( params.alpha <= 0 )
128+
{
129+
cerr << "alpha must be positive float number" << endl;
103130
return 1;
104131
}
105-
106132
if( !parser.check() )
107133
{
108134
parser.printErrors();
109135
return 1;
110136
}
137+
138+
std::vector<cv::String> pos_filenames;
139+
glob(pos_path, pos_filenames);
140+
141+
std::vector<cv::String> bg_filenames;
142+
glob(bg_path, bg_filenames);
143+
144+
if(limit_ps != -1 && (int)pos_filenames.size() > limit_ps)
145+
pos_filenames.erase(pos_filenames.begin()+limit_ps, pos_filenames.end());
146+
if(limit_bg != -1 && (int)bg_filenames.size() > limit_bg)
147+
bg_filenames.erase(bg_filenames.begin()+limit_bg, bg_filenames.end());
148+
149+
//random pick input images
150+
bool random_shuffle = false;
151+
if(random_shuffle)
152+
{
153+
std::srand ( unsigned ( std::time(0) ) );
154+
std::random_shuffle ( pos_filenames.begin(), pos_filenames.end(), randomPred );
155+
std::random_shuffle ( bg_filenames.begin(), bg_filenames.end(), randomPred );
156+
}
157+
158+
int samples_size = (int)((params.bg_per_image * bg_filenames.size()) + pos_filenames.size());
159+
int features_size = params.feature_count;
160+
int max_features_allowed = (int)(INT_MAX/(sizeof(int)* samples_size));
161+
int max_samples_allowed = (int)(INT_MAX/(sizeof(int)* features_size));
162+
int total_samples = (int)((params.bg_per_image * bg_filenames.size()) + pos_filenames.size());
163+
164+
165+
if(total_samples >max_samples_allowed)
166+
{
167+
CV_Error_(1, ("exceeded maximum number of samples. Maximum number of samples with %d features is %d, you have %d (%d positive samples + (%d bg * %d bg_per_image))\n",features_size,max_samples_allowed,total_samples,pos_filenames.size(),bg_filenames.size(),params.bg_per_image ));
168+
}
169+
170+
if(params.feature_count >max_features_allowed)
171+
{
172+
CV_Error_(1, ("exceeded maximum number of features. Maximum number of features with %d samples is %d, you have %d\n",samples_size,max_features_allowed, features_size ));
173+
}
174+
175+
std::cout<<pos_filenames.size()<<std::endl;
176+
std::cout<<bg_filenames.size()<<std::endl;
177+
178+
ICFDetector detector;
179+
180+
181+
detector.train(pos_filenames, bg_filenames, params);
111182

112-
ICFDetector detector;
113-
detector.train(pos_path, bg_path, params);
114183
FileStorage fs(model_filename, FileStorage::WRITE);
115184
fs << "icfdetector";
116185
detector.write(fs);

modules/text/src/erfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,7 @@ void erGroupingNM(InputArray _img, InputArrayOfArrays _src, vector< vector<ERSta
37513751
_src.getMatVector(src);
37523752

37533753
CV_Assert ( !src.empty() );
3754-
CV_Assert ( src.size() == regions.size() );
3754+
//CV_Assert ( src.size() == regions.size() );
37553755
size_t num_channels = src.size();
37563756

37573757
Mat img = _img.getMat();

modules/xobjdetect/doc/integral_channel_features.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,14 @@ Params for ICFDetector training.
165165
int model_n_rows;
166166
int model_n_cols;
167167
int bg_per_image;
168+
std::string features_type;
169+
float alpha;
170+
bool is_grayscale;
171+
bool use_fast_log;
168172

169173
ICFDetectorParams(): feature_count(UINT_MAX), weak_count(100),
170-
model_n_rows(56), model_n_cols(56), bg_per_image(5)
174+
model_n_rows(56), model_n_cols(56), bg_per_image(5),
175+
alpha(0.02), is_grayscale(false), use_fast_log(false)
171176
{}
172177
};
173178

@@ -181,7 +186,7 @@ ICFDetector::train
181186

182187
Train detector.
183188

184-
.. ocv:function:: void ICFDetector::train(const String& pos_path, const String& bg_path, ICFDetectorParams params = ICFDetectorParams())
189+
.. ocv:function:: void ICFDetector::train(const std::vector<String>& pos_filenames, const std::vector<String>& bg_filenames, ICFDetectorParams params = ICFDetectorParams())
185190
186191
:param pos_path: path to folder with images of objects (wildcards like ``/my/path/*.png`` are allowed)
187192
:param bg_path: path to folder with background images
@@ -192,13 +197,20 @@ ICFDetector::detect
192197

193198
Detect objects on image.
194199

195-
.. ocv:function:: void ICFDetector::detect(const Mat& image, vector<Rect>& objects, float scaleFactor, Size minSize, Size maxSize, float threshold)
200+
.. ocv:function:: void ICFDetector::detect(const Mat& image, vector<Rect>& objects, float scaleFactor, Size minSize, Size maxSize, float threshold, int slidingStep, std::vector<float>& values)
201+
202+
.. ocv:function:: detect(const Mat& img, std::vector<Rect>& objects, float minScaleFactor, float maxScaleFactor, float factorStep, float threshold, int slidingStep, std::vector<float>& values)
196203
197204
:param image: image for detection
198205
:param objects: output array of bounding boxes
199206
:param scaleFactor: scale between layers in detection pyramid
200207
:param minSize: min size of objects in pixels
201208
:param maxSize: max size of objects in pixels
209+
:param minScaleFactor: min factor by which the image will be resized
210+
:param maxScaleFactor: max factor by which the image will be resized
211+
:param factorStep: scaling factor is incremented each pyramid layer according to this parameter
212+
:param slidingStep: sliding window step
213+
:param values: output vector with values of positive samples
202214

203215
ICFDetector::write
204216
------------------

modules/xobjdetect/include/opencv2/xobjdetect.hpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ the use of this software, even if advised of the possibility of such damage.
4343
#define __OPENCV_XOBJDETECT_XOBJDETECT_HPP__
4444

4545
#include <opencv2/core.hpp>
46+
#include <opencv2/highgui.hpp>
4647
#include <vector>
4748
#include <string>
4849

@@ -102,6 +103,8 @@ std::vector<std::vector<int> >
102103
generateFeatures(Size window_size, const std::string& type,
103104
int count = INT_MAX, int channel_count = 10);
104105

106+
//sort in-place of columns of the input matrix
107+
void sort_columns_without_copy(Mat& m, Mat indices = Mat());
105108

106109
struct CV_EXPORTS WaldBoostParams
107110
{
@@ -127,8 +130,8 @@ class CV_EXPORTS WaldBoost : public Algorithm
127130
Returns feature indices chosen for cascade.
128131
Feature enumeration starts from 0
129132
*/
130-
virtual std::vector<int> train(const Mat& /*data*/,
131-
const Mat& /*labels*/) = 0;
133+
virtual std::vector<int> train(Mat& /*data*/,
134+
const Mat& /*labels*/, bool use_fast_log=false) = 0;
132135

133136
/* Predict object class given object that can compute object features
134137
@@ -157,28 +160,32 @@ struct CV_EXPORTS ICFDetectorParams
157160
int model_n_rows;
158161
int model_n_cols;
159162
int bg_per_image;
163+
std::string features_type;
164+
float alpha;
165+
bool is_grayscale;
166+
bool use_fast_log;
160167

161168
ICFDetectorParams(): feature_count(UINT_MAX), weak_count(100),
162-
model_n_rows(56), model_n_cols(56), bg_per_image(5)
169+
model_n_rows(56), model_n_cols(56), bg_per_image(5), alpha(0.02f), is_grayscale(false), use_fast_log(false)
163170
{}
164171
};
165172

166173
class CV_EXPORTS ICFDetector
167174
{
168175
public:
169176

170-
ICFDetector(): waldboost_(), features_() {}
177+
ICFDetector(): waldboost_(), features_(), ftype_() {}
171178

172179
/* Train detector
173180
174-
pos_pathpath to folder with images of objects
181+
pos_filenamespaths to objects images
175182
176-
bg_path — path to folder with background images
183+
bg_filenames — path backgrounds images
177184
178185
params — parameters for detector training
179186
*/
180-
void train(const String& pos_path,
181-
const String& bg_path,
187+
void train(const std::vector<String>& pos_filenames,
188+
const std::vector<String>& bg_filenames,
182189
ICFDetectorParams params = ICFDetectorParams());
183190

184191
/* Detect object on image
@@ -192,9 +199,35 @@ class CV_EXPORTS ICFDetector
192199
minSize — min size of objects in pixels
193200
194201
maxSize — max size of objects in pixels
202+
203+
slidingStep — sliding window step
204+
205+
values — output vector with values of positive samples
206+
195207
*/
208+
196209
void detect(const Mat& image, std::vector<Rect>& objects,
197-
float scaleFactor, Size minSize, Size maxSize, float threshold);
210+
float scaleFactor, Size minSize, Size maxSize, float threshold, int slidingStep, std::vector<float>& values);
211+
212+
/* Detect object on image
213+
214+
image — image for detection
215+
216+
object — output array of bounding boxes
217+
218+
minScaleFactor — min factor image will be resized
219+
220+
maxScaleFactor — max factor image will be resized
221+
222+
factorStep — scaling factor is incremented according to factorStep
223+
224+
slidingStep — sliding window step
225+
226+
values — output vector with values of positive samples
227+
228+
229+
*/
230+
void detect(const Mat& img, std::vector<Rect>& objects, float minScaleFactor, float maxScaleFactor, float factorStep, float threshold, int slidingStep, std::vector<float>& values);
198231

199232
/* Write detector to FileStorage */
200233
void write(FileStorage &fs) const;
@@ -207,6 +240,7 @@ class CV_EXPORTS ICFDetector
207240
std::vector<std::vector<int> > features_;
208241
int model_n_rows_;
209242
int model_n_cols_;
243+
std::string ftype_;
210244
};
211245

212246
CV_EXPORTS void write(FileStorage& fs, String&, const ICFDetector& detector);

0 commit comments

Comments
 (0)