-
Notifications
You must be signed in to change notification settings - Fork 9
/
drawRectangle.m
70 lines (58 loc) · 1.94 KB
/
drawRectangle.m
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
%% This script creates a simple GUI for annotating grasp bounding boxes on images
%
% Author: Fu-Jen Chu
% Created date: 2017/06/27
%
% usage:
% 1. put your images in data/Images/; and run this script to see a simple GUI
% 2. click vertexes of a rectangle clock-wisely (first one is the upper-left corner); three vertexes are enough for a rotated rectangle
% 3. draw as many as you want on a single image; click Next for next image
%
% note:
% 1. all rotated rectanges for an image will be saved in data/Annoatations/
% 2. four vertexes represent a rectangle in the annotation (same as Cornell dataset)
%
%%
close all
clear
% set up path
addpath('data/')
imgDataDir = 'data/Images/';
annoDataDir = 'data/Annotations/';
imgFiles = dir([imgDataDir '*.png']);
% main loop
for imgidx = 1:length(imgFiles)
% open image and annotation
imgName = imgFiles(imgidx).name;
[pathstr,imgname] = fileparts(imgName);
im = imread([imgDataDir imgName]);
annofileID = fopen([ annoDataDir imgname '_annotations.txt'],'w');
% open gui
figure();
imshow(im);
hold on;
% bbox annotations
H = uicontrol('Style', 'PushButton','String', 'Next','Callback', 'delete(gcbf)');
while(ishandle(H))
% get x and y
[x y] = ginput(1);
hold off; imshow(im); hold on;
plot(x, y, 'ro', 'MarkerSize', 2);
% get width and angle
[x2 y2] = ginput(1);
width = sqrt((x-x2)^2 + (y-y2)^2);
angle = atan((y2-y)/(x2-x));
angle = angle/pi*180;
plot([x x2], [y y2], '-ro', 'MarkerSize', 2, 'LineWidth', 2);
% get height
[x3 y3] = ginput(1);
height = sqrt((x3-x2)^2 + (y3-y2)^2);
% draw rotated rectangle
XY = rectangle_w_angle(x, y, width, height, angle);
% write to file
for idx = 1:4
fprintf(annofileID, '%s\n', [num2str(XY(1,idx)) ' ' num2str(XY(2,idx))]);
end
pause(2);
end
end