I have the attached binary image and I would like to find the top of the curved semi-continuous line/ignoring the vertical components which pass through sections of the line. (i.e. I would like the output to find have something like the magenta line in the binary2 image). Is there a simple way to do this? Any direction would be greatly appreciated. Thanks!
iterNum=20;
formvec=[];
valvec=[];
for q = 1:iterNum
% 1. fit using 2 random points
CC=[];
[zXBJ xXBJ]=size(binary);
b=1;
for i=1:zXBJ
for j=1:xXBJ
if XBJ(i,j)==1
CC(b,1)=j;
CC(b,2)=i;
b=b+1;
end
end
end
cornerL=length(CC(:,1));
rS=randi([1 cornerL],(floor(cornerL*0.05)),1);
rS2=[];
j=1;
for i=1:length(rS)
rS2(i,:)=CC(rS(j),:);
j=j+1;
end
p=polyfit(rS2(:,1),rS2(:,2),2); %CC(:,1)=z, 2=x
x1=1:xXBJ;
y1=polyval(p,x1);
valvec(end+1)=sum(improfile(XBJ,x1,y1));
formvec(q,:)=p;
end
[num loc]=max(valvec);
p=formvec(loc,:);
x1=1:xXBJ;
y1=polyval(p,x1);
figure
imshow(binary);
hold on
plot(rS2(:,1), rS2(:,2), '*', 'Color', 'c')
hold on
plot(x1,y1,'o','Color','r');
hold off