This is a more specific problem with complex settings. I have calculated the distance of all 1-pixels to its nearest 0-pixels of raw image and transformed to an image with local maxima shown as below:
I used the following code to extract the local maxima from this transformed matrix:
loc_max = imregionalmax(loc,4);
figure;imshow(loc_max)
The loc_max gives out disconnected dots of those local maxima points. Tried a bunch of ways to connect them, but not working as it was supposed to do yet.
Here is one of my ideas:
[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
I tried to find the nearest 1-pixels in loc_max to the current 1-pixel in loc_max, and then connect them. But this isn't the solution yet. Because it won't connect to its next 1-pixel if the previous pixel is the nearest pixel of the current one.
In addition, I want to preserve the pixel information of those 0-pixels along the connected line between two disconnected 1-pixels. I want to be able to reconstruct the whole image out of this simplified skeleton later.
Any help would be greatly appreciated!