40 #include <visp3/imgproc/vpImgproc.h>
44 void 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[
static_cast<int>(i) + cpt1][
static_cast<int>(j) + cpt2] == currValue) {
76 listOfNeighbors.push(
vpImagePoint(
static_cast<int>(i) + cpt1,
static_cast<int>(j) + cpt2));
88 while (!listOfNeighbors.empty()) {
90 unsigned int i =
static_cast<unsigned int>(imPt.
get_i());
91 unsigned int j =
static_cast<unsigned int>(imPt.
get_j());
92 listOfNeighbors.pop();
95 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
99 labels[i][j] = current_label;
117 unsigned int i_copy_height = I_copy.
getHeight();
118 for (
unsigned int i = 0; i < i_copy_height; ++i) {
119 if ((i == 0) || (i == (I_copy.
getHeight() - 1))) {
120 memset(I_copy[i], 0,
sizeof(
unsigned char) * I_copy.
getWidth());
124 memcpy(I_copy[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
125 I_copy[i][I_copy.
getWidth() - 1] = 0;
131 int current_label = 1;
132 std::queue<vpImagePoint> listOfNeighbors;
135 for (
unsigned int cpt1 = 0; cpt1 < i_height; ++cpt1) {
136 unsigned int i = cpt1 + 1;
138 unsigned int i_width = I.
getWidth();
139 for (
unsigned int cpt2 = 0; cpt2 < i_width; ++cpt2) {
140 unsigned int j = cpt2 + 1;
142 if (I_copy[i][j] && (labels_copy[i][j] == 0)) {
144 getNeighbors(I_copy, listOfNeighbors, i, j, connexity);
148 labels_copy[i][j] = current_label;
150 visitNeighbors(I_copy, listOfNeighbors, labels_copy, current_label, connexity);
157 unsigned int labels_height = labels.
getHeight();
158 for (
unsigned int i = 0; i < labels_height; ++i) {
159 memcpy(labels[i], labels_copy[i + 1] + 1,
sizeof(
int) * labels.
getWidth());
162 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 vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)