2012-06-28 542 views
5

將軸座標轉換爲圖像中像素座標的偏好方式(如plotpoint1point2的輸出的那些)的首選方式是什麼?MATLAB如何將軸座標轉換爲像素座標?

我在Mathworks文檔中看到函數axes2pix,但它的工作原理尚不清楚。具體來說,第三個參數是什麼?這些示例僅通過30,但尚不清楚這個值來自哪裏。解釋依賴於我不知道的其他幾個功能的知識。

相關問題:Axis coordinates to pixel coordinates?建議使用poly2mask,這將適用於多邊形,但我如何爲單點或點列表做同樣的事情?

這個問題還鏈接到Scripts to Convert Image to and from Graph Coordinates,但代碼拋出一個異常:

Error using/
Matrix dimensions must agree. 

回答

0

有可能是我還沒有聽說過的一個內置的方式,但是這不應該是很難做到從零開始...

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軸偏移量。如果你不關心這個,不要使用它。同樣,如果您想在屏幕座標中添加通過get(figure_handle,'position')

+0

這看起來很有希望。但是,如果我沒有軸柄,該怎麼辦?例如,houghlines http://www.mathworks.com/help/toolbox/images/ref/houghlines.html接受二進制圖像並返回包含帶有(x,y)的行的結構。這是我想要處理的具體情況。他們是如何做到的呢? – dsg

+0

因爲'houghlines'本身就是一個圖像(2D矩陣),我想'X,Y'對可能已經在像素空間了,因爲沒有圖形/軸對象(因此沒有轉換到屏幕空間)來解釋。 – tmpearce

1

獲得的位置的x偏移量,請考慮以下代碼。它顯示瞭如何從座標軸轉換爲圖像像素座標。

如果使用默認的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 

screenshot

(注意如何軸界限不會在(1,1)啓動,從而爲axes2pix的需要)

相關問題