2013-01-22 117 views
3

我想在MATLAB中使用數學形態學函數來查找can.png圖像的邊界。輸入圖像是:圖像在Matlab中變形

enter image description here

我希望得到一個邊界,例如:

enter image description here

我試圖用不同的組合和參數使用strel,imerode,imdilate,但結果(遠離預期)

我的一個試用代碼是:

a = imread ('can.png'); 
b = im2bw(a); 

SE = strel('rectangle', [10 50 ]) ; 
i2 = imdilate(b,SE); 

figure(1); imshow(i2); 

p = ones(4); 
c = b - imerode(b,p); 

figure(2); imshow(c); 

輸出是:

enter image description here

任何機構可以幫助我,如何創建預期的圖像(黑色背景爲能瘦邊界,好嗎?非常感謝你。

+1

以何種方式是這樣的圖像變形?改變你的問題的標題,以更好地反映你在之後。 – mmgp

回答

5

二值化在它的形態梯度,然後做一個基本的SE一個擴張,填充孔和最終獲得其邊界(瑣碎給定當前圖像)。這不需要任何神奇的任意閾值。

enter image description hereenter image description here

+0

「二元化其形態梯度」是什麼意思?在SE中(使用strel),我們還需要確定我們使用哪種結構,以及它的大小。 – user1917485

+0

@ user1917485形態梯度非常普遍地定義在膨脹 - 侵蝕,與基本平方。爲了對它進行二進制化,我的意思是簡單地對結果運行otsu以獲得二進制閾值:'im2bw(mgrad,graythresh(mgrad))'。在這裏,如果你真的需要Matlab語法,SE總是等於'strel(square,3)'。 – mmgp

+0

謝謝。我是新手,我試圖實現這一點: mgrad = imread('can.png'); I = im2bw(mgrad,graythresh(mgrad)); se = strel('square',3); (I,se) - imerode(I,se); 012fImf = imfill(c,'holes'); (3),imshow(Imf); 但是罐頭是由白色像素填充 – user1917485

4
im = imread('can.png'); 

% turn image to BW 
imb = (im < 220); 

% fill the holes in the image 
imf = imfill(imb, 'holes'); 

% find the edge 
ed = edge(imf); 

得到的圖像:

enter image description here

+1

謝謝你的代碼。結果是完美的。但是,我想通過使用strel,imerode和imdilate來學習MATLAB中的數學形態學函數。 – user1917485