title: How to draw a line between two coordinates of an image permanently in Matlab?
body: <p>I have a set of points that I want to connect sequentially. Suppose the points are (A1,A2,A3,...A9); I want to connect A1 to A2, A2 to A3 and so on and finally connect A9 to A1. </p>
  
  <p>All I need is to know a function that would help me connect A1 to A2, I could do the rest using for loops. </p>
  
  <p>I know connecting two points is a question that has been asked here several times before but I couldn't find the answer I required. Several of the solutions suggest using "plot" and "line" but these functions overlay the results on the image and don't actually make any changes to the original image.</p>
  
  <p>I did try them out and managed to save the resulting figure using the "saveas" and "print" functions but the image doesn't get saved in the proper format and there are a lot of problems using the parameters for these functions. Besides, I don't really want to save the image, it's just an unnecessary overhead I was willing to add if I could get the desired image with the lines. 
  I've also tried "imline" to draw lines but it seems to be interactive.</p>
  
  <p><a href="https://stackoverflow.com/questions/2464637/matlab-drawing-a-line-over-a-black-and-white-image"><strong>This</strong></a> particular question reflects my problem perfectly but when I ran the code snippets given as solutions, all of them gave a set of dots in the resulting image.</p>
  
  <p>I tried the above mentioned codes in the link with <a href="https://ibb.co/e8Egty" rel="nofollow noreferrer"><strong>this</strong></a> image that I found <a href="https://www5.cs.fau.de/research/data/image-manipulation/" rel="nofollow noreferrer">here</a>.</p>
  
  <p>A dotted line was an output for all three code snippets in the link above. 
  For example, I ran the first code like this:</p>
  
  <pre><code>I = imread('berries_copy.png'); 
  grayImage=rgb2gray(I); 
  img =false(size(grayImage,1), size(grayImage,2)); 
  </code></pre>
  
  <p>I wrote the above piece of code just to get a black image for the following operations: </p>
  
  <pre><code>x = [500 470];        % x coordinates 
  y = [300 310];        % y coordinates
  nPoints = max(abs(diff(x)), abs(diff(y)))+1;    % Number of points in line
  rIndex = round(linspace(y(1), y(2), nPoints));  % Row indices
  cIndex = round(linspace(x(1), x(2), nPoints));  % Column indices
  index = sub2ind(size(img), rIndex, cIndex);     % Linear indices
  img(index) = 255;  % Set the line points to white
  imshow(img);       % Display the image
  </code></pre>
  
  <p><a href="https://ibb.co/c7Oo6J" rel="nofollow noreferrer"><strong>This</strong></a> is the resulting image for the above code as well as the other two, which as you can see, is just a few points on a black background which isn't the desired output.</p>
  
  <p>I changed the code and used the "plot" function for the same to get <a href="https://ibb.co/mwOkLd" rel="nofollow noreferrer"><strong>this</strong></a> output which is what I want. Is there anyway I can change the dotted output into a solid line?</p>
  
  <p>Or if could anyone suggest a simple function or a method that would draw a line from A1 to A2 and would actually make a change in the input image, I'd be grateful. (I really hope this is just me being a novice rather than Matlab not having a simple function to draw a line in an image.)</p>
  
  <p>P.S. I don't have the Computer Vision toolbox and if possible, I'd like to find a solution that doesn't involve it.</p>