2014-09-02 151 views
1

作爲圓形識別程序的一部分,我有一個帶有已知座標和半徑的幾何圓的背景圖像。我希望圈內的部分由圖像填充,而外部只剩下一部分。我最好的想法是某種圓形面具,但我不確定這是最好的方法。有什麼建議麼?MATLAB:在圓內顯示圖像

X = imread('X.jpg'); % Background image jpg 
Y = imread('Y.jpg'); % Filling image jpg 
cent = [100,100]; % Center of circle 
rad = 20; % Circle radius 

% Fill circle ? 
... 

由於機密性,我還沒有提供擴展代碼。

+0

是在圖像填充的尺寸'Y'一樣的圈?你想讓整個圖像'Y'填充到'X'裏面的圓圈內嗎? – rayryeng 2014-09-02 20:02:00

回答

3


我覺得最困難的部分是由何人所創作完成這件事:http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F

假設:

  • 我假設你不打算指定超出範圍點圖像(即我不在這裏添加驗證)。
  • 我使用背景圖像將圓的「中心」與座標關聯起來。
  • 我假設半徑是以像素爲單位的。
  • 我沒有創建一個帶有已知半徑圓的背景圖像,因爲我不認爲這有必要創建你正在尋找的填充效果(除非我錯過了某些東西)。

代碼:

X = imread('rdel_x.png'); % Background image jpg (I used a random image but this can be your blank + geometric circle) 
Y = imread('rdel_y.png'); % Filling image jpg 
cent = [100,150]; % Center of circle 
rad = 70; % Circle radius 

% make a mesh grid to provide coords for the circle (mask) 
% taken from http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F 
[columnsInImage rowsInImage] = meshgrid(1:size(X,2), 1:size(X,1)); 

% circle points in pixels: 
circlePixels = (rowsInImage - cent(1)).^2 ... 
    + (columnsInImage - cent(2)).^2 <= rad.^2; 
circlePixels3d=repmat(circlePixels,[1 1 3]); % turn into 3 channel (assuming X and Y are RGB) 


X((circlePixels3d)) = Y((circlePixels3d)); % assign the filling image pixels to the background image for pixels where it's the desired circle 
imagesc(X); 
axis image off 

結果:從左至右,背景圖像,填充圖像,從上述代碼的結果。

enter image description here

編輯:如果所有內容都封裝在座標中,則可能甚至不需要背景圖像。例如試着在後面加上這上面的代碼...

Z=zeros(size(X),'uint8'); % same size as your background 
Z(circlePixels3d) = Y(circlePixels3d); 
figure; % new fig 
imagesc(Z); 
axis image off 

enter image description here

+0

很好地完成!我一直在努力掙扎半個小時而沒有得到任何東西+1 – 2014-09-02 21:50:47

+0

非常好。我會採用同樣的方法,但是我在工作,無法寫出答案:(。+1。 – rayryeng 2014-09-02 21:53:29

+0

謝謝@cthr。這是我完成這項工作所需的輸入。非常感謝! – 2014-09-03 15:44:44