1

I am trying to implement skeletonize alogrithm using opencv. I want to reduce the number of pixels of the below image and make it thin.

Here is the image which I want to skeletonize:

enter image description here

Here is my code:

Mat image = imread("4X4_b9.png",IMREAD_GRAYSCALE);
    int rows = image.rows;
    int cols = image.cols;
    int size = rows * cols;
    Mat skel = Mat::zeros(image.rows,image.cols,CV_8U);
    threshold(image, image, 100, 255, THRESH_BINARY);
    Mat element = getStructuringElement(MORPH_CROSS, Size(3,3));
    bool done = false;
    while(done == false){
        Mat eroded;
        erode(image, eroded, element);
        Mat temp;
        dilate(eroded, temp, element);
        subtract(image, temp, temp);
        bitwise_or(skel, temp, skel);
        eroded.copyTo(image);

        int zeros = size - countNonZero(image);
        if(zeros == size){
            done = true;
        }
    }

    imshow("skel",skel);
    waitKey(0);
    destroyAllWindows();

Note: I tried this link for reference: python implemented

After running this lot of lines are missing.

Here is the output:

enter image description here

What I did wrong ?

10
  • Post your image. Commented Nov 6, 2017 at 17:26
  • 2
    You're dilating the original image dilate(image, temp, element);. You should be dilating the eroded one dilate(eroded, temp, element);. Commented Nov 6, 2017 at 17:41
  • 2
    opencv contrib has thinning github.com/opencv/opencv_contrib/blob/master/modules/ximgproc/… Commented Nov 6, 2017 at 18:46
  • 2
    here you can find another implementation ( i think faster than OpenCV ) Commented Nov 6, 2017 at 18:52
  • 1
    @Xoxo you can find another C++ example here, but probably sturkmen's suggestions are better Commented Nov 6, 2017 at 19:01

1 Answer 1

-1

Convert img to a black background with white color shape

img -> threshold(img, img, 128, 255, THRESHOLD_BINARY_INV);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.