-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateROI.m
153 lines (117 loc) · 4.02 KB
/
CreateROI.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function [S] = CreateROI( path, file, coordinates, bwThresh )
%% CreateROI will create all sub picures.
% + S(x).Picture
% + S(x).PictureGray
% + S(x).BW
% + S(x).Label
% Finally a struct S is saved in the corresponding foler.
%
% Changelog: - no offset for the margin anymore (size of roi = picture
% size now)
%% Concat
document = strcat(path, file);
roiName = '_ROIPosition';
roiFile = strcat(path, roiName, '.jpg');
%% Do check
if ~exist(document, 'file')% || ~exist(coordinates,'file')
clc, clear
disp('Either the document or your coordinates does not exist. Please check.')
return
end
%% Read the image and coordinates and put it in a struct, close the file finally
img = imread(document);
%preallocation S
ll = length(coordinates);
% S(1:ll,1:ll)=zeros;
for ab=1:ll
% Get stuff out of tline
S(ab).Coordinates = coordinates(ab,:); % xmin ymin width height
% Keep track of the specified region
S(ab).Name = ab;
end
%% Create ROIs and a summary map
% colors = ['y'; 'm'; 'c'; 'r'; 'g'; 'b'; 'w'; ]; %create color spec for colored visualization
% get image size
[xI yI zI]=size( img );
% and create an empty image
stitchedImage(xI,yI,zI)=1;
for x=1:length(S)
% Create a ROI for a given coordinate
%% Create a standalone picture according to its coordinates
% required format: (ymin:ymax,xmin:xmax,:)
% given format in S(x) [xmin ymin width height]
%picsize = S(x).Coordinates(3) * S(x).Coordinates(4);
% hoffset = S(x).Coordinates(4) * 0.05; % 0.05 height offset %
% woffset = S(x).Coordinates(3) * 0.05; % 0.05 width offset %
ymin = S(x).Coordinates(2);
ymax = S(x).Coordinates(2) + S(x).Coordinates(4);
xmin = S(x).Coordinates(1);
xmax = S(x).Coordinates(1) + S(x).Coordinates(3);
pictureRegion = img(ymin:ymax,xmin:xmax,:);
S(x).Picture = pictureRegion;
rgbImage = pictureRegion; % Actual image is the rgbImage
S(x).PictureGray = rgb2gray(rgbImage);
S(x).BW = im2bw(S(x).PictureGray, bwThresh);
S(x).Label = file;
if (exist(roiFile, 'file')==0)
% Make an annotation
im = pictureRegion;
[xm ym temp]=size(im);
posy = (xm/2)-10;%(xmax+xmin)/2;
%
posx = (ym/2)+10;%(ymax+ymin)/2;
hf = figure('color','white','units','normalized','position',[.1 .1 .8 .8]);
%image(ones(size(im)));
set(gca,'units','pixels','position',[5 5 size(im,2)-1 size(im,1)-1],'visible','off')
text('units', 'pixels','position',[posx posy],'fontsize',30, 'BackgroundColor',[.7 .9 .7],'string', {x});
% Capture the text image
% Note that the size will have changed by about 1 pixel
tim = getframe(gca);
close(hf)
% Extract the cdata
tim2 = tim.cdata;
% Make a mask with the negative of the text
tmask = tim2==0;
% Place white text
% Replace mask pixels with UINT8 max
im(tmask) = uint8(000);%255);
stitchedImage(ymin:ymax,xmin:xmax,:) = im;
end
end
% Create an image which shows a summary of each ROI
if (exist(roiFile, 'file')==0)
%S().stitchedImage = stitchedImage;
saveOverview(roiFile, stitchedImage);
end
%fid = fopen(coordinates, 'r');
%% ROI stitched picture
%% Fill struct with the coordinates
% region = 0;
% while 1
% tline = fgetl(fid);
%
% if ~ischar(tline) || strcmp(tline, '-1')
% break
% end
% region = region + 1;
%
%
%
% % Get stuff out of tline
% S(region).Coordinates = strread(tline); % xmin ymin width height
%
% % Keep track of the specified region
% S(region).Name = region;
% end
% fclose(fid);
end
function saveOverview(roiFile,image)
%% Create the whole path for the file
% name = '_ROIPosition';
% file = strcat(path, name, '.jpg');
roiFile = char(roiFile);
%% Read the image and convert to gray image
% X = imread(image);
%% Save the image
imwrite(image, roiFile, 'jpg');
end