Search code examples
imagematlabplotmatlab-guideaxes

Retaining a Plot on a Image drawn using impoint function


I have a number of images to be marked with certain fixed number of points. (object tracking) I am using impoint to make the points on the image and then plot to connect them to each other in a logical way. So far so good. Next, I hit next (on my MATLAB GUI developed using GUIDE) and the new image loads in the axis and the information is lost (the point).

  • How can I retain the plotted impoint for my next image?
  • How can I make sure that when I move the impoint, the plotted lines also move?

What I still have to work around is:

That I am already using the addNewPositionCallback for the updation of the position of the point. This callback when written: addNewPositionCallback(H, @fnc); it issues a function call something like fnc(pos)

My problem is that this call back does not provide a handle.

Hence without the handles information I can not address the right element in the Code. As of now I have implemented it so, that I have written 18 One-liner functions for the fixed number of 18 points that I need to mark on the image. these functions call the same function updatestructure(position, 'point_name', handles) which is called right after the creation of impoints to plot the link between them on the image. But here too I have no information on handles. Here is what I tried:

pointname = impoint(gca,[]);
setcolor(h,'y');
position = getPosition(h);
updatestructure(position, 'pointname', handles);
addNewPositionCallback(h, @movepointname)

So when update structure is called the plot works fine. I can see what I want. But when I move the point function movepointname() is called and thats when problem starts.

function movepointname (smart_pos)
updateStruct (smart_pos, 'pointname');
end

This surely generated an error as the handles information was not transferred. I tried vargin to define what it should do when less argument call is set. In that I would still need handles from somewhere.

Thank you.


Solution

  • Use handles when you display and update your images and plots.

    For example, when you display image first time (on any axes):

     hImg = image(ImageMatrix);
    

    The to update it do not issue image or imagesc or other function, but update it directly instead:

    set(hImg, 'CData',NewImageMatrix');
    

    In this case everything else on your axes will remain intact.

    You can do the same trick with your points. First time you plot like this:

    hPlot = plot( PointsX, PointsY, '*-' ); % to plot * connected with line
    

    Then you do

    set(hPlot, 'XData',NewPointsX, 'YData',NewPointsY );
    

    Note that number of actual points when you update your plot does not need to be the same: you can first plot 3 points and then update the plot handle to show 33 points.

    Also in the initialization function of your GUI you can create empty plot with

    hPlot = plot( [], [], '*-' );
    

    to plot actually nothing, but the handle will be available for further updates. Same thing should work with images as well, but you may have issues with axes limits update when you update the image to another one with different number of rows/columns. To solve this you can update the image like this:

    set(hImg, 'CData',NewImageMatrix', 'XData',1:size(NewImageMatrix,2), 'YData',1:size(NewImageMatrix,1) );
    

    EDIT: Ok, above will solve your first problem. To update your lines when you move impoint, you have to look for the impoint reference and more specifically the addNewPositionCallback function. Then you have to define call-back function that updates lines (it could be even a normal plot handle like the hPlot above) with new coordinates.