40 #include <visp3/imgproc/vpImgproc.h>
48 unsigned char currValue = I[i][j];
52 if (I[i - 1][j] == currValue) {
57 if (I[i][j - 1] == currValue) {
62 if (I[i][j + 1] == currValue) {
67 if (I[i + 1][j] == currValue) {
72 for (
int cpt1 = -1; cpt1 <= 1; ++cpt1) {
73 for (
int cpt2 = -1; cpt2 <= 1; ++cpt2) {
75 if ((cpt1 != 0) || (cpt2 != 0)) {
76 if (I[
static_cast<int>(i) + cpt1][
static_cast<int>(j) + cpt2] == currValue) {
77 listOfNeighbors.push(
vpImagePoint(
static_cast<int>(i) + cpt1,
static_cast<int>(j) + cpt2));
89 while (!listOfNeighbors.empty()) {
91 unsigned int i =
static_cast<unsigned int>(imPt.
get_i());
92 unsigned int j =
static_cast<unsigned int>(imPt.
get_j());
93 listOfNeighbors.pop();
100 labels[i][j] = current_label;
115 unsigned int i_copy_height = I_copy.
getHeight();
116 for (
unsigned int i = 0; i < i_copy_height; ++i) {
117 if ((i == 0) || (i == (I_copy.
getHeight() - 1))) {
118 memset(I_copy[i], 0,
sizeof(
unsigned char) * I_copy.
getWidth());
122 memcpy(I_copy[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
123 I_copy[i][I_copy.
getWidth() - 1] = 0;
129 int current_label = 1;
130 std::queue<vpImagePoint> listOfNeighbors;
133 for (
unsigned int cpt1 = 0; cpt1 < i_height; ++cpt1) {
134 unsigned int i = cpt1 + 1;
136 unsigned int i_width = I.
getWidth();
137 for (
unsigned int cpt2 = 0; cpt2 < i_width; ++cpt2) {
138 unsigned int j = cpt2 + 1;
140 if (I_copy[i][j] && (labels_copy[i][j] == 0)) {
146 labels_copy[i][j] = current_label;
148 visitNeighbors(I_copy, listOfNeighbors, labels_copy, current_label, connexity);
155 unsigned int labels_height = labels.
getHeight();
156 for (
unsigned int i = 0; i < labels_height; ++i) {
157 memcpy(labels[i], labels_copy[i + 1] + 1,
sizeof(
int) * labels.
getWidth());
160 nbComponents = current_label - 1;
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
unsigned int getSize() const
unsigned int getHeight() const
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)
void getNeighbors(const vpImage< unsigned char > &I, std::queue< vpImagePoint > &listOfNeighbors, unsigned int i, unsigned int j, const vpImageMorphology::vpConnexityType &connexity)
void visitNeighbors(vpImage< unsigned char > &I_copy, std::queue< vpImagePoint > &listOfNeighbors, vpImage< int > &labels, int current_label, const vpImageMorphology::vpConnexityType &connexity)