40#include <visp3/imgproc/vpImgproc.h>
44void getNeighbors(
const vpImage<unsigned char> &I, std::queue<vpImagePoint> &listOfNeighbors,
unsigned int i,
47 unsigned char currValue = I[i][j];
51 if (I[i - 1][j] == currValue) {
56 if (I[i][j - 1] == currValue) {
61 if (I[i][j + 1] == currValue) {
66 if (I[i + 1][j] == currValue) {
71 for (
int cpt1 = -1; cpt1 <= 1; cpt1++) {
72 for (
int cpt2 = -1; cpt2 <= 1; cpt2++) {
74 if (cpt1 != 0 || cpt2 != 0) {
75 if (I[(
int)i + cpt1][(
int)j + cpt2] == currValue) {
76 listOfNeighbors.push(
vpImagePoint((
int)i + cpt1, (
int)j + cpt2));
88 while (!listOfNeighbors.empty()) {
90 unsigned int i = (
unsigned int)imPt.
get_i();
91 unsigned int j = (
unsigned int)imPt.
get_j();
92 listOfNeighbors.pop();
95 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
99 labels[i][j] = current_label;
117 for (
unsigned int i = 0; i < I_copy.
getHeight(); i++) {
118 if (i == 0 || i == I_copy.
getHeight() - 1) {
119 memset(I_copy[i], 0,
sizeof(
unsigned char) * I_copy.
getWidth());
123 memcpy(I_copy[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
124 I_copy[i][I_copy.
getWidth() - 1] = 0;
130 int current_label = 1;
131 std::queue<vpImagePoint> listOfNeighbors;
133 for (
unsigned int cpt1 = 0; cpt1 < I.
getHeight(); cpt1++) {
134 unsigned int i = cpt1 + 1;
136 for (
unsigned int cpt2 = 0; cpt2 < I.
getWidth(); cpt2++) {
137 unsigned int j = cpt2 + 1;
139 if (I_copy[i][j] && labels_copy[i][j] == 0) {
141 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
145 labels_copy[i][j] = current_label;
147 visitNeighbors(I_copy, listOfNeighbors, labels_copy, current_label, connexity);
155 for (
unsigned int i = 0; i < labels.
getHeight(); i++) {
156 memcpy(labels[i], labels_copy[i + 1] + 1,
sizeof(
int) * labels.
getWidth());
159 nbComponents = current_label - 1;
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
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 vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)