Skeletonization using EmguCV

放肆的年华 提交于 2019-12-13 02:50:31

问题


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!


回答1:


Your code works fine with image like this: http://i.stack.imgur.com/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);



回答2:


The Aforge Library provides the skeletonization function

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



来源:https://stackoverflow.com/questions/24226871/skeletonization-using-emgucv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!