/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to grid_generator.m

  • Committer: Suren A. Chilingaryan
  • Date: 2010-08-17 01:41:15 UTC
  • Revision ID: csa@dside.dyndns.org-20100817014115-xbh0h6086nz5sj2o
Synchronize with Chris version, set precision to 1, initial implementation of labview wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function [grid_x,grid_y]=grid_generator(FileNameBase,PathNameBase);
 
1
function [grid_x,grid_y]=grid_generator(FileNameBase,PathNameBase, grid_x, grid_y);
2
2
 
3
3
% Code to generate the DIC analysis grid
 
4
% Changed by Sven 
4
5
% Completely rewritten by Chris
5
6
% Programmed first by Dan and Rob 
6
7
7
 
% Last revision: 12/27/06
 
8
% Last revision: 05/09/10 Version 2.0
8
9
 
9
 
% The grid_generator function will help you create grids of markers. The
10
 
% dialog has different options allowing you to create a marker grid which is rectangular,
11
 
% circular, a line or two rectangels of a shape or contains only of two
12
 
% markers. After choosing one of the shapes you will be asked for the base
13
 
% image which is typically your first image. After opening that image you
14
 
% will be asked to click at the sites of interest and the markers will be
15
 
% plotted on top of your image. You can choose if you want to keep these
16
 
% markers or if you want to try again.
17
 
% It has to be noted that you can
18
 
% always generate your own marker positions. Therefore the marker position
19
 
% in pixel has to be saved as a text based format where the x-position is
20
 
% saved as grid_x.dat and the y-position saved as grid_y.dat.
 
10
% The grid_generator function will help you create grids of markers.
 
11
% First you'll be asked for the base image that is used to define the grid
 
12
% which is typically your first image. Then you'll be asked if you want to 
 
13
% modify an old grid or create a new one. The dialog has different options
 
14
% allowing you to create a marker grid which is rectangular, circular, a
 
15
% line or contains only of two markers or delet markers from created grid.
 
16
% Every combination of them is also
 
17
% possible. You will be asked to click at the sites of interest and the
 
18
% markers will be plotted on top of your image. You can choose if you want
 
19
% to keep these markers or if you want to try again. If you keep them they
 
20
% will be saved and you'll come back to the main menu.
 
21
% It has to be noted that you can always generate your own marker 
 
22
% positions. Therefore the marker position in pixel has to be saved as a
 
23
% text based format where the x-position is saved as grid_x.dat and the
 
24
% y-position saved as grid_y.dat.
21
25
%
22
26
 
23
 
 
24
 
 
25
 
% Prompt user for base image
26
 
if exist('FileNameBase')==0
27
 
[FileNameBase,PathNameBase] = uigetfile( ...
 
27
% Check if a grid is loaded if not new variables will be created
 
28
if exist('grid_x','var')~=1
 
29
    grid_x=[];
 
30
end
 
31
 
 
32
if exist('grid_y','var')~=1
 
33
    grid_y=[];
 
34
end
 
35
 
 
36
 
 
37
% Prompt user for base image if no image already assigned
 
38
if exist('FileNameBase','var')~=1
 
39
[FileNameBase,PathNameBase,FilterIndex] = uigetfile( ...
28
40
    {'*.bmp;*.tif;*.jpg;*.TIF;*.BMP;*.JPG','Image files (*.bmp,*.tif,*.jpg)';'*.*',  'All Files (*.*)'}, ...
29
41
    'Open base image for grid creation');
30
 
 
31
 
end
32
 
curdir = pwd;
33
 
cd(PathNameBase);
34
 
im_grid = imread(FileNameBase);
35
 
 
36
 
[grid_x,grid_y,FileNameBase,PathNameBase] = gridtypeselection(FileNameBase, PathNameBase, im_grid);
 
42
   
 
43
end
 
44
% Check if an image is chosen, if true go to directory an load image, call
 
45
% gridtypeselection, if false end program
 
46
if ((exist('FilterIndex', 'var')==0) || (FilterIndex~=0)) 
 
47
    
 
48
     curdir = pwd;
 
49
     cd(PathNameBase);   
 
50
     im_grid = imread(FileNameBase); 
 
51
     [grid_x,grid_y] = gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,1); %call gridselection
 
52
     cd(curdir);
 
53
          
 
54
else %if not the empty variables will be cleared
 
55
    
 
56
     clear  FileNameBase PathNameBase FilterIndex
 
57
         disp('No Image is chosen')
 
58
    
 
59
end
 
60
 
37
61
 
38
62
close all
39
 
cd(curdir);
 
63
end
 
64
%% Main Menu
 
65
function [grid_x,grid_y,FileNameBase,PathNameBase] = gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,loadgrid)
40
66
 
41
 
%-------------------------------
42
 
%
 
67
% Code to select which kind of grid should be added and to display the actual grid
43
68
% Decide which type of grid you want to create
44
 
 
45
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = gridtypeselection(FileNameBase, PathNameBase, im_grid);
46
 
 
47
 
hold off
48
 
imshow(im_grid,'InitialMagnification', 100);
49
 
 
50
 
gridselection = menu(sprintf('Which type of grid do you want to use'),...
51
 
    'Rectangular','Circular','Two Markers','Line','Two Rectangles of Markers','Cancel');
