45 #include <visp3/imgproc/vpImgproc.h> 49 void getNeighbors(
const vpImage<unsigned char> &I, std::queue<vpImagePoint> &listOfNeighbors,
unsigned int i,
52 unsigned char currValue = I[i][j];
56 if (I[i - 1][j] == currValue) {
61 if (I[i][j - 1] == currValue) {
66 if (I[i][j + 1] == currValue) {
71 if (I[i + 1][j] == currValue) {
75 for (
int cpt1 = -1; cpt1 <= 1; cpt1++) {
76 for (
int cpt2 = -1; cpt2 <= 1; cpt2++) {
78 if (cpt1 != 0 || cpt2 != 0) {
79 if (I[(
int)i + cpt1][(
int)j + cpt2] == currValue) {
80 listOfNeighbors.push(
vpImagePoint((
int)i + cpt1, (
int)j + cpt2));
92 while (!listOfNeighbors.empty()) {
94 unsigned int i = (
unsigned int)imPt.
get_i();
95 unsigned int j = (
unsigned int)imPt.
get_j();
96 listOfNeighbors.pop();
99 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
103 labels[i][j] = current_label;
130 for (
unsigned int i = 0; i < I_copy.
getHeight(); i++) {
131 if (i == 0 || i == I_copy.
getHeight() - 1) {
132 memset(I_copy[i], 0,
sizeof(
unsigned char) * I_copy.
getWidth());
135 memcpy(I_copy[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
136 I_copy[i][I_copy.
getWidth() - 1] = 0;
142 int current_label = 1;
143 std::queue<vpImagePoint> listOfNeighbors;
145 for (
unsigned int cpt1 = 0; cpt1 < I.
getHeight(); cpt1++) {
146 unsigned int i = cpt1 + 1;
148 for (
unsigned int cpt2 = 0; cpt2 < I.
getWidth(); cpt2++) {
149 unsigned int j = cpt2 + 1;
151 if (I_copy[i][j] && labels_copy[i][j] == 0) {
153 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
157 labels_copy[i][j] = current_label;
159 visitNeighbors(I_copy, listOfNeighbors, labels_copy, current_label, connexity);
167 for (
unsigned int i = 0; i < labels.
getHeight(); i++) {
168 memcpy(labels[i], labels_copy[i + 1] + 1,
sizeof(
int) * labels.
getWidth());
171 nbComponents = current_label - 1;
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
VISP_EXPORT void connectedComponents(const vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)
unsigned int getHeight() const
unsigned int getSize() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
unsigned int getWidth() const