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