0

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

enter image description here

enter image description here

user3470496
  • 141
  • 7
  • 33
  • 1
    use RANSAC to fit a curve. – Shai Jan 18 '16 at 09:01
  • I'm not familiar with RANSAC, I downloaded the Ke Yan version from MATLAB central but was only able to generate a straight line which didn't really seem to fit my data. Is RANSAC very different from polyfit? – user3470496 Jan 18 '16 at 14:47
  • RANSAC is a "concept" algorithm, designed to fit a model to noisy data. It is searching for the meaningful points and discard the noise. Read [here](https://en.wikipedia.org/wiki/RANSAC) more about it. – Shai Jan 18 '16 at 15:00
  • Ok, I see how its different from polyfit-definitely better for my purposes, is there code out there/an easy way to apply this and get a curve as an output? I've been googling around and plugging away but am getting nowhere (apologies for my naivety and thanks loads for the help) – user3470496 Jan 18 '16 at 16:18
  • I don't suppose there's an "off the shelf" RANSAC for fitting general curves. You can use the wiki page as a guideline and write your own, – Shai Jan 18 '16 at 16:29
  • Say theoretically I have no idea how to do this...? It looks like Ransac connects two points randomly, counts inliers vs. outliers, repeats and goes with the line with the most inliers. I guess I could subselect some random group of total points, find a best fit curve through polyfit, then find how many inliers there are when that curve is place back on the original set of points? – user3470496 Jan 18 '16 at 17:20
  • this is the way it works – Shai Jan 18 '16 at 17:35
  • Put something together (attached to main thread), takes forever and results are just okay. There's gotta be something better out there.. – user3470496 Jan 18 '16 at 18:58

0 Answers0