[英]How to draw a line on an image in matlab?
我有兩點可以說:
現在我想在這兩點之間划一條線......並且線應該出現在圖像上意味着應該是可見的。
這該怎么做????
在線條上繪制線條的最簡單方法是使用PLOT 。
%# read and display image
img = imread('autumn.tif');
figure,imshow(img)
%# make sure the image doesn't disappear if we plot something else
hold on
%# define points (in matrix coordinates)
p1 = [10,100];
p2 = [100,20];
%# plot the points.
%# Note that depending on the definition of the points,
%# you may have to swap x and y
plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','r','LineWidth',2)
如果你想要一個不同的顏色,要么將字母改為任何rgbcmykw
,要么使用RGB三元組(紅色是[1 0 0]
)。 查看lineseries屬性以獲取更多格式選項。
從版本R2014a開始,您可以使用insertShape,如下所示:
img = insertShape(img,'Line',[x1 y1 x2 y2],'LineWidth',2,'Color','blue');
您也可以使用相同的命令繪制多行,但x1,x2,y2,y3必須是列向量,每行代表一個新行。
insertShape還允許您繪制矩形,圓形和多邊形。
像這樣:
figure;
hold on;
imagesc(img);
line([x1,x2],[y1,y2],'Color','r','LineWidth',2)
hold off
其中y是“向下”方向,x是圖像中的“向右”方向。 根據需要更改顏色和寬度以使其可見。
如果您有計算機視覺工具箱。 您可以簡單地使用shapeInserter。
查看http://www.mathworks.com/help/vision/ref/vision.shapeinserter-class.html
要指定行,您必須使用下面的行。 否則,您可能會得到一個矩形
例:
%draw a line from point (100,100) to (200,200) on an image saved as nextFrame
line = int32([100 100 200 200]);
shapeInserter = vision.ShapeInserter('Shape', 'Lines');
nextFrame = step(shapeInserter, nextFrame, line);
查看屬性以查看可以編輯的內容。
load clown
image(X)
colormap(map)
c = size(X,2)
mid = round(c/2)
X(:,mid) = 1
image(X)
您可以使用訪問Steve在圖像處理上的技術,下載並使用hline和vline以及hold on
。 或者只是使用他的技術。 無論哪種方式都有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.