Doug's MATLAB Video Tutorials

Making a line visible over an image in MATLAB 5

Posted by Doug Hull,

It can be difficult to see a line that is drawn over an image. The line is often lost in the background colors. That is why cursors are colored as they are, so that they are visible on any background. I demonstrate some code that makes a line more visible with the same technique.
function h = cursorLine(x,y,innerThickness, outerThickness)

if nargin == 2
    innerThickness = 2;
    outerThickness = 4;
end

h.thick = line(x,y);
h.thin  = line(x,y);

set(h.thick, 'color', [1 1 1]);
set(h.thin , 'color', [0 0 0]);

set(h.thick, 'linewidth', outerThickness);
set(h.thin , 'linewidth', innerThickness);

5 CommentsOldest to Newest

Doug,

This is a nice tip. Thank you. I especially like that you made the code available outside the video. Now I can see the tip at a glance.

~Jonathan

I think it would be more useful to have two lines sharing a common thickness (which the user could adjust) and use different lifestyles. One line would be solid, the other dashed.

Thanks for the share, Doug

Just did small tweaks and added some comments. Just wanted to have your suggestions too…

function cursorLine(x,y,innerThickness, outerThickness)
% Making a line visible over an image in MATLAB
% x : X axis co-ordinates of the points on the line
% y : Y axis co-ordinates of the points on the line
% innerThickness : Thickness of the black line (inner line)
% outerThickness : Thickness of the white line (outer line)

% Setting default values if no. of inputs are less than specified
if nargin == 2
innerThickness = 2;
outerThickness = 4;
else
if nargin innerThickness
h.thick = line(x,y);
set(h.thick, ‘color’, [1 1 1]);
set(h.thick, ‘linewidth’, outerThickness);
end

% Display black line only if inner line width is more than zero
if innerThickness > 0
h.thin = line(x,y);
set(h.thin , ‘color’, [0 0 0]);
set(h.thin , ‘linewidth’, innerThickness);
end
end

Add A Comment

What is 2 + 8?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.