52
 
 
53
 
 
54
 
if gridselection==1
55
 
    [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
56
 
    return
57
 
end
58
 
 
59
 
if gridselection==2
60
 
    [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid);
61
 
    return
62
 
end
63
 
 
64
 
if gridselection==3
65
 
    [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid);
66
 
    return
67
 
end
68
 
 
69
 
if gridselection==4
70
 
    [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid);
71
 
    return
72
 
end
73
 
 
74
 
if gridselection==5
75
 
    [grid_x,grid_y,FileNameBase,PathNameBase] = tworect_grid(FileNameBase, PathNameBase, im_grid);
76
 
    return
77
 
end
78
 
 
79
 
if gridselection==6
80
 
    return;
81
 
end
82
 
 
83
 
 
84
 
 
85
 
%-------------------------------
86
 
%
87
 
% Define two rectangles and add them to one marker array
88
 
 
89
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = tworect_grid(FileNameBase, PathNameBase, im_grid);
90
 
 
91
 
[grid_x1,grid_y1,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
92
 
imshow(im_grid,'InitialMagnification', 100);
93
 
[grid_x2,grid_y2,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
94
 
 
95
 
grid_x1=reshape(grid_x1,[],1);
96
 
grid_x2=reshape(grid_x2,[],1);
97
 
grid_y1=reshape(grid_y1,[],1);
98
 
grid_y2=reshape(grid_y2,[],1);
99
 
 
100
 
grid_x=[grid_x1; grid_x2];
101
 
grid_y=[grid_y1; grid_y2];
102
 
 
103
 
imshow(im_grid,'InitialMagnification', 100);
104
 
hold on
105
 
plot(grid_x,grid_y,'.')
106
 
title(['Selected grid has ',num2str(length(grid_x)), ' rasterpoints'])    % plot a title onto the image
107
 
 
108
 
% Accept the chosen markers, try again or give up 
109
 
 
110
 
confirmcircselection = menu(sprintf('Do you want to use these markers?'),...
111
 
    'Yes','No, try again','Go back to grid-type selection');
112
 
 
113
 
if confirmcircselection==2
114
 
    close all
115
 
    hold off
116
 
    imshow(im_grid,'InitialMagnification', 100);
117
 
    tworect_grid(FileNameBase, PathNameBase, im_grid);
118
 
end
119
 
 
120
 
if confirmcircselection==3
121
 
    close all
122
 
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
123
 
end
124
 
 
125
 
if confirmcircselection==1
126
 
    close all
127
 
    save grid_x.dat grid_x -ascii -tabs
128
 
    save grid_y.dat grid_y -ascii -tabs
129
 
end
130
 
 
131
 
%-------------------------------
132
 
%
133
 
% Define line and create markers
134
 
 
135
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid);
136
 
 
137
 
title(sprintf('Pick two points on the sample.') )
 
69
% In this area you should select the type of grid you want to add or create
 
70
%
 
71
hold off
 
72
imshow(im_grid,'InitialMagnification',100); %show chosen Image
 
73
%------------------------
 
74
% Load old grid?
 
75
 
76
if loadgrid
 
77
loadoldgrid=menu(sprintf('Load old grid?'),...
 
78
'Yes','No');
 
79
 
 
80
if loadoldgrid==1
 
81
        [gridxname,Pathgridx] = uigetfile('*.dat','Open grid_x.dat'); %load grid_x
 
82
                if gridxname==0
 
83
                        disp('You did not select a file!')
 
84
        end
 
85
    cd(Pathgridx);
 
86
    grid_x=importdata(gridxname,'\t');
 
87
        
 
88
        [gridyname,Pathgridy] = uigetfile('*.dat','Open grid_y.dat');%load grid_y
 
89
                if gridyname==0
 
90
                        disp('You did not select a file!')
 
91
        end
 
92
    cd(Pathgridy);
 
93
    grid_y=importdata(gridyname,'\t');
 
94
    
 
95
    
 
96
end
 
97
end
 
98
hold on %plot old grid
 
99
plot(grid_x, grid_y,'+r')
 
100
hold off
 
101
%------------------------
 
102
% Grid selection
 
103
%
 
104
gridselection = menu(sprintf('Gridgenerator Main Menue'),...
 
105
    'Rectangular','Circular','Two Markers','Line','Remove Markers','END');
 
106
 
 
107
 
 
108
    % Rectangular
 
109
    if gridselection==1
 
110
        [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
111
        
 
112
    end
 
113
    % Circular
 
114
    if gridselection==2
 
115
        [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
116
        
 
117
    end
 
118
    % Two Markers
 
119
    if gridselection==3
 
120
        [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
121
        
 
122
    end
 
123
    % Line
 
124
    if gridselection==4
 
125
        [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
126
        
 
127
    end
 
128
    % Remove Markers
 
129
    if gridselection==5
 
130
    [grid_x,grid_y,FileNameBase,PathNameBase] = removepoints(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
131
    
 
132
    end
 
133
    % END
 
134
    if gridselection==6
 
135
    
 
136
    end
 
137
end
 
138
%% Select a rect area
 
139
function [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y)
 
140
 
 
141
% Function to select a rectangular grid and to add these to an existing one
 
142
% wirtten by Chris
 
143
%
 
144
 
 
145
title(sprintf('Define the region of interest.  Pick (single click) a point in the LOWER LEFT region of the gage section.\n  Do the same for a point in the UPPER RIGHT portion of the gage section.'))
138
146
 
139
147
[x(1,1),y(1,1)]=ginput(1);
140
148
hold on
141
 
plot(x(1,1),y(1,1),'+g')
 
149
plot(x(1,1),y(1,1),'+b')
142
150
 
143
151
[x(2,1),y(2,1)]=ginput(1);
144
 
plot(x(2,1),y(2,1),'+g')
145
 
 
146
 
 
147
 
linelength=sqrt((x(2,1)-x(1,1))*(x(2,1)-x(1,1))+(y(2,1)-y(1,1))*(y(2,1)-y(1,1)));
148
 
lineslope=(y(2,1)-y(1,1))/(x(2,1)-x(1,1));
149
 
intersecty=y(1,1)-lineslope*x(1,1);
150
 
ycalc=zeros(2,1);
151
 
ycalc=lineslope*x+intersecty;
152
 
plot(x(:,1),ycalc(:,1),'-b')
153
 
 
154
 
 
155
 
prompt = {'Enter the number of intersections between markers on the line:'};
 
152
hold on
 
153
plot(x(2,1),y(2,1),'+b')
 
154
 
 
155
drawnow
 
156
 
 
157
xmin = min(x);
 
158
xmax = max(x);
 
159
ymin = min(y);
 
160
ymax = max(y);
 
161
 
 
162
lowerline=[xmin ymin; xmax ymin];
 
163
upperline=[xmin ymax; xmax ymax];
 
164
leftline=[xmin ymin; xmin ymax];
 
165
rightline=[xmax ymin; xmax ymax];
 
166
 
 
167
plot(lowerline(:,1),lowerline(:,2),'-b')
 
168
plot(upperline(:,1),upperline(:,2),'-b')
 
169
plot(leftline(:,1),leftline(:,2),'-b')
 
170
plot(rightline(:,1),rightline(:,2),'-b')
 
171
 
 
172
% closereq
 
173
 
 
174
cd(PathNameBase)
 
175
 
 
176
% Prompt user for grid spacing/resolution
 
177
prompt = {'Enter horizontal (x) resolution for image analysis [pixels]:', ...
 
178
        'Enter vertical (y) resolution for image analysis [pixels]:'};
156
179
dlg_title = 'Input for grid creation';
157
180
num_lines= 1;
158
 
def     = {'30'};
 
181
def     = {'50','50'};
159
182
answer = inputdlg(prompt,dlg_title,num_lines,def);
160
 
linediv = str2num(cell2mat(answer(1,1)));
161
 
linestep=((max(x)-min(x))/linediv);
162
 
grid_x(1:linediv+1)=min(x)+linestep*(1:linediv+1)-linestep;
163
 
grid_y=lineslope*grid_x+intersecty;
164
 
 
165
 
plot(grid_x,grid_y,'ob')
166
 
title(['Selected grid has ',num2str(linediv), ' rasterpoints'])    % plot a title onto the image
167
 
 
168
 
% Accept the chosen markers, try again or give up 
169
 
 
170
 
confirmcircselection = menu(sprintf('Do you want to use these markers?'),...
171
 
    'Yes','No, try again','Go back to grid-type selection');
172
 
 
173
 
if confirmcircselection==2
174
 
    close all
175
 
    hold off
176
 
    imshow(im_grid,'InitialMagnification', 100);
177
 
    twop_grid(FileNameBase, PathNameBase, im_grid);
178
 
end
179
 
 
180
 
if confirmcircselection==3
181
 
    close all
182
 
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
183
 
end
184
 
 
185
 
if confirmcircselection==1
186
 
    save grid_x.dat grid_x -ascii -tabs
187
 
    save grid_y.dat grid_y -ascii -tabs
188
 
end
189
 
 
190
 
%-------------------------------
191
 
%
192
 
% Select two markers
193
 
 
194
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid);
195
 
 
196
 
title(sprintf('Pick two points on the sample.') )
197
 
 
198
 
[x(1,1),y(1,1)]=ginput(1);
199
 
hold on
200
 
plot(x(1,1),y(1,1),'+g')
201
 
 
202
 
[x(2,1),y(2,1)]=ginput(1);
203
 
plot(x(2,1),y(2,1),'+g')
204
 
 
205
 
% Accept the chosen markers, try again or give up 
206
 
 
207
 
confirmcircselection = menu(sprintf('Do you want to use these two markers?'),...
208
 
    'Yes','No, try again','Go back to grid-type selection');
209
 
 
210
 
if confirmcircselection==2
211
 
    close all
212
 
    hold off
213
 
    imshow(im_grid,'InitialMagnification', 100);
214
 
    twop_grid(FileNameBase, PathNameBase, im_grid);
215
 
end
216
 
 
217
 
if confirmcircselection==3
218
 
    close all
219
 
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
220
 
end
221
 
 
222
 
if confirmcircselection==1
223
 
    grid_x=x;
224
 
    grid_y=y;
225
 
    save grid_x.dat grid_x -ascii -tabs
226
 
    save grid_y.dat grid_y -ascii -tabs
227
 
end
228
 
%-------------------------------
229
 
%
230
 
% Select a circular area
231
 
 
232
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid);
 
183
xspacing = str2double(cell2mat(answer(1,1)));
 
184
yspacing = str2double(cell2mat(answer(2,1)));
 
185
 
 
186
% Round xmin,xmax and ymin,ymax "up" based on selected spacing
 
187
numXelem = ceil((xmax-xmin)/xspacing)-1;
 
188
numYelem = ceil((ymax-ymin)/yspacing)-1;
 
189
 
 
190
xmin_new = (xmax+xmin)/2-((numXelem/2)*xspacing);
 
191
xmax_new = (xmax+xmin)/2+((numXelem/2)*xspacing);
 
192
ymin_new = (ymax+ymin)/2-((numYelem/2)*yspacing);
 
193
ymax_new = (ymax+ymin)/2+((numYelem/2)*yspacing);
 
194
 
 
195
% Create the analysis grid and show user
 
196
[x,y] = meshgrid(xmin_new:xspacing:xmax_new,ymin_new:yspacing:ymax_new);
 
197
[rows columns] = size(x);
 
198
%zdummy = 200.*ones(rows,columns);
 
199
imshow(FileNameBase)
 
200
title(['Selected grid has ',num2str(rows*columns), ' rasterpoints'])    % plot a title onto the image
 
201
hold on;
 
202
plot(grid_x,grid_y,'+r')
 
203
plot(x,y,'+b')
 
204
 
 
205
 
 
206
% Do you want to keep/add the grid?
 
207
confirmselection = menu(sprintf('Do you want to use this grid?'),...
 
208
    'Yes','No, try again','Go back to Main Menu');
 
209
 
 
210
    % Yes
 
211
    if confirmselection==1
 
212
        % Save settings and grid files in the image directory for visualization/plotting later
 
213
        x=reshape(x,[],1);
 
214
        y=reshape(y,[],1);
 
215
        grid_x=[grid_x;x];
 
216
        grid_y=[grid_y;y];
 
217
        save settings.dat xspacing yspacing xmin_new xmax_new ymin_new ymax_new -ascii -tabs
 
218
        save grid_x.dat grid_x -ascii -tabs
 
219
        save grid_y.dat grid_y -ascii -tabs
 
220
        close all
 
221
        hold off
 
222
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
223
    end
 
224
 
 
225
    % No, try again
 
226
    if confirmselection==2
 
227
        close all
 
228
        hold off
 
229
        imshow(im_grid,'InitialMagnification', 100);
 
230
        hold on;
 
231
        plot(grid_x,grid_y,'+r');
 
232
        hold off;
 
233
        rect_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
234
    end
 
235
    
 
236
    % Go back to Main Menu
 
237
    if confirmselection==3
 
238
        close all
 
239
        hold off
 
240
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
241
    end
 
242
end
 
243
%% Select a circular area
 
244
function [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y)
233
245
 
234
246
title(sprintf('Pick three points on the circle in clockwise order at the upper boundary of the sample.') )
235
247
 
253
265
center12(1,2)=(ynew(2,1)-ynew(1,1))/2+ynew(1,1);
254
266
center23(1,1)=(xnew(3,1)-xnew(2,1))/2+xnew(2,1);
255
267
center23(1,2)=(ynew(3,1)-ynew(2,1))/2+ynew(2,1);
256
 
% plot(center12(1,1),center12(1,2),'+b')
257
 
% plot(center23(1,1),center23(1,2),'+b')
258
 
 
259
 
if slope12==slope23
260
 
    return
261
 
end
 
268
 
 
269
 
262
270
 
263
271
% Calculate the crossing point of the two vectors
264
272
achsenabschnitt1=center12(1,2)-center12(1,1)*slope12;
265
273
achsenabschnitt2=center23(1,2)-center23(1,1)*slope23;
266
 
xdata=min(x):max(x);
267
 
ydata1=achsenabschnitt1+slope12*xdata;
268
 
ydata2=achsenabschnitt2+slope23*xdata;
269
 
% plot(xdata,ydata1,'-b')
270
 
% plot(xdata,ydata2,'-b')
271
274
xcross=(achsenabschnitt2-achsenabschnitt1)/(slope12-slope23);
272
275
ycross=slope12*xcross+achsenabschnitt1;
273
276
plot(xcross,ycross,'or')
274
277
 
275
 
% Calculate radius and plot circle
 
278
% Calculate radius 
276
279
R=sqrt((xcross-xnew(1,1))*(xcross-xnew(1,1))+(ycross-ynew(1,1))*(ycross-ynew(1,1)));
277
 
% ydata=ycross-sqrt(R*R-(xdata-xcross).*(xdata-xcross));
278
 
% plot(xdata,ydata,'-b')
279
280
 
280
281
% Calculate angle between vectors
281
282
xvector=[1;0];
282
 
x1vec(1,1)=xnew(1,1)-xcross;x1vec(2,1)=ynew(1,1)-ycross
283
 
x3vec(1,1)=xnew(3,1)-xcross;x3vec(2,1)=ynew(3,1)-ycross
 
283
x1vec(1,1)=xnew(1,1)-xcross;x1vec(2,1)=ynew(1,1)-ycross;
 
284
x3vec(1,1)=xnew(3,1)-xcross;x3vec(2,1)=ynew(3,1)-ycross;
284
285
alpha13=acos((dot(x1vec,x3vec))/(sqrt(x1vec'*x1vec)*sqrt(x3vec'*x3vec)))*180/pi;
285
 
alpha01=acos((dot(xvector,x1vec))/(sqrt(x1vec'*x1vec)*sqrt(xvector'*xvector)))*180/pi;
286
286
alpha03=acos((dot(xvector,x3vec))/(sqrt(xvector'*xvector)*sqrt(x3vec'*x3vec)))*180/pi;
287
287
totalangle=alpha13;
288
 
minangle=alpha01;
289
288
maxangle=alpha03;
290
289
angldiv=abs(round(totalangle))*10;
291
290
anglstep=(totalangle/angldiv);
302
301
 
303
302
confirmcircselection = menu(sprintf('Do you want to use this circle as basis?'),...
304
303
    'Yes','No, try again','Go back to grid-type selection');
305
 
 
306
 
if confirmcircselection==2
307
 
    close all
308
 
    imshow(im_grid,'InitialMagnification', 100);
309
 
    circ_grid(FileNameBase, PathNameBase, im_grid);
310
 
end
311
 
 
312
 
if confirmcircselection==3
313
 
    close all
314
 
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
315
 
end
316
 
 
317
 
if confirmcircselection==1
318
 
    
319
 
    prompt = {'Enter the number of intersections between markers on the circle:'};
320
 
    dlg_title = 'Input for grid creation';
321
 
    num_lines= 1;
322
 
    def     = {'30'};
323
 
    answer = inputdlg(prompt,dlg_title,num_lines,def);
324
 
    angldiv = str2num(cell2mat(answer(1,1)));
325
 
    
326
 
    anglstep=(totalangle/angldiv);
327
 
    anglall(1:angldiv+1)=maxangle+anglstep*(1:angldiv+1)-anglstep;
328
 
    
329
 
    markerxpos(1:angldiv+1)=xcross+R*cos(-anglall(1:angldiv+1)/180*pi);
330
 
    markerypos(1:angldiv+1)=ycross+R*sin(-anglall(1:angldiv+1)/180*pi);
331
 
    
332
 
    plot(markerxpos,markerypos,'ob');
333
 
    
334
 
    % Pick the lower bound in the image
335
 
    title(sprintf('Pick three points lying on the circle in clockwise order. The first and last one define the width of the raster') )
336
 
    
337
 
    [x(4,1),y(4,1)]=ginput(1);
338
 
    hold on
339
 
    plot(x(1,1),y(1,1),'+r')
340
 
    
341
 
    lowboundx=x(4,1);
342
 
    lowboundy=y(4,1);
343
 
    
344
 
    R2=sqrt((xcross-lowboundx(1,1))*(xcross-lowboundx(1,1))+(ycross-lowboundy(1,1))*(ycross-lowboundy(1,1)));
345
 
    markerxposlb(1:angldiv+1)=xcross+R2*cos(-anglall(1:angldiv+1)/180*pi);
346
 
    markeryposlb(1:angldiv+1)=ycross+R2*sin(-anglall(1:angldiv+1)/180*pi);
347
 
    
348
 
    plot(markerxposlb,markeryposlb,'ob');
349
 
    
350
 
    prompt = {'Enter the number of intersections between the upper and lower bound:'};
351
 
    dlg_title = 'Input for grid creation';
352
 
    num_lines= 1;
353
 
    def     = {'5'};
354
 
    answer = inputdlg(prompt,dlg_title,num_lines,def);
355
 
    Rdiv = str2num(cell2mat(answer(1,1)));
356
 
    
357
 
    Rstep=((R-R2)/Rdiv);
358
 
    Rall(1:Rdiv+1)=R2+Rstep*(1:Rdiv+1)-Rstep;
359
 
    
360
 
    grid_x=ones(Rdiv+1,angldiv+1)*xcross;
361
 
    grid_y=ones(Rdiv+1,angldiv+1)*ycross;
362
 
    A=Rall;
363
 
    B=cos(-anglall(1:angldiv+1)/180*pi);
364
 
    C=A'*B;
365
 
    grid_x=grid_x+Rall'*cos(-anglall(1:angldiv+1)/180*pi);
366
 
    grid_y=grid_y+Rall'*sin(-anglall(1:angldiv+1)/180*pi);
367
 
    
368
 
    close all
369
 
    imshow(im_grid,'InitialMagnification', 100);
370
 
    hold on
371
 
    plot(grid_x,grid_y,'.b')    
372
 
    
373
 
    title(['Selected grid has ',num2str(angldiv*Rdiv), ' rasterpoints'])    % plot a title onto the image
374
 
 
375
 
    
376
 
    % Do you want to keep the grid?
377
 
    confirmselection = menu(sprintf('Do you want to use this grid?'),...
378
 
        'Yes','No, try again','Go back to grid-type selection');
379
 
    
 
304
    
 
305
    % No, try again
 
306
    if confirmcircselection==2
 
307
        close all
 
308
        imshow(im_grid,'InitialMagnification', 100);
 
309
        hold on
 
310
        plot(grid_x,grid_y,'+r');
 
311
        hold off
 
312
        circ_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
313
    end
 
314
    
 
315
    % Go back to grid-type selection
 
316
    if confirmcircselection==3
 
317
        close all
 
318
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
319
    end
 
320
 
 
321
    % Yes
 
322
    if confirmcircselection==1
 
323
 
 
324
        prompt = {'Enter the number of intersections between markers on the circle:'};
 
325
        dlg_title = 'Input for grid creation';
 
326
        num_lines= 1;
 
327
        def     = {'30'};
 
328
        answer = inputdlg(prompt,dlg_title,num_lines,def);
 
329
        angldiv = str2double(cell2mat(answer(1,1)));
 
330
 
 
331
        anglstep=(totalangle/angldiv);
 
332
        anglall(1:angldiv+1)=maxangle+anglstep*(1:angldiv+1)-anglstep;
 
333
 
 
334
        markerxpos(1:angldiv+1)=xcross+R*cos(-anglall(1:angldiv+1)/180*pi);
 
335
        markerypos(1:angldiv+1)=ycross+R*sin(-anglall(1:angldiv+1)/180*pi);
 
336
 
 
337
        plot(markerxpos,markerypos,'ob');
 
338
 
 
339
        % Pick the lower bound in the image
 
340
        title(sprintf('Pick three points lying on the circle in clockwise order. The first and last one define the width of the raster') )
 
341
 
 
342
        [x(4,1),y(4,1)]=ginput(1);
 
343
        hold on
 
344
        plot(x(1,1),y(1,1),'+r')
 
345
 
 
346
        lowboundx=x(4,1);
 
347
        lowboundy=y(4,1);
 
348
 
 
349
        R2=sqrt((xcross-lowboundx(1,1))*(xcross-lowboundx(1,1))+(ycross-lowboundy(1,1))*(ycross-lowboundy(1,1)));
 
350
        markerxposlb(1:angldiv+1)=xcross+R2*cos(-anglall(1:angldiv+1)/180*pi);
 
351
        markeryposlb(1:angldiv+1)=ycross+R2*sin(-anglall(1:angldiv+1)/180*pi);
 
352
 
 
353
        plot(markerxposlb,markeryposlb,'ob');
 
354
 
 
355
        prompt = {'Enter the number of intersections between the upper and lower bound:'};
 
356
        dlg_title = 'Input for grid creation';
 
357
        num_lines= 1;
 
358
        def     = {'5'};
 
359
        answer = inputdlg(prompt,dlg_title,num_lines,def);
 
360
        Rdiv = str2double(cell2mat(answer(1,1)));
 
361
 
 
362
        Rstep=((R-R2)/Rdiv);
 
363
        Rall(1:Rdiv+1)=R2+Rstep*(1:Rdiv+1)-Rstep;
 
364
 
 
365
        x=ones(Rdiv+1,angldiv+1)*xcross;
 
366
        y=ones(Rdiv+1,angldiv+1)*ycross;
 
367
        x=x+Rall'*cos(-anglall(1:angldiv+1)/180*pi);
 
368
        y=y+Rall'*sin(-anglall(1:angldiv+1)/180*pi);
 
369
 
 
370
        close all
 
371
        imshow(im_grid,'InitialMagnification', 100);
 
372
        hold on
 
373
        plot(grid_x,grid_y,'+r')    
 
374
        plot(x,y,'.b')    
 
375
 
 
376
        title(['Selected grid has ',num2str(angldiv*Rdiv), ' rasterpoints'])    % plot a title onto the image
 
377
 
 
378
 
 
379
         % Do you want to keep/add the grid?
 
380
        confirmselection = menu(sprintf('Do you want to use this grid?'),...
 
381
        'Yes','No, try again','Go back to Main Menu');
 
382
 
 
383
        % Yes
 
384
        if confirmselection==1
 
385
            % Save settings and grid files in the image directory for visualization/plotting later
 
386
            x=reshape(x,[],1);
 
387
            y=reshape(y,[],1);
 
388
            grid_x=[grid_x;x];
 
389
            grid_y=[grid_y;y];
 
390
            save grid_x.dat grid_x -ascii -tabs
 
391
            save grid_y.dat grid_y -ascii -tabs
 
392
            close all
 
393
            hold off
 
394
            gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
395
        end
 
396
 
 
397
        % No, try again
 
398
        if confirmselection==2
 
399
            close all
 
400
            hold off
 
401
            imshow(im_grid,'InitialMagnification', 100);
 
402
            hold on;
 
403
            plot(grid_x,grid_y,'+r');
 
404
            hold off;
 
405
            circ_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
406
        end
 
407
 
 
408
        % Go back to Main Menu
 
409
        if confirmselection==3
 
410
            close all
 
411
            hold off
 
412
            gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
413
        end
 
414
    end
 
415
end
 
416
%% Select 2 Points
 
417
function [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y)
 
418
 
 
419
title(sprintf('Pick two points on the sample.') )
 
420
 
 
421
[x(1,1),y(1,1)]=ginput(1);
 
422
hold on
 
423
plot(x(1,1),y(1,1),'+b')
 
424
 
 
425
[x(2,1),y(2,1)]=ginput(1);
 
426
plot(x(2,1),y(2,1),'+b')
 
427
 
 
428
% Do you want to keep/add the grid?
 
429
confirmselection = menu(sprintf('Do you want to use this grid?'),...
 
430
    'Yes','No, try again','Go back to Main Menu');
 
431
 
 
432
    % Yes
380
433
    if confirmselection==1
381
434
        % Save settings and grid files in the image directory for visualization/plotting later
382
 
        %         save settings.dat xspacing yspacing xmin_new xmax_new ymin_new ymax_new -ascii -tabs
 
435
        x=reshape(x,[],1);
 
436
        y=reshape(y,[],1);
 
437
        grid_x=[grid_x;x];
 
438
        grid_y=[grid_y;y];
383
439
        save grid_x.dat grid_x -ascii -tabs
384
440
        save grid_y.dat grid_y -ascii -tabs
 
441
        close all
 
442
        hold off
 
443
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
385
444
    end
386
 
    
 
445
 
 
446
    % No, try again
387
447
    if confirmselection==2
388
448
        close all
389
449
        hold off
390
 
        imshow(im_grid,'InitialMagnification', 100);
391
 
        circ_grid(FileNameBase, PathNameBase, im_grid);
 
450
        imshow(im_grid,'InitialMagnification', 100);
 
451
        hold on;
 
452
        plot(grid_x,grid_y,'+r');
 
453
        hold off;
 
454
        twop_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
392
455
    end
393
456
    
 
457
    % Go back to Main Menu
394
458
    if confirmselection==3
395
 
        gridtypeselection(FileNameBase, PathNameBase, im_grid);
 
459
        close all
 
460
        hold off
 
461
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
396
462
    end
397
 
    
398
463
end
399
 
 
400
 
 
401
 
return
402
 
 
403
 
 
404
 
 
405
 
%-------------------------------
406
 
%
407
 
 
408
 
function [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
409
 
 
410
 
title(sprintf('Define the region of interest.  Pick (single click) a point in the LOWER LEFT region of the gage section.\n  Do the same for a point in the UPPER RIGHT portion of the gage section.'))
 
464
%% Select a line
 
465
function [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y)
 
466
 
 
467
title(sprintf('Pick two points on the sample.') )
411
468
 
412
469
[x(1,1),y(1,1)]=ginput(1);
413
470
hold on
414
471
plot(x(1,1),y(1,1),'+b')
415
472
 
416
473
[x(2,1),y(2,1)]=ginput(1);
417
 
hold on
418
474
plot(x(2,1),y(2,1),'+b')
419
475
 
420
 
drawnow
421
 
 
422
 
xmin = min(x);
423
 
xmax = max(x);
424
 
ymin = min(y);
425
 
ymax = max(y);
426
 
 
427
 
lowerline=[xmin ymin; xmax ymin];
428
 
upperline=[xmin ymax; xmax ymax];
429
 
leftline=[xmin ymin; xmin ymax];
430
 
rightline=[xmax ymin; xmax ymax];
431
 
 
432
 
plot(lowerline(:,1),lowerline(:,2),'-b')
433
 
plot(upperline(:,1),upperline(:,2),'-b')
434
 
plot(leftline(:,1),leftline(:,2),'-b')
435
 
plot(rightline(:,1),rightline(:,2),'-b')
436
 
 
437
 
% closereq
438
 
 
439
 
cd(PathNameBase)
440
 
 
441
 
% Prompt user for grid spacing/resolution
442
 
prompt = {'Enter horizontal (x) resolution for image analysis [pixels]:', ...
443
 
        'Enter vertical (y) resolution for image analysis [pixels]:'};
 
476
lineslope=(y(2,1)-y(1,1))/(x(2,1)-x(1,1));
 
477
intersecty=y(1,1)-lineslope*x(1,1);
 
478
ycalc=zeros(2,1);
 
479
ycalc=lineslope*x+intersecty;
 
480
plot(x(:,1),ycalc(:,1),'-b')
 
481
 
 
482
 
 
483
prompt = {'Enter the number of intersections between markers on the line:'};
444
484
dlg_title = 'Input for grid creation';
445
485
num_lines= 1;
446
 
def     = {'50','50'};
 
486
def     = {'30'};
447
487
answer = inputdlg(prompt,dlg_title,num_lines,def);
448
 
xspacing = str2num(cell2mat(answer(1,1)));
449
 
yspacing = str2num(cell2mat(answer(2,1)));
450
 
 
451
 
% Round xmin,xmax and ymin,ymax "up" based on selected spacing
452
 
numXelem = ceil((xmax-xmin)/xspacing)-1;
453
 
numYelem = ceil((ymax-ymin)/yspacing)-1;
454
 
 
455
 
xmin_new = (xmax+xmin)/2-((numXelem/2)*xspacing);
456
 
xmax_new = (xmax+xmin)/2+((numXelem/2)*xspacing);
457
 
ymin_new = (ymax+ymin)/2-((numYelem/2)*yspacing);
458
 
ymax_new = (ymax+ymin)/2+((numYelem/2)*yspacing);
459
 
 
460
 
% Create the analysis grid and show user
461
 
[x,y] = meshgrid(xmin_new:xspacing:xmax_new,ymin_new:yspacing:ymax_new);
462
 
[rows columns] = size(x);
463
 
zdummy = 200.*ones(rows,columns);
464
 
imshow(FileNameBase)
465
 
title(['Selected grid has ',num2str(rows*columns), ' rasterpoints'])    % plot a title onto the image
466
 
hold on;
467
 
plot(x,y,'+b')
468
 
 
469
 
grid_x=x;
470
 
grid_y=y;
471
 
 
472
 
% Do you want to keep the grid?
 
488
linediv = str2num(cell2mat(answer(1,1)));
 
489
linestep=((max(x)-min(x))/linediv);
 
490
x(1:linediv+1)=min(x)+linestep*(1:linediv+1)-linestep;
 
491
y=lineslope*x+intersecty;
 
492
 
 
493
plot(x,y,'ob')
 
494
title(['Selected grid has ',num2str(linediv), ' rasterpoints'])    % plot a title onto the image
 
495
 
 
496
 
 
497
% Do you want to keep/add the grid?
473
498
confirmselection = menu(sprintf('Do you want to use this grid?'),...
474
 
    'Yes','No, try again','Go back to grid-type selection');
475
 
 
476
 
if confirmselection==1
477
 
    % Save settings and grid files in the image directory for visualization/plotting later
478
 
    save settings.dat xspacing yspacing xmin_new xmax_new ymin_new ymax_new -ascii -tabs
479
 
    save grid_x.dat x -ascii -tabs
480
 
    save grid_y.dat y -ascii -tabs
481
 
    close all
482
 
    hold off
483
 
end
484
 
 
485
 
if confirmselection==2
486
 
    close all
487
 
    hold off
488
 
    imshow(im_grid,'InitialMagnification', 100);
489
 
    rect_grid(FileNameBase, PathNameBase, im_grid);
490
 
end
491
 
 
492
 
if confirmselection==3
493
 
    close all
494
 
    hold off
495
 
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
496
 
end
 
499
    'Yes','No, try again','Go back to Main Menu');
 
500
 
 
501
    % Yes
 
502
    if confirmselection==1
 
503
        % Save settings and grid files in the image directory for visualization/plotting later
 
504
        x=reshape(x,[],1);
 
505
        y=reshape(y,[],1);
 
506
        grid_x=[grid_x;x];
 
507
        grid_y=[grid_y;y];
 
508
        save grid_x.dat grid_x -ascii -tabs
 
509
        save grid_y.dat grid_y -ascii -tabs
 
510
        close all
 
511
        hold off
 
512
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
513
    end
 
514
 
 
515
    % No, try again
 
516
    if confirmselection==2
 
517
        close all
 
518
        hold off
 
519
        imshow(im_grid,'InitialMagnification', 100);
 
520
        hold on;
 
521
        plot(grid_x,grid_y,'+r');
 
522
        hold off;
 
523
        line_grid(FileNameBase, PathNameBase, im_grid, grid_x, grid_y);
 
524
    end
 
525
    
 
526
    % Go back to Main Menu
 
527
    if confirmselection==3
 
528
        close all
 
529
        hold off
 
530
        gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
531
    end
 
532
end
 
533
%% Select Points to remove
 
534
function [grid_x,grid_y,FileNameBase,PathNameBase] = removepoints(FileNameBase, PathNameBase, im_grid, grid_x, grid_y)
 
535
 
 
536
% Delete some markers
 
537
%
 
538
 
 
539
%create working copy of the grid
 
540
 
 
541
grid_xtemp=grid_x;
 
542
grid_ytemp=grid_y;
 
543
 
 
544
imshow(im_grid,'InitialMagnification',100); %show chosen Image
 
545
hold on
 
546
plot(grid_x, grid_y,'ob');
 
547
hold off,
 
548
 
 
549
title(sprintf('Define the region of interest.  \n  All points inside that region will be deleted'))
 
550
 
 
551
        [xdel,ydel]=ginput(2);
 
552
        x(1,1) = xdel(1);
 
553
        x(1,2) = xdel(2);
 
554
        y(1,1) = ydel(2);
 
555
        y(1,2) = ydel(1);
 
556
 
 
557
        deletepoints=find(grid_x>min(x) & grid_x<max(x) & grid_y<max(y) & grid_y>min(y));
 
558
        
 
559
        grid_xtemp(deletepoints,:)=[];
 
560
        grid_ytemp(deletepoints,:)=[];
 
561
 
 
562
        imshow(im_grid,'InitialMagnification',100); %show chosen Image
 
563
        hold on
 
564
        plot(grid_xtemp, grid_ytemp,'ob');
 
565
        hold off,
 
566
 
 
567
        
 
568
        % delete point permanently?
 
569
        keepchanges = menu(sprintf('Do you want to delete these markers permanently?'),'Yes','No');
 
570
        if keepchanges==1
 
571
            grid_x=grid_xtemp;
 
572
            grid_y=grid_ytemp;
 
573
            save grid_x.dat grid_x -ascii -tabs
 
574
            save grid_y.dat grid_y -ascii -tabs
 
575
            close all
 
576
            hold off
 
577
            gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
578
        end
 
579
        
 
580
        if keepchanges==2
 
581
            gridtypeselection(FileNameBase, PathNameBase, im_grid, grid_x, grid_y,0);
 
582
        end
 
583
        
 
584
        
 
585
end
 
 
b'\\ No newline at end of file'