# Doug's MATLAB Video Tutorials

## Making a line visible over an image in MATLAB

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 Responses to “Making a line visible over an image in MATLAB”

1. Jonathan replied on :

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

2. Dan replied on :

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.

3. Doug replied on :

@Dan,

That is the great thing here. You have the code, give it a try!

Doug

4. Anand P replied on :

Thanks for the share, Doug

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

5. Doug replied on :

@Anand

Look like good changes to me.

Doug

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 3 + 7 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

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