-
Notifications
You must be signed in to change notification settings - Fork 0
/
digitize2.m
298 lines (268 loc) · 8.24 KB
/
digitize2.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function varargout = digitize2(varargin)
%DIGITIZE digitize data from image.
% DIGITIZE with no input or output arguments allows the user to
% select an image file to load; only IMREAD-compatible image
% files are supported. The function then prompts the user
% to graphically identify the location of the origin and the X-
% and Y- axes of the plot. The user may then graphically select
% an arbitrary number of data points from anywhere on the image
% using the left mouse button. Data acquisition is terminated
% by clicking the right mouse button. The function then prompts
% the user to save the acquired data to file.
%
% ACQDATA = DIGITIZE with one output argument returns the X- and
% Y- values of the graphically selected data in the array ACQDATA.
% The user is not prompted to save the data to file.
%
% DIGITIZE(FILENAME) with one input argument FILENAME is used to
% directly specify the image file to load. As above, the user is
% prompted to graphically set up the coordinate system and select
% target data points.
%
% See also IMREAD, IMFINFO.
% Author(s): A. Prasad
% Original version created by J.D.Cogdell
% Check for proper number of input arguments
error(nargchk(0,1,nargin));
% Identify image filename
if (nargin == 0),
[filename, pathname] = uigetfile( ...
{'*.jpg;*.tif;*.gif;*.png;*.bmp', ...
'All MATLAB Image Files (*.jpg,*.tif,*.gif,*.png,*.bmp)'; ...
'*.jpg;*.jpeg', ...
'JPEG Files (*.jpg,*.jpeg)'; ...
'*.tif;*.tiff', ...
'TIFF Files (*.tif,*.tiff)'; ...
'*.gif', ...
'GIF Files (*.gif)'; ...
'*.png', ...
'PNG Files (*.png)'; ...
'*.bmp', ...
'Bitmap Files (*.bmp)'; ...
'*.*', ...
'All Files (*.*)'}, ...
'Select image file');
if isequal(filename,0) | isequal(pathname,0)
return
else
imagename = fullfile(pathname, filename);
end
elseif nargin == 1,
imagename = varargin{1};
[path, file,ext] = fileparts(imagename);
filename = strcat(file,ext);
end
% Read image from target filename
pic = imread(imagename);
image(pic)
FigName = ['IMAGE: ' filename];
set(gcf,'Units', 'normalized', ...
'Position', [0 0.125 1 0.85], ...
'Name', FigName, ...
'NumberTitle', 'Off', ...
'MenuBar','None')
set(gca,'Units','normalized','Position',[0 0 1 1]);
% Determine location of origin with mouse click
OriginButton = questdlg('Select the ORIGIN with left mouse button click', ...
'DIGITIZE: user input required', ...
'OK','Cancel','OK');
switch OriginButton,
case 'OK',
drawnow
[Xopixels,Yopixels] = ginput(1);
line(Xopixels,Yopixels,...
'Marker','o','Color','g','MarkerSize',14)
line(Xopixels,Yopixels,...
'Marker','x','Color','g','MarkerSize',14)
case 'Cancel',
close(FigName)
return
end % switch OriginButton
% Prompt user for X- & Y- values at origin
prompt={'Enter the abcissa (X value) at the origin',...
'Enter the ordinate (Y value) at the origin:'};
def={'0','0'};
dlgTitle='DIGITIZE: user input required';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
if (isempty(char(answer{:})) == 1),
close(FigName)
return
else
OriginXYdata = str2num(char(answer{:}));
end
% Define X-axis
XLimButton = questdlg(...
'Select a point on the X-axis with left mouse button click ', ...
'DIGITIZE: user input required', ...
'OK','Cancel','OK');
switch XLimButton,
case 'OK',
drawnow
[XAxisXpixels,XAxisYpixels] = ginput(1);
line(XAxisXpixels,XAxisYpixels,...
'Marker','*','Color','b','MarkerSize',14)
line(XAxisXpixels,XAxisYpixels,...
'Marker','s','Color','b','MarkerSize',14)
case 'Cancel',
close(FigName)
return
end % switch XLimButton
% Prompt user for XLim value
prompt={'Enter the abcissa (X value) at the selected point'};
def={'1'};
dlgTitle='DIGITIZE: user input required';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
if (isempty(char(answer{:})) == 1),
close(FigName)
return
else
XAxisXdata = str2num(char(answer{:}));
end
% Determine X-axis scaling
Xtype = questdlg(...
'Select axis type for absicca (X)', ...
'DIGITIZE: user input required', ...
'LINEAR','LOGARITHMIC','Cancel');
drawnow
switch upper(Xtype),
case 'LINEAR',
logx = 0;
scalefactorXdata = XAxisXdata - OriginXYdata(1);
case 'LOGARITHMIC',
logx = 1;
scalefactorXdata = log10(XAxisXdata/OriginXYdata(1));
case 'CANCEL',
close(FigName)
return
end % switch Xtype
% Rotate image if necessary
% note image file line 1 is at top
th = atan((XAxisYpixels-Yopixels)/(XAxisXpixels-Xopixels));
% axis rotation matrix
rotmat = [cos(th) sin(th); -sin(th) cos(th)];
% Define Y-axis
YLimButton = questdlg(...
'Select a point on the Y-axis with left mouse button click', ...
'DIGITIZE: user input required', ...
'OK','Cancel','OK');
switch YLimButton,
case 'OK',
drawnow
[YAxisXpixels,YAxisYpixels] = ginput(1);
line(YAxisXpixels,YAxisYpixels,...
'Marker','*','Color','b','MarkerSize',14)
line(YAxisXpixels,YAxisYpixels,...
'Marker','s','Color','b','MarkerSize',14)
case 'Cancel',
close(FigName)
return
end % switch YLimButton
% Prompt user for YLim value
prompt={'Enter the ordinate (Y value) at the selected point'};
def={'1'};
dlgTitle='DIGITIZE: user input required';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
if (isempty(char(answer{:})) == 1),
close(FigName)
return
else
YAxisYdata = str2num(char(answer{:}));
end
% Determine Y-axis scaling
Ytype = questdlg('Select axis type for ordinate (Y)', ...
'DIGITIZE: user input required', ...
'LINEAR','LOGARITHMIC','Cancel');
drawnow
switch upper(Ytype),
case 'LINEAR',
logy = 0;
scalefactorYdata = YAxisYdata - OriginXYdata(2);
case 'LOGARITHMIC',
logy = 1;
scalefactorYdata = log10(YAxisYdata/OriginXYdata(2));
case 'CANCEL',
close(FigName)
return
end % switch Ytype
% Complete rotation matrix definition as necessary
delxyx = rotmat*[(XAxisXpixels-Xopixels);(XAxisYpixels-Yopixels)];
delxyy = rotmat*[(YAxisXpixels-Xopixels);(YAxisYpixels-Yopixels)];
delXcal = delxyx(1);
delYcal = delxyy(2);
% Commence Data Acquisition from image
msgStr{1} = 'Click with LEFT mouse button to ACQUIRE';
msgStr{2} = ' ';
msgStr{3} = 'Click with RIGHT mouse button to QUIT';
titleStr = 'Ready for data acquisition';
uiwait(msgbox(msgStr,titleStr,'warn','modal'));
drawnow
numberformat = '%6.2f';
nXY = [];
ng = 0;
while 1,
fprintf(['\n INFO >> Click with RIGHT mouse button to QUIT \n\n']);
n = 0;
disp(sprintf('\n %s \n',' Index X Y'))
% %%%%%%%%%%%%%% DATA ACQUISITION LOOP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while 1
[x,y, buttonNumber] = ginput(1);
xy = rotmat*[(x-Xopixels);(y-Yopixels)];
delXpoint = xy(1);
delYpoint = xy(2);
if buttonNumber == 1,
line(x,y,'Marker','.','Color','r','MarkerSize',12)
if logx,
x = OriginXYdata(1)*10^(delXpoint/delXcal*scalefactorXdata);
else
x = OriginXYdata(1) + delXpoint/delXcal*scalefactorXdata;
end
if logy,
y = OriginXYdata(2)*10^(delYpoint/delYcal*scalefactorYdata);
else
y = OriginXYdata(2) + delYpoint/delYcal*scalefactorYdata;
end
n = n+1;
xpt(n) = x;
ypt(n) = y;
disp(sprintf(' %4d %f %f',n, x, y))
ng = ng+1;
nXY(ng,:) = [n x y];
else
query = questdlg('STOP digitizing and QUIT ?', ...
'DIGITIZE: confirmation', ...
'YES', 'NO', 'NO');
drawnow
switch upper(query),
case 'YES',
disp(sprintf('\n'))
break
case 'NO',
end % switch query
end
end
% %%%%%%%%%%%%%% DATA ACQUISITION LOOP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargout == 0,
% Save data to file
[writefname, writepname] = uiputfile('*.dat','Save data as');
if (writefname == 0) | (writepname == 0),
close(FigName)
break
return
end
writepfname = fullfile(writepname, writefname);
writedata = [xpt' ypt'];
fid = fopen(writepfname,'w');
fprintf(fid,' %g %g\n',writedata');
fclose(fid);
close(FigName)
disp(sprintf('\n'))
elseif nargout == 1,
outputdata = [xpt' ypt'];
varargout{1} = outputdata;
close(FigName);
end
break
end