这是复杂设置的更具体问题。我已计算出所有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中最近的1像素到loc_max中的当前1像素,然后连接它们。但这还不是解决方案。因为如果前一个像素是当前像素的最近像素,则它不会连接到下一个1像素。
此外,我想保留沿两个断开的1像素之间的连接线的那些0像素的像素信息。我希望以后能够从这个简化的骨架中重建整个图像。
非常感谢任何帮助!
答案 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);
结果:
你认为它足够好吗?
Ander Biguri获得以下解决方案的荣誉:
J = bwmorph(J,'skel',Inf);