0

I have a question concerning my GUI that has bothered me for some hours now and I cannot get it fixed. The function: I load an image, subset it and plot it as as surf plot. Afterwards I start a GUI, which lets me choose upper left and lower right coordinates of rectangles. The rectangles get drawn onto the surf-plot.

I have two issues: I want to indicate the mouse-clicks by little red patches, the rectangles by red lines.

1) The little red patches work fine, except the first on is really big and fills almost the whole plot-window. As soon as I choose the second point for the first coordinate, everything goes back to normal and the patches plot in the small manner I want them to. I debugged the code to see if somehting is wrong with the coordinates, but they seem to be ok!

2) The drawing of the lines is inaccurate! Especially the first few lines are offset of the mousclick by up to 100 pixels, usually to the left. Sometimes they even move around when adding the next lines. After placing a few rectangles in that way they usually get better and are in place. Any idea why this is? Here is the code:

function resampdem
clc;clear;clear all

%% read DEMs and header
img1 = imread('srtm_55_06.tif');

% subset
img1 = img1(1:500,1:500);
[x y] = meshgrid(1:500,1:500);



%%
f = figure;
imageHandle = surfl(x,y,img1);
colormap jet
shading interp
view(0,90);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback)
hold on

a = axes;
set(a, 'Visible', 'off');


%# Create controls.
uicontrol('Parent', f, 'Style', 'edit', 'String', 'Input...');

m = 1;
bin = [];



%%%  Funktion zur Auswertung des Mouseklicks
helpdlg('Corner: Upper Left');
    function ImageClickCallback ( objectHandle , eventData )

%         if mod(m,2) == 0
%             string = 'Upper Left';
%         else
%             string = 'Lower Right';
%             
%         end
%         message = sprintf('Corner: %s',string);
%         helpdlg(message);

        axesHandle  = get(objectHandle,'Parent');
        coordinates = get(axesHandle,'CurrentPoint');
        coordinates = coordinates(1,1:2);


        bin(m,1) = coordinates(1)

        bin(m,2) = coordinates(2)




        patch([bin(m,1)-3 bin(m,1)+3 bin(m,1)+3 bin(m,1)-3], [bin(m,2)+3 bin(m,2)+3 bin(m,2)-3 bin(m,2)-3],'r','Parent',a);


        if mod(size(bin,1),2) == 0
            resamp_area(bin,m);
        end
        m = m+1
    end

%%%  Funktion zum Zeichnen der Rechtecke
    function resamp_area(coords,m)

        x1 = coords(m-1,1);
        x2 = coords(m,1);
        y1 = coords(m-1,2);
        y2 = coords(m,2);

        patch([x1 x1+20 x1+20 x1], [y1 y1 y1-20 y1-20],'w','Parent',a);
        %horizontal lines
        line([x1, x2], [y1, y1], 'Parent', a, 'Color',[1 0 0], 'LineWidth',2.0);
        line([x1, x2], [y2, y2], 'Parent', a, 'Color',[1 0 0], 'LineWidth',2.0);
        %vertical lines
        line([x1, x1], [y1, y2], 'Parent', a, 'Color',[1 0 0], 'LineWidth',2.0);
        line([x2, x2], [y1, y2], 'Parent', a, 'Color',[1 0 0], 'LineWidth',2.0);
        %get(t)

    end    

end
TheodorBecker
  • 249
  • 1
  • 17
  • I haven't tested this, but a quick search turned up [this](http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/25693) which suggests that your line `coordinates = coordinates(1, 1:2)` may be retrieving a point in the camera plane - which is presumably not what you need. – wakjah Apr 17 '13 at 09:28
  • thanks for pointing this out. But why do most other people not have those issues when it comes to reading coordinates and plotting on a GUI? I changed the graphics from imageHandle = surfl(x,y,img1); to imageHandle = imshow(new_sub,[min(min(new_sub)) max(max(new_sub))]); which should be a 2D plane, therefore camera point and screen point should be the same, but the lines are still offset from where I click... – TheodorBecker Apr 17 '13 at 10:05
  • You could try using the **figure**'s `CurrentPoint` property, which is just the `[x, y]` position in the figure, then offsetting this by the left/top values in the `Position` property of the axes. This would sidestep all the problems people seem to have using an axes' `CurrentPoint`. – wakjah Apr 17 '13 at 12:07

0 Answers0