I’m implementing the component labelling algorithm as in this paper using python and opencv. It requires checking the input image pixel-by-pixel and perform the so-called contour tracing subroutine to assign label to the blobs of a binary image.
I manage to have it running, but it seems very slow. Profiling the code shows that the for-loop to access the pixels seems to be the bottleneck. It takes about 200ms for a 256px*256px image. Here’s roughly what I do:
for i in image.height:
for j in image.width:
p = image[i, j]
pa = image[i - 1, j]
pb = image[i + 1, j]
# etc...
where “image” is a binary opencv image.
I wonder if there’s a faster way of doing it so that it’s usable also for video applications. I’m targeting something like 40-50ms running time for the same problem size, to get 20-25fps. 10-15fps would probably be acceptable as well (66-100ms running time).
Any hints, ideas what I can do is much appreciated.
The latest OpenCV bindings for Python return numpy data types, meaning you have the full numpy arsenal at your disposal. Looping over a 2D array in numpy (with indices) is generally done with an
ndenumerate, which should offer at least a little speedup (since it’s a single loop optimized for N-D arrays). You could look into numpyvectorizewhich would yield even more speedup, but if you need the indices of the array, then anndenumeratewould be what you need.Beyond this, your best bet may be writing bottleneck operations in C.
Update
If it would help, I believe
scipy.ndimage.labeldoes exactly what you’re attempting to do, and might even use the same algorithm.