简体   繁体   English

使用OpenCV获取图像的骨架

[英]Using OpenCV to get skeleton of an image

I read an article talking about how to skeletonsise an image with OpenCV in C++. 我读了一篇文章,谈论如何在C ++中使用OpenCV精简图像。 http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/ http://felix.abecassis.me/2011/09/opencv-morphologic-skeleton/

Seems good. 看起来不错。 But I'm trying to use it in Unity with an asset called OpenCV for Unity (clone of OpenCV Java 3.1.0). 但是我试图在Unity中将它与称为OpenCV for Unity(OpenCV Java 3.1.0的克隆)的资产一起使用。
So I try to port the code from C++ to C# and I failed. 所以我尝试将代码从C ++移植到C#,但失败了。
I tried to debug by writing out image. 我试图通过写出图像进行调试。 Turns out the image is entirely black. 原来图像是全黑的。

BTW, my use case in white background, and the code is for black background I think, but I tested it with black background white shapes, still no luck. 顺便说一句,我的用例是在白色背景中,并且代码是我认为的黑色背景,但是我用黑色背景白色形状对其进行了测试,仍然没有运气。

FML,这就是我所看到的 FML, This is what I see after adding the imwrite line in the loop FML,这是我在循环中添加写入行后看到的内容

Mat GetSkel (Texture2D srcTexture) 
{
    Mat img = new Mat (srcTexture.height, srcTexture.width, CvType.CV_8UC1);
    Utils.texture2DToMat (srcTexture, img);
    Debug.Log ("img.ToString() " + img.ToString ());

    Imgproc.threshold (img, img, 127, 255, Imgproc.THRESH_BINARY);
    Mat skel = new Mat (srcTexture.height, srcTexture.width, CvType.CV_8UC1, new Scalar (0));
    Mat temp = new Mat();
    Mat eroded = new Mat();

    Mat element = Imgproc.getStructuringElement (Imgproc.MORPH_CROSS, new Size(3,3));

    bool done;
    do 
    {
        Imgproc.erode(img, eroded, element);
        Imgproc.dilate(eroded, temp, element);
        Core.subtract(img, temp, temp);
        Core.bitwise_or(skel, temp, skel);
        eroded.copyTo(img);

        done = (Core.countNonZero(img) == 0);
    } while (!done);

    Imgcodecs.imwrite ("/Users/fung/Documents/FYP/Future/Assets/Future -fung/img.jpg", img);

    return img;

}
private void skeleton(Mat _img)
   {
       boolean done = false;
       Mat img = _img.clone();
       CvUtilsFX.showImage(img, "tresh_1");

       Mat imgGray = new Mat();
       Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);

       Mat tresh = new Mat();
       double thresh = Imgproc.threshold(imgGray, tresh, 100, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU); 
       CvUtilsFX.showImage(tresh, "tresh_1");

       Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3,3));
       Mat eroded = new Mat();
       Mat temp = new Mat();
       Mat skel = new Mat (tresh.rows(), tresh.cols(), CvType.CV_8UC1, new Scalar (0));

       int size = _img.cols() * _img.rows();
       int zeros = 0;

       while(!done)
       {
           Imgproc.erode(tresh, eroded, element);
           Imgproc.dilate(eroded, temp, element);
           Core.subtract(tresh, temp, temp);
           Core.bitwise_or(skel, temp, skel);
           eroded.copyTo(tresh);

           zeros = size - Core.countNonZero(tresh);
           if(zeros == size)
               done = true;
       }
       CvUtilsFX.showImage(skel, "Skeleton");
   }

Image 图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM