How to Insert a Dotted Line using InsertShape?

13 次查看(过去 30 天)
Hello. I'm trying to insert a dotted line onto my figure (A) but struggling with the line spec. Here is my line of code. Normally I'll put 'k--' or 'LineSpec','--' but error says "'LineSpec' is not a recognized parameter". Please can you help me?? Thank you, Sally
A = insertShape(A,'line',[1 168.5 301 168.5],'Color','black','LineWidth',6)

回答(2 个)

Abhisek Pradhan
Abhisek Pradhan 2019-7-16
The insertShape MATLAB function doesn’t have the functionality to insert a dotted line over a picture.
You can try a workaround mentioned below.
imshow(A);
hold on;
line([1,301],[168.5,168.5],'Color','r','LineStyle','--','LineWidth',6);
Refer line and insertShape for more information.

Wei Huang
Wei Huang 2023-11-8
编辑:Wei Huang 2023-11-8
Here is a way you can do this by interpolating between the points of your solid line:
% Create some image
I = uint8(zeros(400,400));
% Points defining solid line
solidLine = [1 168.5 301 168.5];
% Interpolating solid line to with n number of points
n = 20;
dashLine = [linspace(solidLine(1),solidLine(3),n);...
linspace(solidLine(2),solidLine(4),n)];
% Account for odd number of interpolated points
if mod(n,2)
n = n-1;
dashLine = dashLine(:,1:end-1);
end
% Reshape dash line for insertShape
dashLine = reshape(dashLine,4,n/2)';
I = insertShape(I,"line",dashLine,'Color','r','LineWidth',6);
imshow(I);

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by