【问题标题】:vectors on an image in matlabmatlab中图像上的向量
【发布时间】:2018-06-15 17:45:41
【问题描述】:

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

【问题讨论】:

    标签: matlab vector orthogonal


    【解决方案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图?

    编辑:关于 cmets,最终代码已修改为在 3D 图中显示 2D 图像。可以做进一步的改进,但应该相当容易(例如,查看thisthis 的箭头)

    % 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);
    

    【讨论】:

    • 我更喜欢让它尽可能简单。如果可以在 2D 中使用它,那么我更喜欢 2D。否则如果3D不会那么麻烦。那么那就更有趣了。至于距离,我对如何确定两条线段的两个最近点感到困惑。因为我认为那将是两条线之间的最短距离。
    • 嗯,这就解释了为什么它只适用于一行而不适用于两行,谢谢。
    • 实际上,如果不是太多工作,3D 更有意义,在这种情况下,线条必须平行于地平面的 y 轴。好主意。
    • 您使用的是哪个 matlab 版本,f2 = image(g,myImg); 给出的参数数量错误。
    • 谢谢,我想我修好了。但是快速的问题,如何在 2D 图中查看它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2014-01-29
    • 2014-12-26
    • 2010-11-19
    相关资源
    最近更新 更多