MATLAB:在黑白图像上画一条线 [英] MATLAB: Drawing a line over a black and white image
本文介绍了MATLAB:在黑白图像上画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果已知起点和终点坐标,在MATLAB中在黑白(二进制)图像上绘制线条的最佳方法是什么?
请注意,我不是想添加注释行。我希望这条线成为图像的一部分。
解决方案
你可能想看看
What is the best way to draw a line over a black and white (binary) image in MATLAB, provided the start and end coordinates are known?
Please note, I am not trying to add an annotation line. I would like the line to become part of the image.
解决方案
You may want to look at my answer to an SO question about adding a line to an image matrix. Here's a similar example to the one I have in that answer, which will make a white line running from row and column index (10, 10)
to (240, 120)
:
img = imread('cameraman.tif'); % Load a sample black and white image
x = [10 240]; % x coordinates
y = [10 120]; % 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
And here's the resulting image:
这篇关于MATLAB:在黑白图像上画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文