54 unsigned char currValue = I[i][j];
58 if (I[i - 1][j] == currValue) {
63 if (I[i][j - 1] == currValue) {
68 if (I[i][j + 1] == currValue) {
73 if (I[i + 1][j] == currValue) {
78 for (
int cpt1 = -1; cpt1 <= 1; ++cpt1) {
79 for (
int cpt2 = -1; cpt2 <= 1; ++cpt2) {
81 if ((cpt1 != 0) || (cpt2 != 0)) {
82 if (I[
static_cast<int>(i) + cpt1][
static_cast<int>(j) + cpt2] == currValue) {
83 listOfNeighbors.push(
vpImagePoint(
static_cast<int>(i) + cpt1,
static_cast<int>(j) + cpt2));
95 while (!listOfNeighbors.empty()) {
97 unsigned int i =
static_cast<unsigned int>(imPt.
get_i());
98 unsigned int j =
static_cast<unsigned int>(imPt.
get_j());
99 listOfNeighbors.pop();
106 labels[i][j] = current_label;
113 if (I.getSize() == 0) {
117 labels.
resize(I.getHeight(), I.getWidth());
121 unsigned int i_copy_height = I_copy.
getHeight();
122 for (
unsigned int i = 0; i < i_copy_height; ++i) {
123 if ((i == 0) || (i == (I_copy.
getHeight() - 1))) {
124 memset(I_copy[i], 0,
sizeof(
unsigned char) * I_copy.
getWidth());
128 memcpy(I_copy[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
129 I_copy[i][I_copy.
getWidth() - 1] = 0;
133 vpImage<int> labels_copy(I.getHeight() + 2, I.getWidth() + 2, 0);
135 int current_label = 1;
136 std::queue<vpImagePoint> listOfNeighbors;
138 unsigned int i_height = I.getHeight();
139 for (
unsigned int cpt1 = 0; cpt1 < i_height; ++cpt1) {
140 unsigned int i = cpt1 + 1;
142 unsigned int i_width = I.getWidth();
143 for (
unsigned int cpt2 = 0; cpt2 < i_width; ++cpt2) {
144 unsigned int j = cpt2 + 1;
146 if (I_copy[i][j] && (labels_copy[i][j] == 0)) {
152 labels_copy[i][j] = current_label;
154 visitNeighbors(I_copy, listOfNeighbors, labels_copy, current_label, connexity);
161 unsigned int labels_height = labels.
getHeight();
162 for (
unsigned int i = 0; i < labels_height; ++i) {
163 memcpy(labels[i], labels_copy[i + 1] + 1,
sizeof(
int) * labels.
getWidth());
166 nbComponents = current_label - 1;
VISP_EXPORT void connectedComponents(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, VISP_NAMESPACE_ADDRESSING vpImage< int > &labels, int &nbComponents, const VISP_NAMESPACE_ADDRESSING vpImageMorphology::vpConnexityType &connexity=VISP_NAMESPACE_ADDRESSING vpImageMorphology::CONNEXITY_4)