Line of controlled thickness in image

Hey!
I have a grayscale image (I) and I would like to plot a line with a controlled thickness in it.
I know the coordinates of the line x=[x0,xf], y=[y0,yf], and lets say the thickness I want is "t", in pixels.
I can do
imshow (I)
line(x,y, 'LineWidth', linewidthvalue)
But the line here has a width that depends on how much I zoom in in the image, since in 'LineWidth' the width is defined by 1/72 inches, not in pixels. Is there any way to change that, namely, to fix the width of the line to a certain number of pixels?
Thanks in advance :)

 Accepted Answer

This is one way to draw a line on the image itself.
% it's assumed that this is a color image
% if it's not, it needs to either be mxnx3
% or you'll have to get rid of the line color
A = repmat(imresize(imread('cameraman.tif'),[700 700]),[1 1 3]);
% parameters
thisline = [50 550; 250 300; 545 640; 431 691; 545 681];
linecolor = [0 255 255];
linewidth = 10;
% construct the line mask
h = imshow(A);
h.Visible = 'off';
L = images.roi.Polyline(gca);
L.Position = thisline;
mask = imdilate(createMask(L),strel('disk',floor(linewidth/2)));
% apply the mask
A = uint8(double(A).*~mask + permute(linecolor,[1 3 2]).*mask); % assuming A is uint8
% then just save the image or do whatever
imshow(A)
If you have Computer Vision Toolbox, there is also insertshape().

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Tags

Asked:

on 29 Sep 2021

Answered:

DGM
on 29 Sep 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!