1

你好吗?我需要帮助。我读取图像然后显示它,然后单击图像上的两个点说: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;
4

1 回答 1

1

请查看 getpts 的文档,它返回 [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 图像。可以做进一步的改进,但应该相当容易(例如,看看这个这个箭头)

% 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);
于 2018-01-05T22:46:33.453 回答