views:

358

answers:

1

I have a strange problem that the plot overlaps in the graph, but not in the image in the same axis.

I am sure I didn't leave hold on somewhere or else it will overlap in the image itself as well.

EDIT : I want to get rid of the blue overlapped lines, I only want one blue line to be present in that pic.

Here is a sample :

(NOTE : The black image is a RGB image but I didn't plot that atm, so it IS meant to be a transition from black to white on the graph. )

alt text

Some part of the code :

   for K=1:23

    hold on
    I = fig.img.(['p' num2str(K)]);
    bw=(I);
    imshow(bw)

    ss = bwlabel(bw);
    s = regionprops(ss,'centroid');


    centroids{K} = cat(1,s.Centroid);
    hold(imgca,'on')
    plot(imgca,centroids{K}(:,1), centroids{K}(:,2), 'r*'); hold on;
    x=centroids{K}(:,1);
    y=centroids{K}(:,2);
    points=plot(x,y,'go',x,y,'rx');

    hold on
    axis on
    axis fill
    ccentroids = cat(1,centroids{:});
    C1=ccentroids(:,1);
    C2=ccentroids(:,2);
    set(points,'XData',C1,'YData',C2);

    .
    .  
    .

    p= polyfit(x2,y2,2)
    parabola_x = linspace(-250,640,500);
    parabola_polyval = polyval(p,parabola_x);
    plot(parabola_x,parabola_polyval,'b-');
    .
    .  
    .
    end

Any ideas?

+2  A: 

The reason you have multiple blue lines is because you plot one for every pass through your loop with the line:

plot(parabola_x,parabola_polyval,'b-');

In fact, you are plotting everything (the images, points, and lines) over and over again in your loop without clearing the old ones.

You should instead initialize plot objects outside of your for loop and use the SET command to update them within the loop, instead of just replotting them. I gave one example of this in this answer to a question you previously asked where I discuss using the handles to plot objects to modify them. For the sample code you give here, you could do something like this:

hImage = imshow(bw(fig.img.p1));  %# Initialize the image
hold on;                          %# Add to the existing plot
hStar = plot(nan,nan,'r*');       %# Initialize the red star
hPoints = plot(nan,nan,'go',...   %# Initialize the other points
               nan,nan,'rx');
hLine = plot(nan,nan,'b-');       %# Initialize the blue line

for K = 1:23

  I = fig.img.(['p' num2str(K)]);
  bw = (I);
  set(hImage,'CData',bw);  %# Update the image

  ss = bwlabel(bw);
  s = regionprops(ss,'centroid');
  centroids{K} = cat(1,s.Centroid);
  set(hStar,'XData',centroids{K}(:,1),...  %# Update the red star
            'YData',centroids{K}(:,2));
  ccentroids = cat(1,centroids{:});
  C1 = ccentroids(:,1);
  C2 = ccentroids(:,2);
  set(hPoints,'XData',C1,'YData',C2);  %# Update the other points

  ...

  p = polyfit(x2,y2,2);
  parabola_x = linspace(-250,640,500);
  parabola_polyval = polyval(p,parabola_x);
  set(hLine,'XData',parabola_x,...      %# Update the blue line
            'YData',parabola_polyval);

  ...

end
gnovice
Is there a way I can initialize it without plotting it at first ? the values made in polyval keep changing, so I need to make sure the plotting wont actually be viewed until the second loop starts
ZaZu
@ZaZu: I updated my answer with a sample of how you can initialize things first. To initialize a plot object that I don't want to show up at first, I usually just make the x and y values `NaN`, which still creates the object for you to update later, but it doesn't render it in the image.
gnovice
Thanks alot for the reply, yeah I forgot to save the edition with setting, but I didnt even think that overlapping lines could be from that ... Thanks for the additions gnovice, I have updated my setting line and it works now ... The problem is it appears under the bw image now ... why is that ?
ZaZu
@ZaZu: I'm not sure why it's doing that, but you can try adding this to the input arguments to SET for updating the line: `,'ZData',ones(size(parabola_x))`
gnovice
You're a genius that worked !! thanks !! This is converting the Z-axis into the "ONES" of parabola's x values right ?
ZaZu
@ZaZu: It creates a vector the same size as `parabola_x` filled with ones, then sets that as the z data for the line, which bumps it a little higher than the image (which sits at a z height of 0).
gnovice
Thanks alot for the explanation :)
ZaZu

related questions