-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mesh.m
127 lines (127 loc) · 5.05 KB
/
create_mesh.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
function Meshes=create_mesh(images)
SS = get(0,'screensize');
SS(3) = round(SS(3)*0.48);
SS(4) = round(SS(4)*0.89);
img_path=images.a;
img_THz=images.b;
waitfor(msgbox(sprintf('INSTRUCTIONS:\n1. LEFT CLICK TO SELECT A POINT\n2. RIGHT CLICK TO DELETE THE LAST POINT\n3. PRESS ENTER TO FINISH\nPLEASE CONSIDER THAT YOU CAN ONLY DELETE THE LAST CREATED POINT')))
img_path_ref=img_path; %Reference when deleting
img_THz_ref=img_THz; %Reference when deleting
TH=1;
op=0;
i=1;
num1=round(size(img_THz,1)*0.005);
num2=round(size(img_path,1)*0.005);
while(op==0)
if(TH==1)
figure(1)
set(gcf,'position',[0 45 SS(4) SS(3)]);
else
figure(2)
set(gcf,'position',[SS(4) 45 SS(4) SS(3)]);
end
[a,b,button]=ginput(1);
if (isempty(a) && TH==1) %Verifies if user pressed 'Enter' to stop
op=1;
else
if(button==3) %Delete last point - press mouse - right button
if(TH==1)
i=i-1;
figure(2)
TH=0;
else
figure(1)
TH=1;
end
[a,b,button]=ginput(1);
if (TH==1)
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,1)=img_THz_ref(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,1);
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,2)=img_THz_ref(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,2);
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,3)=img_THz_ref(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,3);
else
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,1)=img_path_ref(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,1);
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,2)=img_path_ref(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,2);
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,3)=img_path_ref(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,3);
end
end
if (TH==1)
x_THz(i)=round(a);
y_THz(i)=round(b);
%Colors the selected point
z1=size(img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1));
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,1)=zeros(z1);
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,2)=zeros(z1);
img_THz(y_THz(i)-num1:y_THz(i)+num1,x_THz(i)-num1:x_THz(i)+num1,3)=zeros(z1);
imshow(img_THz);
%Position the window
set(gcf,'position',[0 45 SS(4) SS(3)]);
TH=0;
else
x_path(i)=round(a);
y_path(i)=round(b);
%Colors the selected point
z=size(img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,1));
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,1)=zeros(z);
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,2)=zeros(z);
img_path(y_path(i)-num2:y_path(i)+num2,x_path(i)-num2:x_path(i)+num2,3)=zeros(z);
imshow(img_path);
%Position the window
set(gcf,'position',[SS(4) 45 SS(4) SS(3)]);
i=i+1;
TH=1;
end
end
end
%CREATE AUTOMATIC BOUNDARIES
bound='';
while isempty(bound)
bound = questdlg('WOULD YOU LIKE TO CREATE AUTOMATIC BOUNDARIES?', ...
'BOUNDARIES', ...
'YES','NO','NO');
end
if(strcmp(bound,'YES'))
x_THz=[x_THz,1+num1,1+num1,size(img_THz,2)-num1,size(img_THz,2)-num1];
y_THz=[y_THz,1+num1,size(img_THz,1)-num1,size(img_THz,1)-num1,1+num1];
x_path=[x_path,1+num2,1+num2,size(img_path,2)-num2,size(img_path,2)-num2];
y_path=[y_path,1+num2,size(img_path,1)-num2,size(img_path,1)-num2,1+num2];
% A=image_boundaries_THz(img_THz,x_THz,y_THz);
% x_THz=A(1,:);
% y_THz=A(2,:);
% B=image_boundaries_path(img_path,x_path,y_path);
% x_path=B(1,:);
% y_path=B(2,:);
end
figure(1)
Mesh_THz = delaunayTriangulation(x_THz',y_THz');
Mesh_THz = Mesh_THz(:,:);
hold on, triplot(Mesh_THz,x_THz,y_THz,'Color', [0.4 0.4 0.4],'linewidth',3), hold off
figure(2)
Mesh_Path = Mesh_THz(:,:);
hold on, triplot(Mesh_Path,x_path,y_path,'Color', [0.4 0.4 0.4],'linewidth',3), hold off
%check for overlap
pol_tot = polyshape(x_path(Mesh_Path(1,:)),y_path(Mesh_Path(1,:)));
i = 2;
int_ind = 0;
while i <= size(Mesh_Path,1)
pol_add = polyshape(x_path(Mesh_Path(i,:)),y_path(Mesh_Path(i,:)));
int = intersect(pol_tot,pol_add);
if int.NumRegions == 0
pol_tot = union(pol_tot,pol_add);
i = i + 1;
else
int_ind = 1;
break;
end
end
if int_ind == 1
waitfor(msgbox('MESH OVERLAP'));
end
%save and return structure
Meshes=struct;
Meshes.a=x_THz;
Meshes.b=y_THz;
Meshes.c=Mesh_THz;
Meshes.d=x_path;
Meshes.e=y_path;
Meshes.f=Mesh_Path;
end