function Point = find_EdgePoint(I_edge,Finderpoints,order)
vector3_1 = Finderpoints(find(order==1),:)-Finderpoints(find(order==3),:);
vector2_3 = Finderpoints(find(order==3),:)-Finderpoints(find(order==2),:);
vector1_2 = Finderpoints(find(order==2),:)-Finderpoints(find(order==1),:);
vector3_1 = vector3_1./(norm(vector3_1));
vector2_3 = vector2_3./(norm(vector2_3));
vector1_2 = vector1_2./(norm(vector1_2));
%% point 1
pointX = Finderpoints(find(order==1),1);
pointY = Finderpoints(find(order==1),2);
X_31 = pointX;
Y_31 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_31,X_31) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_31,pointY):max(Y_31,pointY),min(X_31,pointX):max(X_31,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_31 = pointX;
Y_31 = pointY;
pointX = round( Finderpoints(find(order==1),1) + k * vector3_1(1));
pointY = round( Finderpoints(find(order==1),2) + k * vector3_1(2));
k = k + 1;
end
X_31 = pointX;
Y_31 = pointY;
scatter(X_31,Y_31,'g*');hold on
pointX = Finderpoints(find(order==1),1);
pointY = Finderpoints(find(order==1),2);
X_21 = pointX;
Y_21 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_21,X_21) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_21,pointY):max(Y_21,pointY),min(X_21,pointX):max(X_21,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_21 = pointX;
Y_21 = pointY;
pointX = round(Finderpoints(find(order==1),1) - k * vector1_2(1));
pointY = round(Finderpoints(find(order==1),2) - k * vector1_2(2));
k = k+1;
end
X_21 = pointX;
Y_21 = pointY;
scatter(X_21,Y_21,'g*');hold on
%% point 2
pointX = Finderpoints(find(order==2),1);
pointY = Finderpoints(find(order==2),2);
X_32 = pointX;
Y_32 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_32,X_32) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_32,pointY):max(Y_32,pointY),min(X_32,pointX):max(X_32,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_32 = pointX;
Y_32 = pointY;
pointX = round(Finderpoints(find(order==2),1) - k * vector2_3(1));
pointY = round(Finderpoints(find(order==2),2) - k * vector2_3(2));
k = k + 1;
end
X_32 = pointX;
Y_32 = pointY;
scatter(X_32,Y_32,'g*');hold on
pointX = Finderpoints(find(order==2),1);
pointY = Finderpoints(find(order==2),2);
X_12 = pointX;
Y_12 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_12,X_12) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_12,pointY):max(Y_12,pointY),min(X_12,pointX):max(X_12,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_12 = pointX;
Y_12 = pointY;
pointX = round(Finderpoints(find(order==2),1) + k * vector1_2(1));
pointY = round(Finderpoints(find(order==2),2) + k * vector1_2(2));
k = k+1;
end
X_12 = pointX;
Y_12 = pointY;
scatter(X_12,Y_12,'g*');hold on
%% point 3
pointX = Finderpoints(find(order==3),1);
pointY = Finderpoints(find(order==3),2);
X_13 = pointX;
Y_13 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_13,X_13) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_13,pointY):max(Y_13,pointY),min(X_13,pointX):max(X_13,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_13 = pointX;
Y_13 = pointY;
pointX = round(Finderpoints(find(order==3),1) - k * vector3_1(1));
pointY = round(Finderpoints(find(order==3),2) - k * vector3_1(2));
k = k + 1;
end
X_13 = pointX;
Y_13 = pointY;
scatter(X_13,Y_13,'g*');hold on
pointX = Finderpoints(find(order==3),1);
pointY = Finderpoints(find(order==3),2);
X_23 = pointX;
Y_23 = pointY;
count = 0;
k = 1;
while count<3 && pointX>=1 && pointY>=1 && pointX<= size(I_edge,2) && pointY<= size(I_edge,1)
if I_edge(Y_23,X_23) == 0
if I_edge(pointY,pointX) >= 1
count = count + 1;
else
patch = I_edge(min(Y_23,pointY):max(Y_23,pointY),min(X_23,pointX):max(X_23,pointX));
[patchx,patchy] = size(patch);
if patchx==2 && patchy==2
S = sum(patch,1);
if S(1)>=1 && S(2)>=1
count = count + 1;
end
end
end
end
X_23 = pointX;
Y_23 = pointY;
pointX = round(Finderpoints(find(order==3),1) + k * vector2_3(1));
pointY = round(Finderpoints(find(order==3),2) + k * vector2_3(2));
k = k + 1;
end
X_23 = pointX;
Y_23 = pointY;
scatter(X_23,Y_23,'g*');hold on
%% Edge point
[m,n] = size(I_edge);
point3 = [X_23,Y_23];
scatter(point3(1),point3(2),'ys');hold on
point2 = [X_32,Y_32];
scatter(point2(1),point2(2),'ys');hold on
tic
point1 = find_intersect([X_32,Y_32],[X_31,Y_31],[X_23,Y_23],[X_21,Y_21],m,n); % direction is significant
toc
if isempty(point1)
error('Cannot Find Edge Point, Please try again');
end
scatter(point1(1),point1(2),'ys');hold on
tic
point4 = find_intersect([X_32,Y_32],[X_12,Y_12],[X_23,Y_23],[X_13,Y_13],m,n);
toc
if isempty(point4)
error('Cannot Find Edge Point, Please try again');
end
scatter(point4(1),point4(2),'ys');hold on
%%
Point = [point1;point2;point3;point4];
end