NESHOM
NESHOM

Reputation: 929

Skeletonization using EmguCV

I am trying to make a Skeletonization in C# using EmguCV. I am using the sample at the bottom of this page which is in C++ and I tried that and it works: http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/

I have converted the cod to C# as follows:

        Image<Gray, Byte> eroded = new Image<Gray, byte>(img2.Size);
        Image<Gray, Byte> temp = new Image<Gray, byte>(img2.Size);
        Image<Gray, Byte> skel = new Image<Gray, byte>(img2.Size);
        skel.SetValue(0);
        CvInvoke.cvThreshold(img2, img2, 127, 256, 0);
        StructuringElementEx element = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_CROSS);
        bool done = false;

        while (!done)
        {
            CvInvoke.cvErode(img2, eroded, element,1);
            CvInvoke.cvDilate(eroded, temp, element,1);
            temp = img2.Sub(temp);
            skel = skel | temp;
            img2 = eroded;
            if (CvInvoke.cvCountNonZero(img2) == 0) done = true;
        }

But this does not work! What is wrong? In the beginning of the while loop, it erodes the image 'img2' and saves it to 'eroded', then it dilates 'eroded' and then saves it to 'temp'. In the above C# code, this results in 'temp=img2' which makes sense. So why this is working in the C++ code and not n C#? Is something wrong with the way 'element' is defined in the above C# code?

Thanks in advance!

Upvotes: 0

Views: 4080

Answers (2)

Umair
Umair

Reputation: 111

The Aforge Library provides the skeletonization function

http://www.aforgenet.com/framework/docs/html/c0cc0715-0e71-cf63-e1cd-922786f5786e.htm

Upvotes: 1

user3824464
user3824464

Reputation: 26

Your code works fine with image like this: https://i.sstatic.net/YMA4r.jpg

I think you use image, with white backround instead of black.

Easy way to overcome is to invert your image.

Image<Gray, Byte> invert_img = (new Image<Gray, byte>(img_old.Width, img_old.Height, new Gray(255))).Sub(img_old);

Then you can invert it back.

skel = (new Image<Gray, byte>(img_old.Width, img_old.Height, new Gray(255))).Sub(skel);

Upvotes: 1

Related Questions