I want to create an image of an object from its morphological skeleton. Is there any function in MATLAB or C,C++ code? Thanks in advance.
Original image, and its skeleton (obtained using bwmorph(image,'skel',Inf)
):
I want to create an image of an object from its morphological skeleton. Is there any function in MATLAB or C,C++ code? Thanks in advance.
Original image, and its skeleton (obtained using bwmorph(image,'skel',Inf)
):
As stated in the comments above, bwmorph(..,'skel',Inf)
gives you a binary image of the skeleton, which is not enough on its own to recover the original image.
On the other, if you had, for each skeleton pixel, the values returned by the distance transform, then you can successfully apply the inverse distance transform (as suggested by @belisarius):
Note that this implementation of InverseDistanceTransform is rather slow (I based it on a previous answer). It repeatedly uses POLY2MASK to get pixels inside the specified circles, so there is room for improvement..
%# get binary image
BW = ~imread('http://img546.imageshack.us/img546/3154/hand2.png');
%# SkeletonTransform[]
skel = bwmorph(BW,'skel',Inf);
DD = double(bwdist(~BW));
D = zeros(size(DD));
D(skel) = DD(skel);
%# zero-centered unit circle
t = linspace(0,2*pi,50);
ct = cos(t);
st = sin(t);
%# InverseDistanceTransform[] : union of all disks centered around each
%# pixel of the distance transform, taking pixel values as radius
[r c] = size(D);
BW2 = false(r,c);
for j=1:c
for i=1:r
if D(i,j)==0, continue; end
mask = poly2mask(D(i,j).*st + j, D(i,j).*ct + i, r, c);
BW2(mask) = true;
end
end
%# plot
figure
subplot(131), imshow(BW), title('original')
subplot(132), imshow(D,[]), title('Skeleton+DistanceTransform')
subplot(133), imshow(BW2), title('InverseDistanceTransform')
The result:
The function bwmorph
can be used for code generation as seen here Image Processing functions for code generation. Write the code in a MATLAB function and use the codegen
command. for generating code. The option for code generation is available past R2012b MATLAB Release Notes.
Depending on your object, you may be able to get a meaningful result using dilation (IMDILATE in Matlab).