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:
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:
What I did wrong ?


dilate(image, temp, element);. You should be dilating the eroded onedilate(eroded, temp, element);.