I have a skeleton as binary pixels, such as this:
I would like to find the coordinates of the end points of this skeleton (in this case there are four), using Open CV if applicable.
Efficiency is important as I'm analysing a number of these in real time from a video feed and need to be doing lots of other things at the same time.
(Note, apologies that the screenshot above has resizing artefacts, but it is an 8-connected skeleton I am working with.)
Given your tags of your questions and answers in your profile, I'm going to assume you want a C++ implementation. When you skeletonize an object, the object should have a 1 pixel thickness. Therefore, one thing that I could suggest is find those pixels that are non-zero in your image, then search in an 8-connected neighbourhood surrounding this pixel and count those pixels that are non-zero. If the count is only 2, then that is a candidate for an skeleton endpoint. Note that I'm also going to ignore the border so we don't go out of bounds. If the count is 1, it's a noisy isolated pixel so we should ignore it. If it's 3 or more, then that means that you're examining part of the skeleton at either a point within the skeleton, or you're at a point where multiple lines are connected together, so this shouldn't be an endpoint either.
I honestly can't think of any algorithm other than checking all of the skeleton pixels for this criteria.... so the complexity will be
O(mn)
, wherem
andn
are the rows and columns of your image. For each pixel in your image, the 8 pixel neighbourhood check takes constant time and this will be the same for all skeleton pixels you check. However, this will certainly be sublinear as the majority of your pixels will be 0 in your image, so the 8 pixel neighbourhood checking won't happen most of the time.As such, this is something that I would try, assuming that your image is stored in a
cv::Mat
structure calledim
, it being a single channel (grayscale) image, and is of typeuchar
. I'm also going to store the co-ordinates of where the skeleton end points are in astd::vector
type. Every time we detect a skeleton point, we will add two integers to the vector at a time - the row and column of where we detect the ending skeleton point.If you want to show the co-ordinates when you're done, just check every pair of elements in this vector:
To be complete, here's a Python implementation as well. I'm using some of
numpy
's functions to make this easier for myself. Assuming that your image is stored inimg
, which is also a grayscale image, and importing the OpenCV library andnumpy
(i.e.import cv2
,import numpy as np
), this is the equivalent code:To show the co-ordinates of the end points, you can do:
Minor note: This code is untested. I don't have C++ OpenCV installed on this machine so hopefully what I wrote will work. If it doesn't compile, you can certainly translate what I have done into the right syntax. Good luck!
A bit late, but this still might be useful for people!
There's a way of doing the exact same thing as @rayryeng suggests, but with the builtin functions of openCV! This makes it much smaller, and probably way faster (especially with Python, if you are using that, as I am!) It is the same solution as this one.
Basically, what we are trying to find is the pixels that are non-zero, with one non-zero neighbor. So what we do is use openCV's built in filter2D function to convolve the skeleton image with a custom kernel that we make. I just learned about convolution and kernels, and this page is really helpful at explaining what these things mean.
So, what kernel would work? How about
Then, after applying this kernel, any pixel with the value 11 is one that we want!
Here is what I use:
Hope this helps!