Reputation: 4904
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.
Upvotes: 4
Views: 3826
Reputation: 599
Many are the posts I've seen lamenting the lack of OpenCV labeling.
As @malloc47 said, scipy.ndimage.label
will work. I've used it, but I wasn't happy with its performance while I was looking for the largest blob in an image. I didn't specifically need a labeling, so I ended up using the cv2.findContours
and cv2.contourArea
to isolate the largest one:
# The [0] is because I didn't care about the hierarchy, which is the second
# return value of cv2.findContours.
contours = cv2.findContours(numpy_array,
mode=cv2.RETR_EXTERNAL,
method=cv2.CHAIN_APPROX_SIMPLE
)[0]
areas = [cv2.contourArea(ctr) for ctr in contours]
max_contour = [contours[areas.index(max(areas))]]
This ended up being a lot faster for me than scipy.ndimage.label
for very similar results. As I said, this isn't exactly a labeling, but one could probably use the contour finder to give a reasonably good labeling.
Upvotes: 1
Reputation: 315
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 numpy vectorize
which would yield even more speedup, but if you need the indices of the array, then an ndenumerate
would 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.label
does exactly what you're attempting to do, and might even use the same algorithm.
Upvotes: 1