这是一个更具体的问题,具有复杂的设置。我计算了原始图像中所有 1 像素到其最近的 0 像素的距离,并转换为具有局部最大值的图像,如下所示:
我使用以下代码从这个变换后的矩阵中提取局部最大值:
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 中与 loc_max 中当前 1 像素最接近的 1 像素,然后将它们连接起来。但这还不是解决方案。因为如果前一个像素是当前像素最近的像素,则它不会连接到下一个像素。
此外,我想保留沿着两个不相连的 1 像素之间的连接线的那些 0 像素的像素信息。我希望稍后能够从这个简化的骨架中重建整个图像。
任何帮助将不胜感激!
我尝试过侵蚀和扩张(就像 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);
结果:
你觉得这样够好吗?
Ander Biguri 因以下解决方案而获奖:
J = bwmorph(J,'skel',Inf);