Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image MATLAB:在黑白图像上画一条线_Image_Matlab_Image Processing_Line_Matlab Cvst - Fatal编程技术网

Image MATLAB:在黑白图像上画一条线

Image MATLAB:在黑白图像上画一条线,image,matlab,image-processing,line,matlab-cvst,Image,Matlab,Image Processing,Line,Matlab Cvst,在MATLAB中,如果起点和终点坐标已知,在黑白(二进制)图像上绘制直线的最佳方法是什么 请注意,我不想添加注释行。我希望该行成为图像的一部分。提供了一种方法。您可能想看看有关的SO问题。这里有一个与我在该答案中的示例类似的示例,它将在行和列索引(10,10)到(240,120)之间画一条白线: 这是最终的图像: 如果您被其他方法的例外情况所困扰,这里有一种防弹方法,它会产生一行: 其像素在整个线条长度内始终相互接触(像素相互之间为8个相邻像素) 线路密度不取决于附加参数,而是灵活确定,以适应

在MATLAB中,如果起点和终点坐标已知,在黑白(二进制)图像上绘制直线的最佳方法是什么

请注意,我不想添加注释行。我希望该行成为图像的一部分。

提供了一种方法。

您可能想看看有关的SO问题。这里有一个与我在该答案中的示例类似的示例,它将在行和列索引
(10,10)
(240,120)
之间画一条白线:

这是最终的图像:


如果您被其他方法的例外情况所困扰,这里有一种防弹方法,它会产生一行:

  • 其像素在整个线条长度内始终相互接触(像素相互之间为8个相邻像素)
  • 线路密度不取决于附加参数,而是灵活确定,以适应从第一点开始的保证

输入(便于利用该代码实现功能):

  • img
    -包含图像的矩阵
  • x1
    y1
    x2
    y2
    -待绘制直线端点的坐标
代码:


下面是一个示例图像,上面画了三条线:

这实际上只是对普列西夫答案的修改。我在一幅图像上画了数千条线,我需要提高性能。通过省略
interp1
调用和使用整数变量所做的最大改进使它稍微快一点。与plesiv的代码相比,它在我的PC上的执行速度快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

如果您有计算机视觉系统工具箱,您可以使用。

这对于对角线非常有效,但对于更平坦的线条可能会添加不需要的像素。如果你不在乎额外的像素,我建议你选择gnovices解决方案,因为它既快又简单。@Jonas:我已经更新了算法以更好地计算线条,从而删除了一些不必要的像素。谢谢你改进我的答案!如果这个方法能够优雅地处理边界外的点,那就太好了。我使用了一些光线相交代码来寻找给定直线的点斜率形式的边界。它在调用这个函数之前就已经完成了,但是可以很容易地合并它。是的。。。很抱歉这是我很久以前写的。现在进行改进需要付出太多的努力,因为我目前没有Matlab。如果你删除
interp1
调用,速度会更快,考虑到你上面的评论,我发布了一个新的答案。
insertShape
速度非常慢,它只返回RGB格式的图像。
% 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;
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