Home:ALL Converter>Skeletonization using EmguCV

Skeletonization using EmguCV

Ask Time:2014-06-15T13:37:22         Author:NESHOM

Json Formatter

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!

Author:NESHOM,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24226871/skeletonization-using-emgucv
user3824464 :

Your code works fine with image like this: https://i.stack.imgur.com/YMA4r.jpg\n\nI think you use image, with white backround instead of black.\n\nEasy way to overcome is to invert your image.\n\nImage<Gray, Byte> invert_img = (new Image<Gray, byte>(img_old.Width, img_old.Height, new Gray(255))).Sub(img_old);\n\n\nThen you can invert it back.\n\nskel = (new Image<Gray, byte>(img_old.Width, img_old.Height, new Gray(255))).Sub(skel);\n",
2014-07-10T09:15:07
yy