opencv_annotation.cpp 11.8 KB
Newer Older
wester committed
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
////////////////////////////////////////////////////////////////////////////////////////
//
//  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
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
////////////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************************************
USAGE:
./opencv_annotation -images <folder location> -annotations <ouput file>

Created by: Puttemans Steven - February 2015
wester committed
49 50 51
Adapted by: Puttemans Steven - April 2016 - Vectorize the process to enable better processing
                                               + early leave and store by pressing an ESC key
                                               + enable delete `d` button, to remove last annotation
wester committed
52 53
*****************************************************************************************************/

wester committed
54 55 56
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
wester committed
57 58 59

#include <fstream>
#include <iostream>
a  
Kai Westerkamp committed
60 61 62 63 64 65

#if defined(_WIN32)
   #include <direct.h>
#else
   #include <sys/stat.h>
#endif
wester committed
66 67 68 69 70 71

using namespace std;
using namespace cv;

// Function prototypes
void on_mouse(int, int, int, int, void*);
wester committed
72
vector<Rect> get_annotations(Mat);
wester committed
73 74 75 76

// Public parameters
Mat image;
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0, num_of_rec = 0;
wester committed
77
bool start_draw = false, stop = false;
wester committed
78 79

// Window name for visualisation purposes
wester committed
80
const string window_name = "OpenCV Based Annotation Tool";
wester committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

// FUNCTION : Mouse response for selecting objects in images
// If left button is clicked, start drawing a rectangle as long as mouse moves
// Stop drawing once a new left click is detected by the on_mouse function
void on_mouse(int event, int x, int y, int , void * )
{
    // Action when left button is clicked
    if(event == EVENT_LBUTTONDOWN)
    {
        if(!start_draw)
        {
            roi_x0 = x;
            roi_y0 = y;
            start_draw = true;
        } else {
            roi_x1 = x;
            roi_y1 = y;
            start_draw = false;
        }
    }
wester committed
101 102

    // Action when mouse is moving and drawing is enabled
wester committed
103 104 105 106 107 108 109 110 111 112
    if((event == EVENT_MOUSEMOVE) && start_draw)
    {
        // Redraw bounding box for annotation
        Mat current_view;
        image.copyTo(current_view);
        rectangle(current_view, Point(roi_x0,roi_y0), Point(x,y), Scalar(0,0,255));
        imshow(window_name, current_view);
    }
}

wester committed
113 114
// FUNCTION : returns a vector of Rect objects given an image containing positive object instances
vector<Rect> get_annotations(Mat input_image)
wester committed
115
{
wester committed
116
    vector<Rect> current_annotations;
wester committed
117

wester committed
118 119
    // Make it possible to exit the annotation process
    stop = false;
wester committed
120 121 122 123 124

    // Init window interface and couple mouse actions
    namedWindow(window_name, WINDOW_AUTOSIZE);
    setMouseCallback(window_name, on_mouse);

wester committed
125
    image = input_image;
wester committed
126 127 128 129 130
    imshow(window_name, image);
    int key_pressed = 0;

    do
    {
wester committed
131 132 133 134
        // Get a temporary image clone
        Mat temp_image = input_image.clone();
        Rect currentRect(0, 0, 0, 0);

wester committed
135 136 137
        // Keys for processing
        // You need to select one for confirming a selection and one to continue to the next image
        // Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
wester committed
138 139 140 141
        //      <c> = 99          add rectangle to current image
        //	<n> = 110	  save added rectangles and show next image
        //      <d> = 100         delete the last annotation made
        //	<ESC> = 27        exit program
wester committed
142 143 144 145 146 147
        key_pressed = 0xFF & waitKey(0);
        switch( key_pressed )
        {
        case 27:
                destroyWindow(window_name);
                stop = true;
wester committed
148
                break;
wester committed
149 150 151 152
        case 99:
                // Draw initiated from top left corner
                if(roi_x0<roi_x1 && roi_y0<roi_y1)
                {
wester committed
153 154 155 156
                    currentRect.x = roi_x0;
                    currentRect.y = roi_y0;
                    currentRect.width = roi_x1-roi_x0;
                    currentRect.height = roi_y1-roi_y0;
wester committed
157 158 159 160
                }
                // Draw initiated from bottom right corner
                if(roi_x0>roi_x1 && roi_y0>roi_y1)
                {
wester committed
161 162 163 164
                    currentRect.x = roi_x1;
                    currentRect.y = roi_y1;
                    currentRect.width = roi_x0-roi_x1;
                    currentRect.height = roi_y0-roi_y1;
wester committed
165 166 167 168
                }
                // Draw initiated from top right corner
                if(roi_x0>roi_x1 && roi_y0<roi_y1)
                {
wester committed
169 170 171 172
                    currentRect.x = roi_x1;
                    currentRect.y = roi_y0;
                    currentRect.width = roi_x0-roi_x1;
                    currentRect.height = roi_y1-roi_y0;
wester committed
173 174 175 176
                }
                // Draw initiated from bottom left corner
                if(roi_x0<roi_x1 && roi_y0>roi_y1)
                {
wester committed
177 178 179 180 181 182 183 184 185 186 187 188 189
                    currentRect.x = roi_x0;
                    currentRect.y = roi_y1;
                    currentRect.width = roi_x1-roi_x0;
                    currentRect.height = roi_y0-roi_y1;
                }
                // Draw the rectangle on the canvas
                // Add the rectangle to the vector of annotations
                current_annotations.push_back(currentRect);
                break;
        case 100:
                // Remove the last annotation
                if(current_annotations.size() > 0){
                    current_annotations.pop_back();
wester committed
190
                }
wester committed
191 192 193 194
                break;
        default:
                // Default case --> do nothing at all
                // Other keystrokes can simply be ignored
wester committed
195 196 197 198 199 200 201 202
                break;
        }

        // Check if escape has been pressed
        if(stop)
        {
            break;
        }
wester committed
203 204 205 206 207 208 209 210 211

        // Draw all the current rectangles onto the top image and make sure that the global image is linked
        for(int i=0; i < (int)current_annotations.size(); i++){
            rectangle(temp_image, current_annotations[i], Scalar(0,255,0), 1);
        }
        image = temp_image;

        // Force an explicit redraw of the canvas --> necessary to visualize delete correctly
        imshow(window_name, image);
wester committed
212 213 214 215 216 217
    }
    // Continue as long as the next image key has not been pressed
    while(key_pressed != 110);

    // Close down the window
    destroyWindow(window_name);
wester committed
218 219 220

    // Return the data
    return current_annotations;
wester committed
221 222 223 224
}

