~charon-developers/charon-utils/trunk

« back to all changes in this revision

Viewing changes to include/charon-utils/DrawAnnotations.hxx

  • Committer: Moritz Becker
  • Date: 2014-06-24 13:33:36 UTC
  • Revision ID: moritz.becker@pallas-ludens.com-20140624133336-ajyzs2jqb137elwr
Modified annotation class which now contains a list of points and added annotation writer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  Copyright (C) 2014 Moritz Becker
 
2
 
 
3
        This file is part of Charon.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
*/
 
18
/** \file DrawAnnotations.hxx
 
19
 *  Implementation of the parameter class DrawAnnotations.
 
20
 *  \author <a href="mailto:moritz.becker@iwr.uni-heidelberg.de">
 
21
 *      Moritz Becker</a>
 
22
 *  \date 19.05.2014
 
23
 */
 
24
 
 
25
#ifndef _DRAWANNOTATIONS_HXX_
 
26
#define _DRAWANNOTATIONS_HXX_
 
27
 
 
28
#include <charon-core/ParameteredObject.hxx>
 
29
#include "DrawAnnotations.h"
 
30
 
 
31
template <typename T>
 
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"
 
37
                )
 
38
{
 
39
        ParameteredObject::_addInputSlot(
 
40
                annotations, "annotations",
 
41
                "Annotation list object",
 
42
                "AnnotationList");
 
43
        ParameteredObject::_addInputSlot(
 
44
                filepaths, "filepaths",
 
45
                "filepaths",
 
46
                "vector<string>");
 
47
 
 
48
        ParameteredObject::_addOutputSlot(
 
49
                images, "images",
 
50
                "images",
 
51
                "CImgList<T>");
 
52
 
 
53
        ParameteredObject::_addParameter(
 
54
                isBox, "isBox",
 
55
                "JSON data contains boxes", true);
 
56
 
 
57
        ParameteredObject::_addParameter(
 
58
                closeLoop, "closeLoop",
 
59
                "draws a closed loop of list of points", true);
 
60
 
 
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);
 
65
 
 
66
        ParameteredObject::_addParameter(
 
67
                resultsFinal, "resultsFinal",
 
68
                "draw final results", true);
 
69
 
 
70
        ParameteredObject::_addParameter(
 
71
                resultsAdd, "resultsAdd",
 
72
                "draw add results", true);
 
73
 
 
74
        ParameteredObject::_addParameter(
 
75
                resultsDone, "resultsDone",
 
76
                "draw done results", true);
 
77
 
 
78
        ParameteredObject::_addParameter(
 
79
                resultsFailed, "resultsFailed",
 
80
                "draw failed results", true);
 
81
 
 
82
        ParameteredObject::_addParameter(
 
83
                resultsEdit, "resultsEdit",
 
84
                "draw edit results", true);
 
85
 
 
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);
 
92
 
 
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);
 
99
 
 
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);
 
106
 
 
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);
 
113
}
 
114
 
 
115
std::string getFilepath( std::string filename, const std::vector<std::string> &list ){
 
116
 
 
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 )
 
122
                        path = (*it);
 
123
        }
 
124
 
 
125
        //sout << "filename2 = " << filename2 << std::endl;
 
126
 
 
127
        return path;
 
128
 
 
129
}
 
130
 
 
131
template <typename T>
 
