Connected component algorithms are basic building blocks of many computer vision algorithms. With bwconncomp
, MatLab provides a simple connected components algorithm for binary images. Motivated by the need to compute connected components in (semantic) segmentations, where applying bwconncomp
is painful, I wrote a MEX wrapper for Ali Rahimi's C++ implementation of a connected component algorithm.
The code is available on GitHub:
GitHubBuilding
The MEX wrapper is compiled as follows:
>> mex sp_fast_connected_relabel.cpp Building with 'g++'. Warning: You are using gcc version '4.8.4'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release. Warning: You are using gcc version '4.8.4-2ubuntu1~14.04.1)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release. MEX completed successfully.
Usage
The wrapper operates on matrices of type double. A usage example is given below:
>> image = imread('checkerboard.png'); >> labels = sp_fast_connected_relabel(double(image)); >> imshow(uint8(labels)*10) >> imwrite(uint8(labels)*10, 'checkerboard_components.png');
Given an image checkerboard.png
, Figure 1 shows the generated output.