-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathObjectMarker.cpp
More file actions
447 lines (388 loc) · 14.7 KB
/
ObjectMarker.cpp
File metadata and controls
447 lines (388 loc) · 14.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*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, 2006 Gunawan Herman, 2003 Florian Adolf.
// 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 "ObjectMarker.h"
#include "util_functions.h"
#include "util_cv_functions.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <boost/filesystem/path.hpp>
#include <iostream>
using namespace std;
ObjectMarker::ObjectMarker(){
}
ObjectMarker::~ObjectMarker(void){
}
bool ObjectMarker::saveConfiguration(const std::string& config_name,
const std::string& input_dir, const std::string& outputname,
const MarkerViewer& marker_viewer)
{
cv::FileStorage fs(config_name, cv::FileStorage::WRITE);
if (!fs.isOpened())
return false;
fs << "image_folder" << input_dir;
fs << "output_file" << outputname;
marker_viewer.Write(fs, "Viewer");
return true;
}
bool ObjectMarker::loadConfiguration(const std::string& config_name,
std::string& input_dir, std::string& outputname,
MarkerViewer& marker_viewer)
{
cv::FileStorage fs(config_name, cv::FileStorage::READ);
if (!fs.isOpened())
return false;
fs["image_folder"] >> input_dir;
fs["output_file"] >> outputname;
marker_viewer.Read(fs["Viewer"]);
return true;
}
void ObjectMarker::printHelp() {
printf("\nObject Marker: \n\t 画像中のオブジェクト位置を記録するためのツール\n");
printf("\tGunawan Herman, April 2006\n");
printf("\tAdapted from ObjectMarker.cpp by A.Florian\n");
printf("\tModified and localized by Takuya MINAGAWA in 2014\n");
printf("\n");
printf("------------------------------------------------------------\n");
printf("| btn | function |\n");
printf("|-------|--------------------------------------------------|\n");
printf("|Enter | 現在のマーカーをファイルに記述し、 |\n");
printf("| | 次の画像を読み込む |\n");
printf("|Space | Enterと同じ |\n");
printf("|BS | ひとつ前の画像に戻る |\n");
printf("|ESC | プログラムを終了 |\n");
printf("|d | 一番新しいマーカーを消去 |\n");
printf("|r | 前フレームのマーカーを1つ呼び出す |\n");
printf("|8 | 一番新しいマーカーを1 px上へ動かす |\n");
printf("|9 | 一番新しいマーカーを10px上へ動かす |\n");
printf("|2 | 一番新しいマーカーを1 px下へ動かす |\n");
printf("|3 | 一番新しいマーカーを10px下へ動かす |\n");
printf("|4 | 一番新しいマーカーを1 px左へ動かす |\n");
printf("|5 | 一番新しいマーカーを10px左へ動かす |\n");
printf("|6 | 一番新しいマーカーを1 px右へ動かす |\n");
printf("|7 | 一番新しいマーカーを10px右へ動かす |\n");
printf("|w | 一番新しいマーカーの幅を1 px拡大する |\n");
printf("|W | 一番新しいマーカーの幅を1 px縮小する |\n");
printf("|h | 一番新しいマーカーの高さを1 px拡大する |\n");
printf("|H | 一番新しいマーカーの高さを1 px縮小する |\n");
printf("|z | 一番新しいマーカーのを大きさを2%%拡大する |\n");
printf("|Z | 一番新しいマーカーのを大きさを2%%縮小する |\n");
printf("|m | マーカーの縦横比を固定/固定解除する |\n");
printf("|a | マーカーの縦横比を指定する |\n");
printf("|s | 画像の表示サイズを変更する |\n");
printf("|c | マーカー領域の画像を切り取ってファイル保存 |\n");
printf("|p | マーカーを矩形だけでなく点を許可する |\n");
printf("|e | 現フレームにつけたマーカー座標を画面出力 |\n");
printf("|g | ガイドの表示/非表示 |\n");
printf("|G | ガイドの設定 |\n");
printf("|f | 画像のフォルダ名を変更 |\n");
printf("|o | 出力ファイルを変更 |\n");
printf("|O | 出力ファイルを成形して新たに作成 |\n");
printf("|j | 指定番号の画像へジャンプ |\n");
printf("|t | このヘルプを表示 |\n");
printf("------------------------------------------------------------\n");
printf("オブジェクトを右クリックで選択\n");
printf("\n");
}
void ObjectMarker::printStatus() const
{
std::cout << "このプログラムは入力画像を'" << _input_dir << "'フォルダの中から探します\n";
std::cout << "オブジェクトの位置はファイル'" << _annotation_file << "'にテキスト出力されます\n";
_marker_viewer.PrintStatus();
}
//! アノテーションの並び替え
/*!
ロードされたアノテーションに対応する画像ファイル名が、参照画像ファイル名と対応するように並び替え
\param[in] loaded_img_list 入力画像リスト
\param[in] loaded_annotation 読み込まれたアノテーション
\param[in] ref_img_list 参照アノテーション
\return 整理されたアノテーション
*/
std::vector<std::vector<cv::Rect>> ObjectMarker::reorderAnnotation(
const std::vector<std::string>& loaded_img_list,
const std::vector<std::vector<cv::Rect>>& loaded_annotation,
const std::vector<std::string>& ref_img_list)
{
assert(loaded_img_list.size() == loaded_annotation.size());
using namespace boost::filesystem;
std::vector<std::vector<cv::Rect>> annotation(ref_img_list.size());
int num_loaded = loaded_img_list.size();
int num_ref = ref_img_list.size();
int i,j;
for (i = 0; i < num_loaded; i++){
path loaded_img_path = path(loaded_img_list[i]);
for (j = 0; j < num_ref; j++){
if (loaded_img_path.compare(ref_img_list[j]) == 0)
break;
}
if (j < num_ref)
annotation[j] = loaded_annotation[i];
}
return annotation;
}
bool ObjectMarker::Load(const std::string& image_dir, const std::string& anno_file)
{
// フォルダから画像一覧を取得
std::string input_dir = image_dir;
_file_list.clear();
while (!util::ReadImageFilesInDirectory(input_dir, _file_list)){
std::cout << "no appropriate input files in directory " << input_dir << std::endl;
std::string flag = util::AskQuestionGetString("Quit?(1:Yes, 0:No): ");
if (flag == "1")
return false;
input_dir = util::AskQuestionGetString("New Image Directory Name: ");
}
_image_idx = 0;
_input_dir = input_dir;
return LoadAnnotationFile(anno_file);
}
bool ObjectMarker::LoadAnnotationFile(const std::string& anno_file)
{
// アノテーションファイルを読み込み
std::vector<std::string> anno_file_list;
std::vector<std::vector<cv::Rect>> anno_rect_list;
util::LoadAnnotationFile(anno_file, anno_file_list, anno_rect_list);
// フォルダの画像一覧とアノテーションファイルを紐づけ
_rectlist = reorderAnnotation(anno_file_list, anno_rect_list, _file_list);
anno_file_list.clear();
anno_rect_list.clear();
_annotation_file = anno_file;
// ヘッダの書き込み
return !util::AddHeaderLine(anno_file);
}
bool ObjectMarker::jump(int idx)
{
if (_marker_viewer.is_changed()){
_rectlist[_image_idx] = _marker_viewer.GetMarkers();
util::AddAnnotationLine(_annotation_file, _file_list[_image_idx], _rectlist[_image_idx]);
_marker_viewer.reset_change();
}
if (idx < 0 || idx >= _file_list.size())
return false;
std::string load_img_file = _file_list[idx];
cv::Mat img = cv::imread(load_img_file);
if (img.empty()){
std::cerr << "Fail to read file " << load_img_file << "." << std::endl;
return false;
}
// マーカーをセット
_marker_viewer.SetMarkers(_rectlist[idx]);
_marker_viewer.reset_change();
std::ostringstream oss;
oss << idx + 1 << " - " << load_img_file;
_marker_viewer.Open(img, oss.str());
_image_idx = idx;
return true;
}
//! アノテーションファイルを整形して出力
inline bool ObjectMarker::ExportAnnotationFile(const std::string& filename){
return util::SaveAnnotationFile(filename, _file_list, _rectlist);
};
void ObjectMarker::CopyFormerMarkers()
{
if (_image_idx > 0){
if (!_rectlist[_image_idx].empty() || !_rectlist[_image_idx - 1].empty()){
_marker_viewer.SetMarkers(_rectlist[_image_idx - 1]);
}
}
}
//! アノテーションで画像を切り取って保存
inline void ObjectMarker::CropAndSaveImages(const std::string& dir_name){
util::CropAnnotatedImageRegions(dir_name, _file_list, _rectlist);
}
int ObjectMarker::run(const std::string& conf_file)
{
///////// パラメータ ///////////
std::string annotation_file; // 出力テキストファイル名
std::string input_dir = "rawdata"; // 入力フォルダ
///////////////////////////////////
bool ret = loadConfiguration(conf_file, input_dir, annotation_file, _marker_viewer);
if (annotation_file.empty())
annotation_file = "annotation.txt";
Load(input_dir, annotation_file);
printHelp();
printStatus();
enum KeyBindings {
Key_Enter = 13, Key_ESC = 27, Key_Space = 32, Key_BS = 8
};
bool loop = this->begin();
while(loop){
// Get user input
int iKey = _marker_viewer.GetWindowKey();
// Press ESC to close this program, any unsaved changes will be discarded
if (iKey == Key_ESC){
loop = false;
}
// Press Space or Enter to save marked objects on current image and proceed to the next image
else if (iKey == Key_Space || iKey == Key_Enter){
loop = this->next();
}
else if (iKey == Key_BS){
if(!this->prev())
std::cout << "Can't back to the former image." << std::endl;
}
// Press d to remove the last added object
else if (iKey == 'd'){
_marker_viewer.DeleteMarker();
}
else if (iKey == '8'){
_marker_viewer.ReshapeMarker(cv::Rect(0,-1,0,0));
}
else if(iKey == '9'){
_marker_viewer.ReshapeMarker(cv::Rect(0, -10, 0, 0));
}
else if (iKey == '2'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 1, 0, 0));
}
else if(iKey == '3'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 10, 0, 0));
}
else if (iKey == '4'){
_marker_viewer.ReshapeMarker(cv::Rect(-1, 0, 0, 0));
}
else if (iKey == '5'){
_marker_viewer.ReshapeMarker(cv::Rect(-10, 0, 0, 0));
}
else if (iKey == '6'){
_marker_viewer.ReshapeMarker(cv::Rect(1, 0, 0, 0));
}
else if (iKey == '7'){
_marker_viewer.ReshapeMarker(cv::Rect(10, 0, 0, 0));
}
else if (iKey == 'w'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 0, 1, 0));
}
else if (iKey == 'W'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 0, -1, 0));
}
else if (iKey == 'h'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 0, 0, 1));
}
else if (iKey == 'H'){
_marker_viewer.ReshapeMarker(cv::Rect(0, 0, 0, -1));
}
else if (iKey == 'z'){
_marker_viewer.ResizeMarker(1.02);
}
else if (iKey == 'Z'){
_marker_viewer.ResizeMarker(0.98);
}
else if (iKey == 'c'){
std::cout << "Cropping images with annotated rectangles." << std::endl;
std::string dir_name = util::AskQuestionGetString("Folder name to save images: ");
CropAndSaveImages(dir_name);
}
else if (iKey == 'm'){
_marker_viewer.SwitchFixAR();
printStatus();
}
else if (iKey == 'a'){
double ratio = util::AskQuestionGetDouble("Aspect ratio of object (Width / Height): ");
if (ratio <= 0){
std::cerr << "Illegal aspect ratio: " << ratio;
}
else{
_marker_viewer.SetAspectRatio(ratio);
}
}
else if (iKey == 's'){
double scale = util::AskQuestionGetDouble("Display Image Scale: ");
_marker_viewer.SetDisplayScale(scale);
this->reload();
}
else if (iKey == 'e'){
std::vector<cv::Rect> objects = _marker_viewer.GetMarkers();
for (int i = 0; i < objects.size(); i++) {
cv::Rect rect = objects[i];
std::cout << rect.x << " " << rect.y << " " << rect.width << " " << rect.height << std::endl;
}
}
else if (iKey == 'p'){
std::cout << "点形状の許可: " << (_marker_viewer.SwitchAcceptPointShape() ? "YES" : "NO") << std::endl;
}
else if (iKey == 't'){
printHelp();
printStatus();
}
else if (iKey == 'j'){
int target_id = util::AskQuestionGetInt("Jump to image#: ");
if (!jump(target_id - 1)){
std::cerr << "Fail to jump #" << target_id << std::endl;
}
}
else if (iKey == 'r'){
this->CopyFormerMarkers();
}
else if (iKey == 'g'){
std::cout << "GUIDE: " << (_marker_viewer.SwitchShowGuide() ? "ON" : "OFF") << std::endl;
}
else if (iKey == 'G'){
int shape_id = util::AskQuestionGetInt("Choose shape (0:NONE, 1:SQUARE, 2:RECTANGLE, 3:CIRCLE, 4:ELLIPSE): ");
if (shape_id >= 0 && shape_id < 5){
_marker_viewer.SetGuideShape(shape_id);
if (shape_id > 0){
cv::Rect grect;
grect.x = util::AskQuestionGetInt("input x: ");
grect.y = util::AskQuestionGetInt("input y: ");
grect.width = util::AskQuestionGetInt("input width: ");
grect.height = util::AskQuestionGetInt("input height: ");
_marker_viewer.SetGuideRectangle(grect);
}
_marker_viewer.ShowGuide();
}
else{
std::cout << "Wrong Input" << std::endl;
}
}
// change work folder (= reboot)
else if (iKey == 'f'){
input_dir = util::AskQuestionGetString("New Image Directory Name: ");
Load(input_dir, _annotation_file);
this->begin();
}
else if (iKey == 'o'){
std::string outputname = util::AskQuestionGetString("New Output File Name: ");
LoadAnnotationFile(outputname);
this->reload();
}
else if (iKey == 'O'){
std::string save_file = util::AskQuestionGetString("Export Annotation File Name: ");
if (!ExportAnnotationFile(save_file))
std::cerr << "Fail to export annotation to file " << save_file << "." << std::endl;
}
else{
std::cout << "Unrecognised command" << std::endl;
}
};
saveConfiguration(conf_file, _input_dir, _annotation_file, _marker_viewer);
return 0;
}