|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <opencv2/core.hpp>
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
#include <opencv2/imgcodecs.hpp>
|
|
|
#include <opencv2/videoio.hpp>
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
|
|
#include <fstream>
|
|
|
#include <iostream>
|
|
|
#include <map>
|
|
|
|
|
|
using namespace std;
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
|
void on_mouse(int, int, int, int, void*);
|
|
|
vector<Rect> get_annotations(Mat);
|
|
|
|
|
|
|
|
|
Mat image;
|
|
|
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0, num_of_rec = 0;
|
|
|
bool start_draw = false, stop = 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
vector<Rect> get_annotations(Mat input_image)
|
|
|
{
|
|
|
vector<Rect> current_annotations;
|
|
|
|
|
|
|
|
|
stop = false;
|
|
|
|
|
|
|
|
|
namedWindow(window_name, WINDOW_AUTOSIZE);
|
|
|
setMouseCallback(window_name, on_mouse);
|
|
|
|
|
|
image = input_image;
|
|
|
imshow(window_name, image);
|
|
|
int key_pressed = 0;
|
|
|
|
|
|
do
|
|
|
{
|
|
|
|
|
|
Mat temp_image = input_image.clone();
|
|
|
Rect currentRect(0, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
key_pressed = 0xFF & waitKey(0);
|
|
|
switch( key_pressed )
|
|
|
{
|
|
|
case 27:
|
|
|
stop = true;
|
|
|
break;
|
|
|
case 99:
|
|
|
|
|
|
if(roi_x0<roi_x1 && roi_y0<roi_y1)
|
|
|
{
|
|
|
currentRect.x = roi_x0;
|
|
|
currentRect.y = roi_y0;
|
|
|
currentRect.width = roi_x1-roi_x0;
|
|
|
currentRect.height = roi_y1-roi_y0;
|
|
|
}
|
|
|
|
|
|
if(roi_x0>roi_x1 && roi_y0>roi_y1)
|
|
|
{
|
|
|
currentRect.x = roi_x1;
|
|
|
currentRect.y = roi_y1;
|
|
|
currentRect.width = roi_x0-roi_x1;
|
|
|
currentRect.height = roi_y0-roi_y1;
|
|
|
}
|
|
|
|
|
|
if(roi_x0>roi_x1 && roi_y0<roi_y1)
|
|
|
{
|
|
|
currentRect.x = roi_x1;
|
|
|
currentRect.y = roi_y0;
|
|
|
currentRect.width = roi_x0-roi_x1;
|
|
|
currentRect.height = roi_y1-roi_y0;
|
|
|
}
|
|
|
|
|
|
if(roi_x0<roi_x1 && roi_y0>roi_y1)
|
|
|
{
|
|
|
currentRect.x = roi_x0;
|
|
|
currentRect.y = roi_y1;
|
|
|
currentRect.width = roi_x1-roi_x0;
|
|
|
currentRect.height = roi_y0-roi_y1;
|
|
|
}
|
|
|
|
|
|
|
|
|
current_annotations.push_back(currentRect);
|
|
|
break;
|
|
|
case 100:
|
|
|
|
|
|
if(current_annotations.size() > 0){
|
|
|
current_annotations.pop_back();
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
if(stop)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
for(int i=0; i < (int)current_annotations.size(); i++){
|
|
|
rectangle(temp_image, current_annotations[i], Scalar(0,255,0), 1);
|
|
|
}
|
|
|
image = temp_image;
|
|
|
|
|
|
|
|
|
imshow(window_name, image);
|
|
|
}
|
|
|
|
|
|
while(key_pressed != 110);
|
|
|
|
|
|
|
|
|
destroyWindow(window_name);
|
|
|
|
|
|
|
|
|
return current_annotations;
|
|
|
}
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
|
{
|
|
|
|
|
|
CommandLineParser parser(argc, argv,
|
|
|
"{ help h usage ? | | show this message }"
|
|
|
"{ images i | | (required) path to image folder [example - /data/testimages/] }"
|
|
|
"{ annotations a | | (required) path to annotations txt file [example - /data/annotations.txt] }"
|
|
|
"{ maxWindowHeight m | -1 | (optional) images larger in height than this value will be scaled down }"
|
|
|
"{ resizeFactor r | 2 | (optional) factor for scaling down [default = half the size] }"
|
|
|
);
|
|
|
|
|
|
if (parser.has("help")){
|
|
|
parser.printMessage();
|
|
|
cerr << "TIP: Use absolute paths to avoid any problems with the software!" << endl;
|
|
|
return 0;
|
|
|
}
|
|
|
string image_folder(parser.get<string>("images"));
|
|
|
string annotations_file(parser.get<string>("annotations"));
|
|
|
if (image_folder.empty() || annotations_file.empty()){
|
|
|
parser.printMessage();
|
|
|
cerr << "TIP: Use absolute paths to avoid any problems with the software!" << endl;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
int resizeFactor = parser.get<int>("resizeFactor");
|
|
|
int const maxWindowHeight = parser.get<int>("maxWindowHeight") > 0 ? parser.get<int>("maxWindowHeight") : -1;
|
|
|
|
|
|
|
|
|
|
|
|
map< String, vector<Rect> > annotations;
|
|
|
vector<String> filenames;
|
|
|
String folder(image_folder);
|
|
|
glob(folder, filenames);
|
|
|
|
|
|
|
|
|
cout << "* mark rectangles with the left mouse button," << endl;
|
|
|
cout << "* press 'c' to accept a selection," << endl;
|
|
|
cout << "* press 'd' to delete the latest selection," << endl;
|
|
|
cout << "* press 'n' to proceed with next image," << endl;
|
|
|
cout << "* press 'esc' to stop." << endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < filenames.size(); i++){
|
|
|
|
|
|
Mat current_image = imread(filenames[i]);
|
|
|
bool const resize_bool = (maxWindowHeight > 0) && (current_image.rows > maxWindowHeight);
|
|
|
|
|
|
|
|
|
|
|
|
if(current_image.empty()){
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(resize_bool){
|
|
|
resize(current_image, current_image, Size(current_image.cols/resizeFactor, current_image.rows/resizeFactor), 0, 0, INTER_LINEAR_EXACT);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<Rect> current_annotations = get_annotations(current_image);
|
|
|
if(resize_bool){
|
|
|
for(int j =0; j < (int)current_annotations.size(); j++){
|
|
|
current_annotations[j].x = current_annotations[j].x * resizeFactor;
|
|
|
current_annotations[j].y = current_annotations[j].y * resizeFactor;
|
|
|
current_annotations[j].width = current_annotations[j].width * resizeFactor;
|
|
|
current_annotations[j].height = current_annotations[j].height * resizeFactor;
|
|
|
}
|
|
|
}
|
|
|
annotations[filenames[i]] = current_annotations;
|
|
|
|
|
|
|
|
|
if(stop){
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
for(map<String, vector<Rect> >::iterator it = annotations.begin(); it != annotations.end(); it++){
|
|
|
vector<Rect> &anno = it->second;
|
|
|
output << it->first << " " << anno.size();
|
|
|
for(size_t j=0; j < anno.size(); j++){
|
|
|
Rect temp = anno[j];
|
|
|
output << " " << temp.x << " " << temp.y << " " << temp.width << " " << temp.height;
|
|
|
}
|
|
|
output << endl;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|