int main( int argc, const char** argv )
{
a  
Kai Westerkamp committed
225 226 227 228 229
    // If no arguments are given, then supply some information on how this tool works
    if( argc == 1 ){
        cout << "Usage: " << argv[0] << endl;
        cout << " -images <folder_location> [example - /data/testimages/]" << endl;
        cout << " -annotations <ouput_file> [example - /data/annotations.txt]" << endl;
wester committed
230
        cout << "TIP: Use absolute paths to avoid any problems with the software!" << endl;
a  
Kai Westerkamp committed
231 232 233
        return -1;
    }

wester committed
234
    // Read in the input arguments
a  
Kai Westerkamp committed
235
    string image_folder;
wester committed
236
    string annotations_file;
a  
Kai Westerkamp committed
237 238 239 240 241 242 243 244
    for(int i = 1; i < argc; ++i )
    {
        if( !strcmp( argv[i], "-images" ) )
        {
            image_folder = argv[++i];
        }
        else if( !strcmp( argv[i], "-annotations" ) )
        {
wester committed
245
            annotations_file = argv[++i];
a  
Kai Westerkamp committed
246 247 248 249 250 251 252 253 254 255 256 257
        }
    }

    // Check if the folder actually exists
    // If -1 is returned then the folder actually exists, and thus you can continue
    // In all other cases there was a folder creation and thus the folder did not exist
    #if defined(_WIN32)
    if(_mkdir(image_folder.c_str()) != -1){
        // Generate an error message
        cerr << "The image folder given does not exist. Please check again!" << endl;
        // Remove the created folder again, to ensure a second run with same code fails again
        _rmdir(image_folder.c_str());
wester committed
258 259
        return 0;
    }
a  
Kai Westerkamp committed
260 261 262 263 264 265 266
    #else
    if(mkdir(image_folder.c_str(), 0777) != -1){
        // Generate an error message
        cerr << "The image folder given does not exist. Please check again!" << endl;
        // Remove the created folder again, to ensure a second run with same code fails again
        remove(image_folder.c_str());
        return 0;
wester committed
267
    }
a  
Kai Westerkamp committed
268
    #endif
wester committed
269

wester committed
270
    // Start by processing the data
wester committed
271
    // Return the image filenames inside the image folder
wester committed
272
    vector< vector<Rect> > annotations;
wester committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    vector<String> filenames;
    String folder(image_folder);
    glob(folder, filenames);

    // Loop through each image stored in the images folder
    // Create and temporarily store the annotations
    // At the end write everything to the annotations file
    for (size_t i = 0; i < filenames.size(); i++){
        // Read in an image
        Mat current_image = imread(filenames[i]);

        // Check if the image is actually read - avoid other files in the folder, because glob() takes them all
        // If not then simply skip this iteration
        if(current_image.empty()){
            continue;
        }

wester committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        // Perform annotations & store the result inside the vectorized structure
        vector<Rect> current_annotations = get_annotations(current_image);
        annotations.push_back(current_annotations);

        // Check if the ESC key was hit, then exit earlier then expected
        if(stop){
            break;
        }
    }

    // When all data is processed, store the data gathered inside the proper file
    // This now even gets called when the ESC button was hit to store preliminary results
    ofstream output(annotations_file.c_str());
    if ( !output.is_open() ){
        cerr << "The path for the output file contains an error and could not be opened. Please check again!" << endl;
        return 0;
    }
wester committed
307

wester committed
308 309 310 311 312 313
    // Store the annotations, write to the output file
    for(int i = 0; i < (int)annotations.size(); i++){
        output << filenames[i] << " " << annotations[i].size();
        for(int j=0; j < (int)annotations[i].size(); j++){
            Rect temp = annotations[i][j];
            output << " " << temp.x << " " << temp.y << " " << temp.width << " " << temp.height;
wester committed
314
        }
wester committed
315
        output << endl;
wester committed
316 317 318 319
    }

    return 0;
}