Home How to draw a line between two coordinates of an image permanently in Matlab?
Reply: 0

How to draw a line between two coordinates of an image permanently in Matlab?

user3365 Published in May 27, 2018, 3:36 am

This question already has an answer here:

  • MATLAB: Drawing a line over a black and white image 5 answers

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.

All I need is to know a function that would help me connect A1 to A2, I could do the rest using for loops.

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.

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.

This 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.

I tried the above mentioned codes in the link with this image that I found here.

A dotted line was an output for all three code snippets in the link above. For example, I ran the first code like this:

I = imread('berries_copy.png'); 
img =false(size(grayImage,1), size(grayImage,2)); 

I wrote the above piece of code just to get a black image for the following operations:

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

This 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.

I changed the code and used the "plot" function for the same to get this output which is what I want. Is there anyway I can change the dotted output into a solid line?

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.S. I don't have the Computer Vision toolbox and if possible, I'd like to find a solution that doesn't involve it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317077 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO