matlab中图像上的向量

vectors on an image in matlab

你好吗?我需要帮助。我读取图像然后显示它,然后单击图像上的两个点说:p1p2。现在我想画线(如果可能的话用箭头表示),表明这些是与地平面正交的矢量。然后画平行于 y 轴的线,每个点表示 l1l2,最后计算这些线之间的最短距离。我已经得到了点,画了线,但我不确定如何得到距离。我还观察到,对于一条线,它工作正常,但对于两条线,它们被绘制在不同的位置,我不知道为什么。我已经尝试这样做了大约三天,但我得到的结果与我想要的相差甚远。至于某些部分,我不确定该怎么做。下面是我目前的代码。我还附上了一张图片,展示了我正在努力实现的目标。我希望它是可以理解的。我删除了我试图获得距离的部分,因为即使我知道我需要这些线中的四个点来估计它们之间的最短距离,我也不知道如何获得这些点:( - 我愿意接受建议。 [![在此处输入图片描述][1]][1]

感谢您的帮助。 PS。如果我想做的事情是不可能的,请告诉我。虽然当我开始这样做时,这对我来说似乎是合乎逻辑的。

下面是我当前的代码

% Read and display image
i = imread('sample.jpg');
im = i;
f = figure;
imshow(im);
% Get user clicked points
[p1 p2] = getpts(f);
% Draw parallel vertical lines at given pts. parallel to the y-axis/post
hold on;
line([p1(1) p1(1)], get(gca, 'ylim'));
line([p2(1) p2(1)], get(gca, 'ylim'));

% Calculate shortest distance between these two lines 
% - not sure how to proceed with this because as far as i know I need at
% least 4 points to determine where they meet :(


% Draw vector symbols at points
% 1. draw x component arrow/line - parallel to the x-axis of image 
% (or perpendicular to y-axis??) but must be perpendicular to ground plane
  % Slope of current line
    m = (diff(p1)/diff(get(gca, 'ylim')));
    % Slope of new line
    Li = 10 ;
    minv = -1/m;
    line([mean(p1) mean(p1)+Li],[mean(get(gca, 'ylim')) mean(get(gca, 'ylim'))+Li*minv],'Color','r')
% 2. draw y component arrow/line - parallel to the y-axis of image
    % Slope of current line
    m = (diff(p2)/diff(get(gca, 'ylim')));
    % Slope of new line
    Li = 10 ;
    minv = -1/m;
    line([mean(p2) mean(p2)+Li],[mean(get(gca, 'ylim')) mean(get(gca, 'ylim'))+Li*minv],'Color','k')
axis equal;

请查看 getpts 的文档,它 returns [X,Y],Point_i=[X(i), Y(i)]。

由于在您的情况下两条线是平行的,因此它们不相交,因此 d 是两个选定点的 x 差。或者这只是一个特例?

初稿如下所示:

% Read and display image
i = imread('Sample.jpg');
im = i;
f = figure;
imshow(im);
% Get user clicked points
[X, Y] = getpts(f);
% Draw parallel vertical lines at given pts. parallel to the y-axis/post
hold on;
line([X(1) X(1)], get(gca, 'ylim'));
line([X(2) X(2)], get(gca, 'ylim'));

d=X(2)-X(1);

对于其余代码,请更具体一些。既然要显示图片的法向量,要不要把整个东西画成3D图?

编辑:关于评论,修改了最终代码以在 3D 绘图中显示 2D 图像。可以做进一步的改进,但应该很容易(例如,看看 this or this 的箭头)

% Read and display image
myImg = imread('Sample.jpg');
f1 = figure;
imshow(myImg);
imSize=size(myImg);
% Get user clicked points
[X, Y] = getpts(f1);
xLim=get(gca, 'xlim');
yLim=get(gca, 'ylim');
close(f1);

% Draw parallel vertical lines at given pts. parallel to the y-axis/post in the image
lWidth = 5; %width of line in Pixel, should be odd
lColor = [255,0,0]; %Linecolor in RGB
pColor = [0,255,0]; %Pointcolor in RGB
vColor = [0,0,255]; %Vectorcolor in RGB

% image positions of points in pixel
xP1=int16((X(1)-xLim(1))/(xLim(2)-xLim(1))*imSize(2));
xP2=int16((X(2)-xLim(1))/(xLim(2)-xLim(1))*imSize(2));
yP1=int16((Y(1)-yLim(1))/(yLim(2)-yLim(1))*imSize(1));
yP2=int16((Y(2)-yLim(1))/(yLim(2)-yLim(1))*imSize(1));

% Draw lines in image
for xLine = [xP1,xP2]
    for i = max([1,xLine-(lWidth-1)/2]):min([imSize(2),xLine+(lWidth-1)/2])
        for j=1:imSize(1)
            myImg(j,i,1:3)=lColor;
        end
    end
end

%mark Points
Points=[[xP1,yP1];[xP2,yP2]];
for k = 1:2
    Pos=Points(k,:);
    for i = max([1,Pos(1)-(lWidth-1)/2]):min([imSize(2),Pos(1)+(lWidth-1)/2])
        for j = max([1,Pos(2)-(lWidth-1)/2]):min([imSize(1),Pos(2)+(lWidth-1)/2])
            myImg(j,i,1:3)=pColor;
        end
    end
end

%3D plot - Some rotations required to get a nice axisvalue alignment
g = hgtransform('Matrix',makehgtform('translate',[0,imSize(1),0])*makehgtform('axisrotate',[0,1,0],pi)*makehgtform('zrotate',pi));
f2 = image(g,myImg);
axis([0,imSize(2),0,imSize(1),-0.1,1.1]) %axis range
view(40,30) %view point

%add normal vectors
hold on
r=(lWidth-1)/2;
[X,Y,Z] = cylinder(r);
xC1=X+double(xP1);
xC2=X+double(xP2);
yC1=imSize(1)-Y-double(yP1);
yC2=imSize(1)-Y-double(yP2);
mesh(xC1,yC1,Z,'facecolor',vColor/255,'edgecolor',vColor/255);
mesh(xC2,yC2,Z,'facecolor',vColor/255,'edgecolor',vColor/255);