【问题标题】:MATLAB How to convert axis coordinates to pixel coordinates?MATLAB如何将轴坐标转换为像素坐标?
【发布时间】:2013-08-01 13:46:12
【问题描述】:

从轴坐标(例如由plot 获取的坐标或point1point2 中的houghlines 中的输出坐标)转换为图像中像素坐标的首选方法是什么?

我在 Mathworks 文档中看到了函数 axes2pix,但不清楚它是如何工作的。具体来说,第三个论点是什么?示例只是传入30,但不清楚该值来自何处。解释取决于我不知道的其他几个功能的知识。

相关问题:Axis coordinates to pixel coordinates? 建议使用poly2mask,这适用于多边形,但我如何为单个点或点列表做同样的事情?

该问题也链接到Scripts to Convert Image to and from Graph Coordinates,但该代码引发了异常:

Error using  / 
Matrix dimensions must agree.

【问题讨论】:

    标签: image matlab pixel coordinate


    【解决方案1】:

    考虑以下代码。它展示了如何将坐标轴坐标转换为图像像素坐标。

    如果您使用默认的1:width1:height 以外的自定义XData/YData 位置绘制图像,这将特别有用。在下面的示例中,我在 x/y 方向上移动了 100 和 200 像素。

    function imageExample()
        %# RGB image
        img = imread('peppers.png');
        sz = size(img);
    
        %# show image
        hFig = figure();
        hAx = axes();
        image([1 sz(2)]+100, [1 sz(1)]+200, img)    %# shifted XData/YData
    
        %# hook-up mouse button-down event
        set(hFig, 'WindowButtonDownFcn',@mouseDown)
    
        function mouseDown(o,e)
            %# get current point
            p = get(hAx,'CurrentPoint');
            p = p(1,1:2);
    
            %# convert axes coordinates to image pixel coordinates
            %# I am also rounding to integers
            x = round( axes2pix(sz(2), [1 sz(2)], p(1)) );
            y = round( axes2pix(sz(1), [1 sz(1)], p(2)) );
    
            %# show (x,y) pixel in title
            title( sprintf('image pixel = (%d,%d)',x,y) )
        end
    end
    

    (注意轴限制不是从(1,1) 开始的,因此需要axes2pix

    【讨论】:

      【解决方案2】:

      可能有一种我没有听说过的内置方式,但这应该不难从头开始......

      set(axes_handle,'units','pixels');
      pos = get(axes_handle,'position');
      xlim = get(axes_handle,'xlim');
      ylim = get(axes_handle,'ylim');
      

      使用这些值,您可以轻松地将坐标轴坐标转换为像素。

      x_in_pixels = pos(1) + pos(3) * (x_in_axes-xlim(1))/(xlim(2)-xlim(1));
      %# etc...
      

      上面使用pos(1)作为图中坐标轴的x-offset。如果您不关心这一点,请不要使用它。同样,如果你想要它在屏幕坐标中,添加由get(figure_handle,'position')获得的位置的x-offset

      【讨论】:

      • 这看起来很有希望。但是,如果我没有轴手柄怎么办?例如,houghlines mathworks.com/help/toolbox/images/ref/houghlines.html 接收二进制图像并返回包含 (x, y) 行的结构。这是我要处理的具体情况。他们是怎么做到的?
      • 由于 houghlines 本身对图像(2D 矩阵)进行操作,我想 X,Y 对可能已经在像素空间中,因为没有图形/轴对象(因此没有转换到屏幕空间)来考虑。
      猜你喜欢
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2020-07-05
      • 2013-09-22
      • 2014-07-21
      • 2013-12-16
      • 1970-01-01
      相关资源
      最近更新 更多