13
A
回答
3
This algorithm提供了一种方法。
7
你可能想在my answer寻找到一个SO质疑约adding a line to an image matrix。这里有一个类似的例子,一个我在这个问题的答案,这将使运行的行和列索引(10, 10)
到(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
而这里的结果图像:
5
如果您对其他方法的例外情况感到困扰,这里采用一种防弹方法可以产生一条线:
- 其像素总是线的整个长度期间彼此接触(像素是8邻居彼此),该行的
- 密度不依赖于附加参数但灵活地确定,以适应保从第一点。
输入(方便用于制造功能超出此代码):
img
- 基质含有图像,x1
,y1
,x2
,y2
- 结束点的坐标要绘制的线条。
代码:
% distances according to both axes
xn = abs(x2-x1);
yn = abs(y2-y1);
% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (xn > yn)
xc = x1 : sign(x2-x1) : x2;
yc = round(interp1([x1 x2], [y1 y2], xc, 'linear'));
else
yc = y1 : sign(y2-y1) : y2;
xc = round(interp1([y1 y2], [x1 x2], yc, 'linear'));
end
% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
ind = sub2ind(size(img), yc, xc);
% draw line on the image (change value of '255' to one that you need)
img(ind) = 255;
这里的三行上绘制的示例图像:
0
实际上它只是在plesiv的回答修改。我在图像上绘制了数千行,我需要提高性能。通过省略interp1
调用和使用整数变量所做的最大改进使其稍快。与我们的plesiv代码相比,它在我的电脑上执行速度提高了大约18%。
function img = drawLine(img, x1, y1, x2, y2)
x1=int16(x1); x2=int16(x2); y1=int16(y1); y2=int16(y2);
% distances according to both axes
xn = double(x2-x1);
yn = double(y2-y1);
% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (abs(xn) > abs(yn))
xc = x1 : sign(xn) : x2;
if yn==0
yc = y1+zeros(1, abs(xn)+1, 'int16');
else
yc = int16(double(y1):abs(yn/xn)*sign(yn):double(y2));
end
else
yc = y1 : sign(yn) : y2;
if xn==0
xc = x1+zeros(1, abs(yn)+1, 'int16');
else
xc = int16(double(x1):abs(xn/yn)*sign(xn):double(x2));
end
end
% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
ind = sub2ind(size(img), yc, xc);
% draw line on the image (change value of '255' to one that you need)
img(ind) = 255;
end
0
相关问题
- 1. 黑白图像在Matlab中的颜色
- 2. 黑白图像直方图(MatLab)
- 3. Heroku黑白图像
- 4. 了解画布如何将图像转换为黑白图像
- 5. OpenCV图像黑白图形
- 6. 在matlab中连接黑色图像中的白色点
- 7. 查找黑白图像
- 8. 缩放黑白图像
- 9. 黑白图像处理
- 10. Python图像库黑白两色图像
- 11. 在画面黑线
- 12. MATLAB:黑白打印pdf
- 13. 在C中制作黑白图像#
- 14. MATLAB环抱在黑色图像
- 15. HTML画布:平滑着色黑白图像
- 16. 在matlab中确定黑色像素为白色
- 17. 随机黑白画布?
- 18. 确定图像是黑白图像还是彩色图像
- 19. 在Tensorflow中将RGBA图像转换为黑白图像
- 20. 为什么黑白图像在图像处理中会颠倒?
- 21. 如何在PHP中将图像转换为黑白图像
- 22. 如何在Matlab中的图像上画一条线?
- 23. .NET Drawing.Graphics.FromImage()返回空白黑色图像
- 24. C#转换图像来完成黑白
- 25. 将粒子分割成黑白图像
- 26. 使图像变成黑色和白色?
- 27. 制作黑白图像的Javascript
- 28. 使图像变成黑白悬停
- 29. 将JPEG图像转换为黑白
- 30. 将网页图像更改为黑白
这对于一条对角线来说非常合适,但可能会为不平的线条添加不需要的像素。如果你不关心额外的像素,我建议选择gnovices解决方案,因为它快速简单。 – Jonas 2010-03-17 19:44:14
@Jonas:我已经更新了算法来更好地计算线条,从而消除了一些不必要的像素。 – gnovice 2017-05-02 04:10:56
谢谢你改进我的答案! – Jonas 2017-05-02 07:22:45