Benas
Benas

Reputation: 2332

How to draw a line on image in Matlab?

I want to draw a line on picture, but it only draws line at 45 angle. Line seems to be reacting only at x coordinate change.

function demoOnImageClick
clc;clear;close all;

imObj = rand(500)
figure;
hAxes = axes();
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);

function ImageClickCallback ( objectHandle , eventData )
axesHandle  = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint'); 
coordinates = coordinates(1,1:2);
line([0 coordinates (1)], [0 coordinates (2)]);
message     = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
helpdlg(message);
end

end

Upvotes: 0

Views: 951

Answers (1)

Removing the spaces between coordinates and the indices seems to do the trick.

line([0 coordinates(1)], [0 coordinates(2)]);

I believe with your code, MATLAB is drawing two lines: the first one from (0,0) to (coordinates,coordinates) where it's using the first x-value only, and then the second line from (coordinates,coordinates) to (1,2).

Hope this helps!

Upvotes: 2

Related Questions