2017-02-13 37 views
2

這是一個複雜設置的更具體的問題。我已經計算所有1像素的距離,以原始圖像的其最近0像素,並轉化到與局部最大值如下所示的圖像:如何用Matlab連接圖像中的斷開點?

enter image description here

我用下面的代碼,以提取的局部最大值從這個變換後的矩陣:

loc_max = imregionalmax(loc,4); 
figure;imshow(loc_max) 

loc_max給出那些局部最大點的斷開點。嘗試了一系列連接它們的方式,但並未按照它應該做的那樣工作。

這裏是我的一個想法:

[yy xx]=find(loc_max ==1); 
d = []; 
dmin = []; 
idx = []; 
for i = 1: size(yy,1) 
    for j = 1:size(xx,1) 
     if i ~= j 
      d(j) = sqrt(sum(bsxfun(@minus,[yy(j) xx(j)],[yy(i) xx(i)]).^2,2)); 
      %calculate the distance between current 1-pixel in loc_max and all other local maxima pixels in loc_max. 
     end   
    end  
    [dmin(i),idx(i)] = min(d(:));%find the minimum distance between current 1-pixel to others 
end 

我試圖找到在loc_max最近的1個像素的loc_max目前的1像素,然後將它們連接起來。但這還不是解決方案。因爲如果前一個像素是當前像素的最近像素,它將不會連接到其下一個1像素。

此外,我想保留這些0像素的像素信息沿兩個斷開的1像素之間的連接線。我希望能夠在以後重建這個簡化的框架中的整個圖像。

任何幫助將不勝感激!

+0

侵蝕和擴張的! –

+0

謝謝,與imdilate嘗試,不按我想要的那樣工作。 – Orangeblue

+0

這是一個非常有限的信息量 –

回答

3

我試過侵蝕和擴張(比如Ander Biguri建議)。
我試着設置內核的角度是正確的。

檢查以下內容:

%Fix the input (remove JPEG artifacts and remove while margins) 
%(This is not part of the solution - just a little cleanup). 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
I = imread('https://i.stack.imgur.com/4L3fP.jpg'); %Read image from imgur. 
I = im2bw(I); 
[y0, x0] = find(I == 0); 
[y1, x1] = find(I == 0, 1, 'last'); 
I = I(y0:y1, x0:x1); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

se0 = strel('disk', 3); 
J = imdilate(I, se0); %Dilate - make line fat 

se1 = strel('line', 30, 150); %150 degrees. 
se2 = imdilate(se1.getnhood , se0); %Create 150 degrees fat line 
J = imclose(J, se2); %Close - (dilate and erode) 

se1 = strel('line', 30, 140); %140 degrees. 
se2 = imdilate(se1.getnhood , se0); 
J = imclose(J, se2); 

se1 = strel('line', 80, 60); %60 degrees. 
se2 = imdilate(se1.getnhood , se0); %Create 60 degrees fat line 
J = imclose(J, se2); 

se4 = strel('disk', 2); 
J = imerode(J, se4); %Erode - make lines little thinner. 

figure;imshow(J); 

結果:

enter image description here

你覺得它足夠好?


安德Biguri獲取信貸以下解決方案:

J = bwmorph(J,'skel',Inf); 

enter image description here

+0

安德爾的建議後,我得到了類似的結果。但是,它可以給所有點連接一條線嗎? – Orangeblue

+0

@Orangeblue做到這一點,然後使用'bwmorph(BW,'skel',Inf)獲得圖像的骨架;' –

+0

@AnderBiguri尼斯......下次嘗試連接點時嘗試記住它。 – Rotem