如何在Matlab中永久地在图像的两个坐标之间画一条线? [重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我有一组要按顺序连接的点。假设点是(A1,A2,A3,... A9);我想将A1连接到A2,A2连接到A3等等,最后将A9连接到A1。

我只需要知道一个可以帮助我将A1连接到A2的函数,我可以使用for循环完成剩下的工作。

我知道连接两点是这里曾经问过好几次的问题,但我找不到我要求的答案。一些解决方案建议使用“plot”和“line”,但这些功能会覆盖图像上的结果,并且实际上不会对原始图像进行任何更改。

我确实尝试了它们并设法使用“saveas”和“print”函数保存结果图形但是图像没有以正确的格式保存,并且使用这些函数的参数存在很多问题。此外,我真的不想保存图像,这只是我愿意添加的不必要的开销,如果我能用线条获得所需的图像。我也试过“imline”画线,但它似乎是互动的。

This特别的问题完全反映了我的问题但是当我运行作为解决方案给出的代码片段时,它们都在结果图像中给出了一组点。

我在this图像的链接中尝试了上面提到的代码,我找到了here

虚线是上面链接中所有三个代码片段的输出。例如,我运行了第一个代码:

I = imread('berries_copy.png'); 
grayImage=rgb2gray(I); 
img =false(size(grayImage,1), size(grayImage,2)); 

我编写了上面的代码只是为了获得以下操作的黑色图像:

x = [500 470];        % x coordinates 
y = [300 310];        % 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

This是上面代码以及其他两个代码的结果图像,正如您所看到的,它只是黑色背景上的几个点,而不是所需的输出。

我更改了代码并使用“plot”函数来获取this输出,这就是我想要的。无论如何我可以将虚线输出改成实线吗?

或者,如果任何人都可以建议一个简单的函数或方法从A1到A2绘制一条线,并实际上会改变输入图像,我将不胜感激。 (我真的希望这只是我的新手,而不是Matlab没有简单的功能来在图像中画一条线。)

附:我没有计算机视觉工具箱,如果可能的话,我想找到一个不涉及它的解决方案。

matlab image-processing
3个回答
2
投票

您可以使用Bresenham算法。当然它已经实现,你可以在这里找到它:Bresenham optimized for Matlab。该算法选择近似于一条线的像素。

一个简单的例子,使用您的变量名称可以是:

I = rgb2gray(imread('peppers.png'));
A1 = [1 1];
A2 = [40 40];
[x y] = bresenham(A1(1),A1(2),A2(1),A2(2));
ind = sub2ind(size(I),x,y);
I(ind) = 255;
imshow(I) 

3
投票

您的第一个问题是您创建的空白图像与使用此行加载的图像大小相同:

img =false(size(grayImage,1), size(grayImage,2));

当你向它添加线条时,你会得到一个黑色图像,上面有一条白线,正如预期的那样。

你的第二个问题是你正在尝试将grayscale intensity images的解决方案应用于RGB (Truecolor) image,这需要你使用modify the data at the given indices for all three color planes(红色,绿色和蓝色)。以下是如何修改my other answer的灰度解决方案:

img = imread('berries_copy.png');  % Load image
[R, C, D] = size(img);             % Get dimension sizes, D should be 3
x = [500 470];                     % x coordinates
y = [300 310];                     % 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([R C], rIndex, cIndex);         % Linear indices
img(index) = 255;                  % Modify red plane
img(index+R*C) = 255;              % Modify green plane
img(index+2*R*C) = 255;            % Modify blue plane
imshow(img);                       % Display image
imwrite(img, 'berries_line.png');  % Save image, if desired

并得到的图像(注意右下角浆果上方的白线):

enter image description here


1
投票

您可以使用imshow显示图像,然后使用plot绘制线条并保存图形。检查以下代码:

I = imread('peppers.png') ;
imshow(I)
hold on
[m,n,p] = size(I) ;
%% Get random points A1, A2,..A10
N = 9 ;
x = (n-1)*rand(1,N)+1 ;
y = (m-1)*rand(1,N)+1 ;
P = [x; y]; % coordinates / points 
c = mean(P,2); % mean/ central point 
d = P-c ; % vectors connecting the central point and the given points 
th = atan2(d(2,:),d(1,:)); % angle above x axis
[th, idx] = sort(th);   % sorting the angles 
P = P(:,idx); % sorting the given points
P = [P P(:,1)]; % add the first at the end to close the polygon 
plot( P(1,:), P(2,:), 'k');
saveas(gcf,'image.png')
© www.soinside.com 2019 - 2024. All rights reserved.