This source file includes following definitions.
- on_mouse
- int2string
- get_annotations
- main
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <fstream>
#include <iostream>
using namespace std;
using namespace cv;
void on_mouse(int, int, int, int, void*);
string int2string(int);
void get_annotations(Mat, stringstream*);
Mat image;
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0, num_of_rec = 0;
bool start_draw = false;
const string window_name="OpenCV Based Annotation Tool";
void on_mouse(int event, int x, int y, int , void * )
{
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;
}
}
if((event == EVENT_MOUSEMOVE) && start_draw)
{
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);
}
}
string int2string(int num)
{
stringstream temp_stream;
temp_stream << num;
return temp_stream.str();
}
void get_annotations(Mat input_image, stringstream* output_stream)
{
bool stop = false;
num_of_rec = 0;
image = input_image;
namedWindow(window_name, WINDOW_AUTOSIZE);
setMouseCallback(window_name, on_mouse);
imshow(window_name, image);
stringstream temp_stream;
int key_pressed = 0;
do
{
key_pressed = 0xFF & waitKey(0);
switch( key_pressed )
{
case 27:
destroyWindow(window_name);
stop = true;
case 99:
num_of_rec++;
if(roi_x0<roi_x1 && roi_y0<roi_y1)
{
temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y0) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y1-roi_y0);
}
if(roi_x0>roi_x1 && roi_y0>roi_y1)
{
temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y1) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y0-roi_y1);
}
if(roi_x0>roi_x1 && roi_y0<roi_y1)
{
temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y0) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y1-roi_y0);
}
if(roi_x0<roi_x1 && roi_y0>roi_y1)
{
temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y1) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y0-roi_y1);
}
rectangle(input_image, Point(roi_x0,roi_y0), Point(roi_x1,roi_y1), Scalar(0,255,0), 1);
break;
}
if(stop)
{
break;
}
}
while(key_pressed != 110);
if(num_of_rec>0 && key_pressed==110)
{
*output_stream << " " << num_of_rec << temp_stream.str() << endl;
}
destroyWindow(window_name);
}
int main( int argc, const char** argv )
{
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;
return -1;
}
string image_folder;
string annotations;
for(int i = 1; i < argc; ++i )
{
if( !strcmp( argv[i], "-images" ) )
{
image_folder = argv[++i];
}
else if( !strcmp( argv[i], "-annotations" ) )
{
annotations = argv[++i];
}
}
ofstream output(annotations.c_str());
vector<String> filenames;
String folder(image_folder);
glob(folder, filenames);
for (size_t i = 0; i < filenames.size(); i++){
Mat current_image = imread(filenames[i]);
stringstream output_stream;
get_annotations(current_image, &output_stream);
if (output_stream.str() != ""){
output << filenames[i] << output_stream.str();
}
}
return 0;
}