-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathUtil.cpp
250 lines (221 loc) · 7.79 KB
/
Util.cpp
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
//
// Copyright (C) 2014 Takuya MINAGAWA.
// Third party copyrights are property of their respective owners.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//M*/
#include "Util.h"
#include <fstream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
namespace util{
//! はみ出る領域をカット
cv::Rect TruncateRect(const cv::Rect& obj_rect, const cv::Size& img_size)
{
cv::Rect resize_rect = obj_rect;
if (obj_rect.x < 0){
resize_rect.x = 0;
resize_rect.width += obj_rect.x;
}
if (obj_rect.y < 0){
resize_rect.y = 0;
resize_rect.height += obj_rect.y;
}
if (resize_rect.x + resize_rect.width > img_size.width){
resize_rect.width = img_size.width - resize_rect.x;
}
if (resize_rect.y + resize_rect.height > img_size.height){
resize_rect.height = img_size.height - resize_rect.y;
}
return resize_rect;
}
//! 中心を動かさずに、はみ出る領域をカット
cv::Rect TruncateRectKeepCenter(const cv::Rect& obj_rect, const cv::Size& max_size)
{
cv::Rect exp_rect = obj_rect;
if (exp_rect.x < 0){
exp_rect.width += 2 * exp_rect.x;
exp_rect.x = 0;
}
if (exp_rect.y < 0){
exp_rect.height += 2 * exp_rect.y;
exp_rect.y = 0;
}
if (exp_rect.x + exp_rect.width > max_size.width){
exp_rect.x += (exp_rect.x + exp_rect.width - max_size.width) / 2;
exp_rect.width = max_size.width - exp_rect.x;
}
if (exp_rect.y + exp_rect.height > max_size.height){
exp_rect.y += (exp_rect.y + exp_rect.height - max_size.height) / 2;
exp_rect.height = max_size.height - exp_rect.y;
}
return exp_rect;
}
//! アノテーションファイルの読み込み
/*!
opencv_createsamles.exeと同形式のアノテーションファイル読み書き
ReadCsvFile()関数必須
\param[in] gt_file アノテーションファイル名
\param[out] imgpathlist 画像ファイルへのパス
\param[out] rectlist 各画像につけられたアノテーションのリスト
\return 読み込みの成否
*/
bool LoadAnnotationFile(const std::string& gt_file, std::vector<std::string>& imgpathlist, std::vector<std::vector<cv::Rect>>& rectlist)
{
std::vector<std::vector<std::string>> tokenized_strings;
std::vector<std::string> sep;
sep.push_back(" ");
if (!ReadCSVFile(gt_file, tokenized_strings, sep))
return false;
std::vector<std::vector<std::string>>::iterator it, it_end = tokenized_strings.end();
for (it = tokenized_strings.begin(); it != it_end; it++){
int num_str = it->size();
if (num_str < 2)
continue;
std::string filename = (*it)[0];
if (filename.empty() || filename.find("#") != std::string::npos){
continue;
}
imgpathlist.push_back(filename);
int obj_num = atoi((*it)[1].c_str());
std::vector<cv::Rect> rects;
for (int i = 0; i<obj_num && 4 * i + 6 <= num_str; i++){
int j = 4 * i + 2;
cv::Rect obj_rect;
obj_rect.x = atoi((*it)[j].c_str());
obj_rect.y = atoi((*it)[j + 1].c_str());
obj_rect.width = atoi((*it)[j + 2].c_str());
obj_rect.height = atoi((*it)[j + 3].c_str());
rects.push_back(obj_rect);
}
rectlist.push_back(rects);
}
return true;
}
//! アノテーションファイルへ追記
/*!
opencv_createsamles.exeと同形式のアノテーションファイル読み書き
\param[in] anno_file アノテーションファイル名
\param[in] img_file 画像ファイルへのパス
\param[int] obj_rects 各画像につけられたアノテーションのリスト
\return 保存の成否
*/
bool AddAnnotationLine(const std::string& anno_file, const std::string& img_file, const std::vector<cv::Rect>& obj_rects, const std::string& sep)
{
// 出力ファイルを開く
std::ofstream ofs(anno_file, std::ios::app);
if (!ofs.is_open()){
return false;
}
ofs << img_file << sep << obj_rects.size();
for (int i = 0; i < obj_rects.size(); i++){
cv::Rect rect = obj_rects[i];
ofs << sep << rect.x << sep << rect.y << sep << rect.width << sep << rect.height;
}
ofs << std::endl;
return true;
}
// ディレクトリから画像ファイル名一覧を取得
bool ReadImageFilesInDirectory(const std::string& img_dir, std::vector<std::string>& image_lists)
{
using namespace boost::filesystem;
path img_dir_path(img_dir);
if (!is_directory(img_dir_path)){
return false;
}
directory_iterator end;
for (directory_iterator p(img_dir_path); p != end; ++p){
std::string file_name = p->path().generic_string();
if (hasImageExtention(file_name)){
image_lists.push_back(file_name);
}
}
return true;
}
bool hasImageExtention(const std::string& filename){
std::string ext = boost::filesystem::path(filename).extension().string();
return (ext == ".jpg" || ext == ".JPG" || ext == ".jpeg" || ext == ".JPEG" ||
ext == ".bmp" || ext == ".BMP" || ext == ".png" || ext == ".PNG" ||
ext == ".dib" || ext == ".DIB" || ext == ".pbm" || ext == ".PBM" ||
ext == ".pgm" || ext == ".PGM" || ext == ".ppm" || ext == ".PPM" ||
ext == ".sr" || ext == ".SR" || ext == ".ras" || ext == ".RAS");
}
bool ReadCSVFile(const std::string& input_file, std::vector<std::vector<std::string>>& output_strings,
const std::vector<std::string>& separater_vec)
{
std::vector<std::string> sep_vec;
if (separater_vec.empty()){
sep_vec.push_back(",");
}
else{
sep_vec = separater_vec;
}
std::ifstream ifs(input_file);
if (!ifs.is_open())
return false;
output_strings.clear();
std::string buf;
while (ifs && std::getline(ifs, buf)){
std::vector<std::string> str_list = TokenizeString(buf, sep_vec);
output_strings.push_back(str_list);
}
return true;
}
std::vector<std::string> TokenizeString(const std::string& input_string, const std::vector<std::string>& separater_vec)
{
std::vector<std::string>::const_iterator separater_itr;
std::vector<std::string::size_type> index_vec;
std::string::size_type index;
for (separater_itr = separater_vec.begin(); separater_itr != separater_vec.end(); separater_itr++){
index = 0;
while (true){
index = input_string.find(*separater_itr, index);
if (index == std::string::npos){
break;
}
else{
index_vec.push_back(index);
index++;
}
}
}
sort(index_vec.begin(), index_vec.end());
std::vector<std::string> ret_substr_vec;
std::vector<std::string::size_type>::iterator idx_itr;
std::string::size_type start_idx = 0;
int str_size;
for (idx_itr = index_vec.begin(); idx_itr != index_vec.end(); idx_itr++){
str_size = *idx_itr - start_idx;
ret_substr_vec.push_back(input_string.substr(start_idx, str_size));
start_idx = *idx_itr + 1;
}
ret_substr_vec.push_back(input_string.substr(start_idx));
return ret_substr_vec;
}
}