-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfringe_spacing.m
306 lines (300 loc) · 14.8 KB
/
interfringe_spacing.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
% Last change (can be reversed by uncommenting lines): the criteria for
% acceptance of two fringes to be stacked is no longer that the mean
% distance between the fringes is within the min and max limits. Now the
% distance between each adyacent pixel of the two fringes has to be below
% the max limit.
function spacing = interfringe_spacing(fringes,I8,px_nm,min_len,min_spa,max_spa,fc,fileName)
cc=bwconncomp(I8,8);
for i=1:fringes.number-1;
for j=i+1:fringes.number;
fringe1=fringes.coordinates(i).XY;
row1=fringe1(:,1); col1=fringe1(:,2);
fringe2=fringes.coordinates(j).XY;
row2=fringe2(:,1); col2=fringe2(:,2);
% If the fringe i is SMALLER thant the fringe j and the fringes
% orientation is between -45 and 45 degrees (tending to horizontal)
if length(fringe1) <= length(fringe2);
if fringes.orientation(i) <= 45 && fringes.orientation(i) >= -45;
%find the fringe i respect to the fringe j
%trimming process
disI=sqrt((row2-row1(1)).^2+(col2-col1(1)).^2);
disF=sqrt((row2-row1(length(row1))).^2+(col2-col1(length(col1))).^2);
pxi2=find(disI==min(disI)); %pxi2=find(row2==row1(1));
pxf2=find(disF==min(disF)); %pxf2=find(row2==row1(length(row1)));
if isempty(pxi2)==1 && isempty(pxf2)==1;
else
if isempty(pxi2)==1;
pxi1=find(col1==col2(1));
pxi2=1;
else
pxi2=pxi2(1);
pxi1=1;
end
if isempty(pxf2)==1;
pxf1=find(col1==col2(length(col2)));
pxf2=length(col2);
else
pxf2=pxf2(length(pxf2));
pxf1=length(col1);
end
if pxf1-pxi1 <= pxf2-pxi2;
rowa=row1(pxi1:pxf1);
cola=col1(pxi1:pxf1);
rowb=row2(pxi2:(pxi2+pxf1-pxi1));
colb=col2(pxi2:(pxi2+pxf1-pxi1));
else
rowa=row1(pxi1:(pxi1+pxf2-pxi2));
cola=col1(pxi1:(pxi1+pxf2-pxi2));
rowb=row2(pxi2:pxf2);
colb=col2(pxi2:pxf2);
end
%having trimmed the two "paralell" fringes to the same lenght find the distance between the two pieces
% (only if the paralell part has more than min_len)
dist=0; distn=0;
if length(rowa)>= round(0.8*min_len*px_nm) && length(rowb)>= round(0.8*min_len*px_nm)
for k=1:length(rowa);
for l=1:length(rowb);
dist(l)=sqrt((rowa(k)-rowb(l))^2+(cola(k)-colb(l))^2);
end
distn(k)=min(dist);
end
%distance=mean(distn);
distance=(distn);
else
distance = 0;
end
%If the distance between the two fringes is less than max spacing allowed nm then
%they are stacked
if sum(distance< (max_spa*px_nm))/numel(distance)==1 && mean(distance)>= min_spa*px_nm
%if distance<= max_spa*px_nm && distance>= min_spa*px_nm;
spacing(i,j)=mean(distance);
%spacing(i,j)=(distance)/px_nm;
%figure
%imshow(fc);
%hold on
scatter(cola,rowa,2,'g','filled','o');
scatter(colb,rowb,2,'g','filled','o');
%export_fig(['C:\Users\Maria\Documents\Postdoc UdeA\HRTEM images\Cristian Avila - Imagenes TEM\HRTEM\Spacing\',fileName,'_',num2str(spacing(i,j)),'.tif'],'-transparent');
%hold on
else
spacing(i,j)=0;
end
end
% If the fringe i is SMALLER thant the fringe j and the fringes
% orientation is less -45 or more 45 degrees (tending to vertical)
else
%find if the fringe i respect to the fringe j
%trimming process
disI=sqrt((row2-row1(1)).^2+(col2-col1(1)).^2);
disF=sqrt((row2-row1(length(row1))).^2+(col2-col1(length(col1))).^2);
pxi2=find(disI==min(disI)); %pxi2=find(row2==row1(1));
pxf2=find(disF==min(disF)); %pxf2=find(row2==row1(length(row1)));
if isempty(pxi2)==1 && isempty(pxf2)==1;
else
if isempty(pxi2)==1;
pxi1=find(row1==row2(1));
pxi2=1;
else
pxi2=pxi2(1);
pxi1=1;
end
if isempty(pxf2)==1;
pxf1=find(row1==row2(length(row2)));
pxf2=length(row2);
else
pxf2=pxf2(length(pxf2));
pxf1=length(row1);
end
if isempty(pxf1)==1 || isempty(pxf2)==1 || isempty(pxi1)==1 || isempty(pxi2)==1;
rowa=100; rowb=0; cola=100; colb=0;
elseif pxf1-pxi1 <= pxf2-pxi2;
rowa=row1(pxi1:pxf1);
cola=col1(pxi1:pxf1);
rowb=row2(pxi2:(pxi2+pxf1-pxi1));
colb=col2(pxi2:(pxi2+pxf1-pxi1));
else
rowa=row1(pxi1:(pxi1+pxf2-pxi2));
cola=col1(pxi1:(pxi1+pxf2-pxi2));
rowb=row2(pxi2:pxf2);
colb=col2(pxi2:pxf2);
end
%having trimmed the two "paralell" fringes to the same lenght find the distance between the two pieces
% (only if the paralell part is larger than min_len)
dist=0; distn=0;
if length(rowa)>= round(0.8*min_len*px_nm) && length(rowb)>= round(0.8*min_len*px_nm)
for k=1:length(rowa);
for l=1:length(rowb);
dist(l)=sqrt((rowa(k)-rowb(l))^2+(cola(k)-colb(l))^2);
end
distn(k)=min(dist);
end
distance=(distn);
%distance=mean(distn);
else
distance=0;
end
%If the distance between the two fringes is less than max spacing allowednm then
%they are stacked
if sum(distance< (max_spa*px_nm))/numel(distance)==1 && mean(distance)>= min_spa*px_nm
%if distance<= (max_spa*px_nm) && distance>= min_spa*px_nm;
%spacing(i,j)=(distance)/px_nm;
spacing(i,j)=mean(distance);
%figure
%imshow(fc);
%hold on
scatter(cola,rowa,2,'g','filled','o')
scatter(colb,rowb,2,'g','filled','o')
%export_fig(['C:\Users\Maria\Documents\Postdoc UdeA\HRTEM images\Cristian Avila - Imagenes TEM\HRTEM\Spacing\',fileName,'_',num2str(spacing(i,j)),'.tif'],'-transparent');
% hold on
else
spacing(i,j)=0;
end
end
end
% If the fringe i is BIGGER than the fringe j and the fringes
% orientation is between -45 and 45 degrees (tending to horizontal)
else
if fringes.orientation(i) <= 45 && fringes.orientation(i) >= -45;
%find if the fringe j respect to the fringe i
%trimming process
disI=sqrt((row1-row2(1)).^2+(col1-col2(1)).^2);
disF=sqrt((row1-row2(length(row2))).^2+(col1-col2(length(col2))).^2);
pxi1=find(disI==min(disI)); %find(row1==row2(1));
pxf1=find(disF==min(disF)); %find(row1==row2(length(row2)));
if isempty(pxi1)==1 && isempty(pxf1)==1;
else
if isempty(pxi1)==1;
pxi2=find(col2==col1(1));
pxi1=1;
else
pxi1=pxi1(1);
pxi2=1;
end
if isempty(pxf1)==1;
pxf2=find(col2==col1(length(col1)));
pxf1=length(col1);
else
pxf1=pxf1(length(pxf1));
pxf2=length(col2);
end
if isempty(pxf1)==1 || isempty(pxf2)==1 || isempty(pxi1)==1 || isempty(pxi2)==1;
rowa=100; rowb=0; cola=100; colb=0;
elseif pxf1-pxi1 <= pxf2-pxi2;
rowa=row1(pxi1:pxf1);
cola=col1(pxi1:pxf1);
rowb=row2(pxi2:(pxi2+pxf1-pxi1));
colb=col2(pxi2:(pxi2+pxf1-pxi1));
else
rowa=row1(pxi1:(pxi1+pxf2-pxi2));
cola=col1(pxi1:(pxi1+pxf2-pxi2));
rowb=row2(pxi2:pxf2);
colb=col2(pxi2:pxf2);
end
%having trimmed the two "paralell" fringes to the same lenght find the distance between the two pieces
% (only if the paralell part is larger than min_len)
dist=0; distn=0;
if length(rowa)>= round(0.8*min_len*px_nm) && length(rowb)>= round(0.8*min_len*px_nm)
for k=1:length(rowa);
for l=1:length(rowb);
dist(l)=sqrt((rowa(k)-rowb(l))^2+(cola(k)-colb(l))^2);
end
distn(k)=min(dist);
end
%distance=mean(distn);
distance=(distn);
else
distance=0;
end
%If the distance between the two fringes is less than max allowed space then
%they are stacked
if sum(distance< (max_spa*px_nm))/numel(distance)==1 && mean(distance)>= min_spa*px_nm
%if distance<= max_spa*px_nm && distance>= min_spa*px_nm;
%spacing(i,j)=(distance)/px_nm;
spacing(i,j)=mean(distance);
%figure
%imshow(fc);
%hold on
scatter(cola,rowa,2,'g','filled','o')
scatter(colb,rowb,2,'g','filled','o')
%export_fig(['C:\Users\Maria\Documents\Postdoc UdeA\HRTEM images\Cristian Avila - Imagenes TEM\HRTEM\Spacing\',fileName,'_',num2str(spacing(i,j)),'.tif'],'-transparent');
% hold on
else
spacing(i,j)=0;
end
end
% If the fringe i is BIGGER thant the fringe j and the fringes
% orientation is less -45 or more 45 degrees (tending to vertical)
else fringes.orientation(i) >= 45 && fringes.orientation(i) <= -45;
%find if the fringe j respect to the fringe i
%trimming process
disI=sqrt((row1-row2(1)).^2+(col1-col2(1)).^2);
disF=sqrt((row1-row2(length(row2))).^2+(col1-col2(length(col2))).^2);
pxi1=find(disI==min(disI)); %find(row1==row2(1));
pxf1=find(disF==min(disF)); %find(row1==row2(length(row2)));
if isempty(pxi1)==1 && isempty(pxf1)==1;
else
if isempty(pxi1)==1;
pxi2=find(row2==row1(1));
pxi1=1;
else
pxi1=pxi1(1);
pxi2=1;
end
if isempty(pxf1)==1;
pxf2=find(row2==row1(length(row1)));
pxf1=length(row1);
else
pxf1=pxf1(length(pxf1));
pxf2=length(row2);
end
if isempty(pxf1)==1 || isempty(pxf2)==1 || isempty(pxi1)==1 || isempty(pxi2)==1;
rowa=100; rowb=0; cola=100; colb=0;
elseif pxf1-pxi1 <= pxf2-pxi2
rowa=row1(pxi1:pxf1);
cola=col1(pxi1:pxf1);
rowb=row2(pxi2:(pxi2+pxf1-pxi1));
colb=col2(pxi2:(pxi2+pxf1-pxi1));
else
rowa=row1(pxi1:(pxi1+pxf2-pxi2));
cola=col1(pxi1:(pxi1+pxf2-pxi2));
rowb=row2(pxi2:pxf2);
colb=col2(pxi2:pxf2);
end
%having trimmed the two "paralell" fringes to the same lenght find the distance between the two pieces
% (only if the paralell part is larger than min_len)
dist=0; distn=0;
if length(rowa)>= round(0.8*min_len*px_nm) && length(rowb)>= round(0.8*min_len*px_nm)
for k=1:length(rowa);
for l=1:length(rowb);
dist(l)=sqrt((rowa(k)-rowb(l))^2+(cola(k)-colb(l))^2);
end
distn(k)=min(dist);
end
distance=(distn);
%distance=mean(distn);
else
distance=0;
end
%If the distance between the two fringes is less than max allowed space then
%they are stacked
if sum(distance< (max_spa*px_nm))/numel(distance)==1 && mean(distance)>= min_spa*px_nm
%if distance<= max_spa*px_nm && distance>= min_spa*px_nm;
%spacing(i,j)=(distance)/px_nm;
spacing(i,j)=mean(distance);
%figure
%imshow(fc);
%hold on
scatter(cola,rowa,2,'g','filled','o')
scatter(colb,rowb,2,'g','filled','o')
%export_fig(['C:\Users\Maria\Documents\Postdoc UdeA\HRTEM images\Cristian Avila - Imagenes TEM\HRTEM\Spacing\',fileName,'_',num2str(spacing(i,j)),'.tif'],'-transparent');
% hold on
else
spacing(i,j)=0;
end
end
end
end
end
%close all
end
return