2017-02-13 197 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