132
void DrawAnnotations<T>::execute() {
 
133
        // check uint8 range
 
134
        assert (colorR() < 256u);
 
135
        assert (colorG() < 256u);
 
136
        assert (colorB() < 256u);
 
137
 
 
138
        // paint color
 
139
        unsigned char color[] = {
 
140
                (unsigned char) this->colorR(),
 
141
                (unsigned char) this->colorG(),
 
142
                (unsigned char) this->colorB()
 
143
        };
 
144
 
 
145
        // paint wrong color
 
146
        unsigned char color2[] = {
 
147
                (unsigned char) this->colorRWrong(),
 
148
                (unsigned char) this->colorGWrong(),
 
149
                (unsigned char) this->colorBWrong()
 
150
        };
 
151
 
 
152
        // paint final color
 
153
        unsigned char color3[] = {
 
154
                (unsigned char) this->colorRFinal(),
 
155
                (unsigned char) this->colorGFinal(),
 
156
                (unsigned char) this->colorBFinal()
 
157
        };
 
158
 
 
159
        // paint final color
 
160
        unsigned char color4[] = {
 
161
                (unsigned char) this->colorRFinalWrong(),
 
162
                (unsigned char) this->colorGFinalWrong(),
 
163
                (unsigned char) this->colorBFinalWrong()
 
164
        };
 
165
 
 
166
        const annotation::AnnotationList &in = this->annotations();
 
167
 
 
168
        const std::vector< const annotation::Frame* > frames = in.getFrames();
 
169
 
 
170
        int nBoxesAll = 0;
 
171
 
 
172
        // iterate over list of all available frames
 
173
        auto it = frames.begin();
 
174
        for (; it != frames.end(); it++ ){
 
175
 
 
176
                // get filename saved in annoation list
 
177
                std::string filename = (*it)->filename;
 
178
 
 
179
                // find corresponding filepath from filelist
 
180
                std::string filepath = getFilepath(filename, this->filepaths());
 
181
 
 
182
                if( filepath == "" ){
 
183
                        sout << "Could not find image: " << filename << std::endl;
 
184
                        continue;
 
185
                }
 
186
                
 
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);
 
190
 
 
191
                const int w = img.width();
 
192
                const int h = img.height();
 
193
 
 
194
                int nBoxes = 0;
 
195
 
 
196
                int noTaskState = 0;
 
197
                
 
198
                // iterate over list of annotations
 
199
                auto it2 = (*it)->annotationIds.begin();
 
200
                const auto it2End = (*it)->annotationIds.end();
 
201
                for (; it2 != it2End; it2++){
 
202
 
 
203
                        const annotation::AnnotationPoints* box = in.getAnnotationPoints(*it2);
 
204
 
 
205
                        unsigned char *col = &color[0];
 
206
 
 
207
                        if (box->taskResultState == "Done" && this->resultsDone())
 
208
                                col = &color3[0];
 
209
                        else if (box->taskResultState == "Edit" && this->resultsEdit())
 
210
                                col = &color4[0];
 
211
                        else if (box->taskResultState == "Add" && this->resultsAdd())
 
212
                                col = &color[0];
 
213
                        else if (box->taskResultState == "Failed" && this->resultsFailed())
 
214
                                col = &color2[0];
 
215
                        else if (box->taskResultState == "Final" && this->resultsFinal()){
 
216
                                col = &color3[0];
 
217
                        }
 
218
                        else{
 
219
                                noTaskState++;
 
220
                                col = &color2[0];
 
221
                        }
 
222
 
 
223
                        if (box->points.size() == 0){
 
224
                                continue;
 
225
                        }
 
226
                        // single point
 
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);
 
235
                        }
 
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);
 
246
                        }
 
247
                        // draw line between list of points
 
248
                        else{
 
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);
 
255
                                }
 
256
                                // close loop
 
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);
 
263
                                }
 
264
                        }
 
265
 
 
266
                        nBoxes++;       // count paint points   
 
267
 
 
268
                }
 
269
 
 
270
                if (noTaskState > 0)
 
271
                        sout << "!! No task result given for " << noTaskState << " annotations !!" << std::endl;
 
272
                
 
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 );
 
277
                }
 
278
 
 
279
                nBoxesAll += nBoxes;                    
 
280
 
 
281
        }
 
282
 
 
283
        sout << "Painted " << nBoxesAll << " point lists. Thank you and see you again soon!" << std::endl;
 
284
        
 
285
        
 
286
        //const annotation::AnnotationList &list = this->annotations();
 
287
        
 
288
        //const annotation::Frame* frame = list.getFrame("wer");
 
289
        //
 
290
        //sout << frame->frameId << std::endl;
 
291
        //
 
292
        //sout << list.getFrame(0)->frameId << std::endl;
 
293
        
 
294
        //for( auto it = list.begin(); it != list.end(); it++ ){
 
295
        //      sout << it->frameId << ": " << it->annotations.size() << std::endl;
 
296
        //}
 
297
        //
 
298
        //for( auto it = box.begin(); it != box.end(); it++ ){
 
299
        //      sout << it->annotationId << ": " << it->frame->filename << std::endl;
 
300
        //}
 
301
 
 
302
}
 
303
 
 
304
#endif /* _DRAWANNOTATIONS_HXX_ */