1
/* Copyright (C) 2014 Moritz Becker
3
This file is part of Charon.
5
Charon is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
10
Charon is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public License
16
along with Charon. If not, see <http://www.gnu.org/licenses/>.
18
/** \file DrawAnnotations.hxx
19
* Implementation of the parameter class DrawAnnotations.
20
* \author <a href="mailto:moritz.becker@iwr.uni-heidelberg.de">
25
#ifndef _DRAWANNOTATIONS_HXX_
26
#define _DRAWANNOTATIONS_HXX_
28
#include <charon-core/ParameteredObject.hxx>
29
#include "DrawAnnotations.h"
32
DrawAnnotations<T>::DrawAnnotations(const std::string& name) :
33
TemplatedParameteredObject<T>(
34
"DrawAnnotations", name,
35
"<h2>Draw boxes on given images</h2><br>"
36
"Draw boxes on given images"
39
ParameteredObject::_addInputSlot(
40
annotations, "annotations",
41
"Annotation list object",
43
ParameteredObject::_addInputSlot(
44
filepaths, "filepaths",
48
ParameteredObject::_addOutputSlot(
53
ParameteredObject::_addParameter(
55
"JSON data contains boxes", true);
57
ParameteredObject::_addParameter(
58
closeLoop, "closeLoop",
59
"draws a closed loop of list of points", true);
61
ParameteredObject::_addParameter(
62
processAll, "processAll",
63
"Draw all frames of annotation data no matter if those frames contain annotations or not."
64
"Otherwise, only images with annotations are saved.", false);
66
ParameteredObject::_addParameter(
67
resultsFinal, "resultsFinal",
68
"draw final results", true);
70
ParameteredObject::_addParameter(
71
resultsAdd, "resultsAdd",
72
"draw add results", true);
74
ParameteredObject::_addParameter(
75
resultsDone, "resultsDone",
76
"draw done results", true);
78
ParameteredObject::_addParameter(
79
resultsFailed, "resultsFailed",
80
"draw failed results", true);
82
ParameteredObject::_addParameter(
83
resultsEdit, "resultsEdit",
84
"draw edit results", true);
86
ParameteredObject::_addParameter(
87
colorR, "colorR", "Color: red (0-255)", 255u);
88
ParameteredObject::_addParameter(
89
colorG, "colorG", "Color: green (0-255)", 255u);
90
ParameteredObject::_addParameter(
91
colorB, "colorB", "Color: blue (0-255)", 0u);
93
ParameteredObject::_addParameter(
94
colorRWrong, "colorRWrong", "Wrong Color: red (0-255)", 255u);
95
ParameteredObject::_addParameter(
96
colorGWrong, "colorGWrong", "Wrong Color: green (0-255)", 0u);
97
ParameteredObject::_addParameter(
98
colorBWrong, "colorBWrong", "Wrong Color: blue (0-255)", 0u);
100
ParameteredObject::_addParameter(
101
colorRFinal, "colorRFinal", "Final Color: red (0-255)", 0u);
102
ParameteredObject::_addParameter(
103
colorGFinal, "colorGFinal", "Final Color: green (0-255)", 255u);
104
ParameteredObject::_addParameter(
105
colorBFinal, "colorBFinal", "Final Color: blue (0-255)", 0u);
107
ParameteredObject::_addParameter(
108
colorRFinalWrong, "colorREdit", "Final Wrong Color: red (0-255)", 0u);
109
ParameteredObject::_addParameter(
110
colorGFinalWrong, "colorGEdit", "Final Wrong Color: green (0-255)", 0u);
111
ParameteredObject::_addParameter(
112
colorBFinalWrong, "colorBEdit", "Final Wrong Color: blue (0-255)", 255u);
115
std::string getFilepath( std::string filename, const std::vector<std::string> &list ){
117
std::string path = "";
118
std::string filename2 = "";
119
for( auto it = list.begin(); it != list.end(); it++ ){
120
filename2 = (*it).substr((*it).find_last_of(FileTool::slash)+1,(*it).length());
121
if( filename2 == filename )
125
//sout << "filename2 = " << filename2 << std::endl;
131
template <typename T>
132
void DrawAnnotations<T>::execute() {
134
assert (colorR() < 256u);
135
assert (colorG() < 256u);
136
assert (colorB() < 256u);
139
unsigned char color[] = {
140
(unsigned char) this->colorR(),
141
(unsigned char) this->colorG(),
142
(unsigned char) this->colorB()
146
unsigned char color2[] = {
147
(unsigned char) this->colorRWrong(),
148
(unsigned char) this->colorGWrong(),
149
(unsigned char) this->colorBWrong()
153
unsigned char color3[] = {
154
(unsigned char) this->colorRFinal(),
155
(unsigned char) this->colorGFinal(),
156
(unsigned char) this->colorBFinal()
160
unsigned char color4[] = {
161
(unsigned char) this->colorRFinalWrong(),
162
(unsigned char) this->colorGFinalWrong(),
163
(unsigned char) this->colorBFinalWrong()
166
const annotation::AnnotationList &in = this->annotations();
168
const std::vector< const annotation::Frame* > frames = in.getFrames();
172
// iterate over list of all available frames
173
auto it = frames.begin();
174
for (; it != frames.end(); it++ ){
176
// get filename saved in annoation list
177
std::string filename = (*it)->filename;
179
// find corresponding filepath from filelist
180
std::string filepath = getFilepath(filename, this->filepaths());
182
if( filepath == "" ){
183
sout << "Could not find image: " << filename << std::endl;
187
cimg_library::CImg<T> img( filepath.c_str() );
188
img.resize(img.width(),img.height(),img.depth(),3);
189
//cimg_library::CImg<T> img(2000,1000,1,3,0);
191
const int w = img.width();
192
const int h = img.height();
198
// iterate over list of annotations
199
auto it2 = (*it)->annotationIds.begin();
200
const auto it2End = (*it)->annotationIds.end();
201
for (; it2 != it2End; it2++){
203
const annotation::AnnotationPoints* box = in.getAnnotationPoints(*it2);
205
unsigned char *col = &color[0];
207
if (box->taskResultState == "Done" && this->resultsDone())
209
else if (box->taskResultState == "Edit" && this->resultsEdit())
211
else if (box->taskResultState == "Add" && this->resultsAdd())
213
else if (box->taskResultState == "Failed" && this->resultsFailed())
215
else if (box->taskResultState == "Final" && this->resultsFinal()){
223
if (box->points.size() == 0){
227
else if (box->points.size() == 1){
228
int x = std::max(0, std::min(w - 1, (int)box->points[0].first));
229
int y = std::max(0, std::min(h - 1, (int)box->points[0].second));
230
int x1 = std::max(0, std::min(w - 1, x - 1));
231
int y1 = std::max(0, std::min(h - 1, y - 1));
232
int x2 = std::max(0, std::min(w - 1, x + 1));
233
int y2 = std::max(0, std::min(h - 1, y + 1));
234
img.draw_rectangle(x1, y1, x2, y2, col);
236
// draw line between list of points
237
else if(this->isBox() && box->points.size() == 2){
238
int x1 = std::max(0, std::min(w - 1, (int)box->points[0].first));
239
int y1 = std::max(0, std::min(h - 1, (int)box->points[0].second));
240
int x2 = std::max(0, std::min(w - 1, (int)box->points[1].first));
241
int y2 = std::max(0, std::min(h - 1, (int)box->points[1].second));
242
img.draw_line(x1, y1, x2, y1, col);
243
img.draw_line(x2, y1, x2, y2, col);
244
img.draw_line(x2, y2, x1, y2, col);
245
img.draw_line(x1, y2, x1, y1, col);
247
// draw line between list of points
249
for (int k = 0; k < box->points.size() - 1; k++){
250
int x1 = std::max(0, std::min(w - 1, (int)box->points[k].first));
251
int y1 = std::max(0, std::min(h - 1, (int)box->points[k].second));
252
int x2 = std::max(0, std::min(w - 1, (int)box->points[k + 1].first));
253
int y2 = std::max(0, std::min(h - 1, (int)box->points[k + 1].second));
254
img.draw_line(x1, y1, x2, y2, col);
257
if (this->closeLoop()){
258
int x1 = std::max(0, std::min(w - 1, (int)box->points[0].first));
259
int y1 = std::max(0, std::min(h - 1, (int)box->points[0].second));
260
int x2 = std::max(0, std::min(w - 1, (int)box->points[box->points.size() - 1].first));
261
int y2 = std::max(0, std::min(h - 1, (int)box->points[box->points.size() - 1].second));
262
img.draw_line(x1, y1, x2, y2, col);
266
nBoxes++; // count paint points
271
sout << "!! No task result given for " << noTaskState << " annotations !!" << std::endl;
273
// Only save images if at least one box was painted to it
274
if (nBoxes > 0 || this->processAll() ){
275
sout << "\tProcessed '" << filepath << "'" << std::endl;
276
this->images().push_back( img );
283
sout << "Painted " << nBoxesAll << " point lists. Thank you and see you again soon!" << std::endl;
286
//const annotation::AnnotationList &list = this->annotations();
288
//const annotation::Frame* frame = list.getFrame("wer");
290
//sout << frame->frameId << std::endl;
292
//sout << list.getFrame(0)->frameId << std::endl;
294
//for( auto it = list.begin(); it != list.end(); it++ ){
295
// sout << it->frameId << ": " << it->annotations.size() << std::endl;
298
//for( auto it = box.begin(); it != box.end(); it++ ){
299
// sout << it->annotationId << ": " << it->frame->filename << std::endl;
304
#endif /* _DRAWANNOTATIONS_HXX_ */