image processing - Matlab - create a line mask -


i have mask of size mxn.

i add line mask, such points passes through set true.

the line defined 2 points: (x1,y1),(x2,y2).

what best way achieve result?

please notice have image processing toolbox.

example possible input, , desired output:

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

desired result:

enter image description here

thanks!

we can first determine how many pixels there between 2 points computing distance (in pixels) between points. can use linspace create linear spacing of points between 2 end points specifying number of points. can round result pixel coordinates.

then can use sub2ind set these values within mask 1.

% distance (in pixels) between 2 endpoints npoints = ceil(sqrt((x2 - x1).^2 + (y2 - y1).^2)) + 1;  % determine x , y locations along line xvalues = round(linspace(x1, x2, npoints)); yvalues = round(linspace(y1, y2, npoints));  % replace relevant values within mask mask(sub2ind(size(mask), yvalues, xvalues)) = 1; 

enter image description here


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -