2

I have a mask of size mxn.

I would like to add a line to this mask, such that all the points which passes through it will be set to true.

The line is defined by two points: (x1,y1),(x2,y2).

What is the best way to achieve this result?

Please notice that I only have the Image processing toolbox.

Example for possible input, and desired output:

%generates a mask
m = 152; n=131; 
mask = false(m,n);
%example for possible input points
y1 = 68; x1 = 69;
y2 = 28; x2 = 75;

% code for adding the line into the mask%

imshow(mask);

desired result:

enter image description here

Thanks!

ibezito
  • 5,782
  • 2
  • 22
  • 46

1 Answers1

3

We can first determine how many pixels there are between the two points by computing the distance (in pixels) between the points. We can then use linspace to create a linear spacing of points between the two end points specifying this number of points. We can then round the result to get pixel coordinates.

Then we can use sub2ind to set these values within the mask to 1.

% Distance (in pixels) between the two endpoints
nPoints = ceil(sqrt((x2 - x1).^2 + (y2 - y1).^2)) + 1;

% Determine x and y locations along the line
xvalues = round(linspace(x1, x2, nPoints));
yvalues = round(linspace(y1, y2, nPoints));

% Replace the relevant values within the mask
mask(sub2ind(size(mask), yvalues, xvalues)) = 1;

enter image description here

Suever
  • 64,497
  • 14
  • 82
  • 101