int main() { graphm g; g=graphm_vide(2); sommet s1=0,s2=1,s3=2; //g=ajout_arete(s1,s1,g); g=ajout_arete(s1,s2,g); g=ajout_arete(s2,s3,g); g=ajout_arete(s1,s3,g); print_graphm(g); //g=ajout_sommet(3,g); //g=ajout_sommet(4,g); //g=ajout_sommet(5,g); //g=ajout_sommet(6,g); //printf("ffffffff%d\n",g.n); //g=ajout_arete(5,6,g); //g=ajout_arete(3,4,g); //print_graphm(g); //g=ajout_arete(3,s2,g); print_graphm(g); //printf("%d",degre(1,g)); //adjacents(1,g); //g=supp_sommet(s1,g); //print_graphm(g); //g=supp_arete(s2,3,g); //print_graphm(g); //profondeur2(g); //print_coulors(g); //print_graphm(g); g=connexe(g); print_coulors(g); //printf(" %d \n",articulation(g)); //printf(" %d \n",articulation(g)); articulation(g); //print_graphm(g); //print_coulors(g); return 0; }
/*! Compute the center of gravity (COG) of the dot using connex components. We assume the origin pixel (u, v) is in the dot. If not, the dot is seach around this origin using a spiral search. \param I : Image to process. \param u : Starting pixel coordinate along the columns from where the dot is searched . \param v : Starting pixel coordinate along the rows from where the dot is searched . \warning The content of the image is modified. \exception vpTrackingException::featureLostError : If the tracking fails. \sa connexe() */ void vpDot::COG(const vpImage<unsigned char> &I, double& u, double& v) { // Set the maximal number of points considering the maximal dot size // image percentage nbMaxPoint = (I.getWidth() * I.getHeight()) * maxDotSizePercentage; // segmentation de l'image apres seuillage // (etiquetage des composante connexe) if (compute_moment) m00 = m11 = m02 = m20 = m10 = m01 = mu11 = mu20 = mu02 = 0; double u_cog = 0 ; double v_cog = 0 ; double npoint = 0 ; this->mean_gray_level = 0 ; ip_connexities_list.clear() ; ip_edges_list.clear(); // Initialise the boundig box this->u_min = I.getWidth(); this->u_max = 0; this->v_min = I.getHeight(); this->v_max = 0; #if 0 // Original version if ( connexe(I, (unsigned int)u, (unsigned int)v, gray_level_min, gray_level_max, mean_gray_level, u_cog, v_cog, npoint) == vpDot::out) { bool sol = false ; unsigned int pas ; for (pas = 2 ; pas <= 25 ; pas ++ )if (sol==false) { for (int k=-1 ; k <=1 ; k++) if (sol==false) for (int l=-1 ; l <=1 ; l++) if (sol==false) { u_cog = 0 ; v_cog = 0 ; ip_connexities_list.clear() ; this->mean_gray_level = 0 ; if (connexe(I, (unsigned int)(u+k*pas),(unsigned int)(v+l*pas), gray_level_min, gray_level_max, mean_gray_level, u_cog, v_cog, npoint) != vpDot::out) { sol = true ; u += k*pas ; v += l*pas ; } } } if (sol == false) { vpERROR_TRACE("Dot has been lost") ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot has been lost")) ; } } #else // If the dot is not found, search around using a spiral if ( !connexe(I,(unsigned int)u,(unsigned int)v, mean_gray_level, u_cog, v_cog, npoint) ) { bool sol = false ; unsigned int right = 1; unsigned int botom = 1; unsigned int left = 2; unsigned int up = 2; double u_ = u, v_ = v; unsigned int k; // Spiral search from the center to find the nearest dot while( (right < SPIRAL_SEARCH_SIZE) && (sol == false) ) { for (k=1; k <= right; k++) if(sol==false) { u_cog = 0 ; v_cog = 0 ; ip_connexities_list.clear() ; ip_edges_list.clear(); this->mean_gray_level = 0 ; if ( connexe(I, (unsigned int)u_+k, (unsigned int)(v_),mean_gray_level, u_cog, v_cog, npoint) ) { sol = true; u = u_+k; v = v_; } } u_ += k; right += 2; for (k=1; k <= botom; k++) if (sol==false) { u_cog = 0 ; v_cog = 0 ; ip_connexities_list.clear() ; ip_edges_list.clear(); this->mean_gray_level = 0 ; if ( connexe(I, (unsigned int)(u_), (unsigned int)(v_+k),mean_gray_level, u_cog, v_cog, npoint) ) { sol = true; u = u_; v = v_+k; } } v_ += k; botom += 2; for (k=1; k <= left; k++) if (sol==false) { u_cog = 0 ; v_cog = 0 ; ip_connexities_list.clear() ; ip_edges_list.clear(); this->mean_gray_level = 0 ; if ( connexe(I, (unsigned int)(u_-k), (unsigned int)(v_),mean_gray_level,u_cog, v_cog, npoint) ) { sol = true ; u = u_-k; v = v_; } } u_ -= k; left += 2; for (k=1; k <= up; k++) if(sol==false) { u_cog = 0 ; v_cog = 0 ; ip_connexities_list.clear() ; ip_edges_list.clear(); this->mean_gray_level = 0 ; if ( connexe(I, (unsigned int)(u_), (unsigned int)(v_-k),mean_gray_level,u_cog, v_cog, npoint) ) { sol = true ; u = u_; v = v_-k; } } v_ -= k; up += 2; } if (sol == false) { vpERROR_TRACE("Dot has been lost") ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot has been lost")) ; } } #endif /* vpImagePoint ip; unsigned int i, j; std::list<vpImagePoint>::iterator it; for (it = ip_connexities_list.begin(); it != ip_connexities_list.end(); it ++) { ip = *it; i = (unsigned int) ip.get_i(); j = (unsigned int) ip.get_j(); I[i][j] = 255 ; }*/ u_cog = u_cog/npoint ; v_cog = v_cog/npoint ; u = u_cog ; v = v_cog ; // Initialize the threshold for the next call to track() double Ip = pow((double)this->mean_gray_level/255,1/gamma); if(Ip - (1 - grayLevelPrecision)<0){ gray_level_min = 0 ; } else{ gray_level_min = (unsigned int) (255*pow(Ip - (1 - grayLevelPrecision),gamma)); if (gray_level_min > 255) gray_level_min = 255; } gray_level_max = (unsigned int) (255*pow(Ip + (1 - grayLevelPrecision),gamma)); if (gray_level_max > 255) gray_level_max = 255; //vpCTRACE << "gray_level_min: " << gray_level_min << std::endl; //vpCTRACE << "gray_level_max: " << gray_level_max << std::endl; if (npoint < 5) { vpERROR_TRACE("Dot to small") ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot to small")) ; } if (npoint > nbMaxPoint) { vpERROR_TRACE("Too many point %lf (%lf%%). Max allowed is %lf (%lf%%). This threshold can be modified using the setMaxDotSize() method.", npoint, npoint / (I.getWidth() * I.getHeight()), nbMaxPoint, maxDotSizePercentage) ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot to big")) ; } }
bool vpDot::connexe(const vpImage<unsigned char>& I,unsigned int u,unsigned int v, double &mean_value, double &u_cog, double &v_cog, double &n,std::vector<bool> &checkTab) { unsigned int width = I.getWidth(); unsigned int height= I.getHeight(); // Test if we are in the image if ( (u >= width) || (v >= height) ) { //std::cout << "out of bound" << std::endl; return false; } if(checkTab[u + v*I.getWidth()]) return true; vpImagePoint ip; ip.set_u(u); ip.set_v(v); if (I[v][u] >= gray_level_min && I[v][u] <= gray_level_max) { checkTab[v*I.getWidth() + u] = true; ip_connexities_list.push_back(ip); u_cog += u ; v_cog += v ; n+=1 ; if (n > nbMaxPoint) { vpERROR_TRACE("Too many point %lf (%lf%% of image size). " "This threshold can be modified using the setMaxDotSize() " "method.", n, n / (I.getWidth() * I.getHeight()), nbMaxPoint, maxDotSizePercentage) ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot to big")) ; } // Bounding box update if (u < this->u_min) this->u_min = u; if (u > this->u_max) this->u_max = u; if (v < this->v_min) this->v_min = v; if (v > this->v_max) this->v_max = v; // Mean value of the dot intensities mean_value = (mean_value *(n-1) + I[v][u]) / n; if (compute_moment==true) { m00++ ; m10 += u ; m01 += v ; m11 += (u*v) ; m20 += u*u ; m02 += v*v ; } } else { //std::cout << "not in" << std::endl; return false; } bool edge = false; //if((int)u-1 >= 0) if(u >= 1) if(!checkTab[u-1 + v*I.getWidth()]) if(!connexe(I,u-1,v, mean_value,u_cog,v_cog, n, checkTab)) edge = true; if(u+1 < I.getWidth()) if(!checkTab[u+1+v*I.getWidth()]) if(!connexe(I,u+1,v,mean_value,u_cog, v_cog, n, checkTab)) edge = true; if(v >= 1) if(!checkTab[u+(v-1)*I.getWidth()]) if(!connexe(I,u, v-1,mean_value,u_cog, v_cog, n, checkTab)) edge = true; if(v+1 < I.getHeight()) if(!checkTab[u+(v+1)*I.getWidth()]) if(!connexe(I,u,v+1,mean_value,u_cog, v_cog, n, checkTab)) edge = true; if (connexityType == CONNEXITY_8) { if(v >= 1 && u >= 1) if(!checkTab[u-1+(v-1)*I.getWidth()]) if(!connexe(I,u-1,v-1,mean_value,u_cog, v_cog, n, checkTab)) edge = true; if(v >= 1 && u+1 < I.getWidth()) if(!checkTab[u+1+(v-1)*I.getWidth()]) if(!connexe(I,u+1,v-1,mean_value,u_cog, v_cog, n, checkTab)) edge = true; if(v+1 < I.getHeight() && u >= 1) if(!checkTab[u-1+(v+1)*I.getWidth()]) if(!connexe(I,u-1,v+1,mean_value, u_cog, v_cog, n, checkTab)) edge = true; if(v+1 < I.getHeight() && u+1 < I.getWidth()) if(!checkTab[u+1+(v+1)*I.getWidth()]) if(!connexe(I,u+1,v+1,mean_value,u_cog, v_cog, n, checkTab)) edge = true; } if(edge){ ip_edges_list.push_back(ip); if (graphics==true) { vpImagePoint ip_(ip); for(unsigned int t=0; t<thickness; t++) { ip_.set_u(ip.get_u() + t); vpDisplay::displayPoint(I, ip_, vpColor::red) ; } //vpDisplay::flush(I); } } return true; }
bool vpDot::connexe(const vpImage<unsigned char>& I,unsigned int u,unsigned int v, double &mean_value, double &u_cog, double &v_cog, double &n) { std::vector<bool> checkTab(I.getWidth()*I.getHeight(),false); return connexe(I,u,v,mean_value,u_cog,v_cog,n,checkTab); }
/*! Perform the tracking of a dot by connex components. \param mean_value : Threshold to use for the next call to track() and corresponding to the mean value of the dot intensity. \warning The content of the image is modified thanks to setGrayLevelOut() called before. This method choose a gray level (default is 0) used to modify the "in" dot level in "out" dot level. This gray level is here needed to stop the recursivity . The pixels of the dot are set to this new gray level "\out\". \return vpDot::out if an error occurs, vpDot::in otherwise. \sa setGrayLevelOut() */ int vpDot::connexe(vpImage<unsigned char>& I, int u, int v, unsigned int gray_level_min, unsigned int gray_level_max, double &mean_value, double &u_cog, double &v_cog, double &n) { int width = I.getWidth(); int height= I.getHeight(); // Test if we are in the image if ( (u < 0) || (v < 0) || (u >= width) || (v >= height) ) { return vpDot::out ; } if (I[v][u] >= gray_level_min && I[v][u] <= gray_level_max) { vpImagePoint ip; ip.set_u(u); ip.set_v(v); if (graphics==true) { // printf("u %d v %d\n", u, v); vpDisplay::displayPoint(I, ip, vpColor::green) ; //vpDisplay::flush(I); } ip_edges_list += ip; u_cog += u ; v_cog += v ; n+=1 ; if (n > nbMaxPoint) { vpERROR_TRACE("Too many point %lf (%lf%% of image size). " "This threshold can be modified using the setMaxDotSize() " "method.", n, n / (I.getWidth() * I.getHeight()), nbMaxPoint, maxDotSizePercentage) ; throw(vpTrackingException(vpTrackingException::featureLostError, "Dot to big")) ; } // Bounding box update if (u < this->u_min) this->u_min = u; if (u > this->u_max) this->u_max = u; if (v < this->v_min) this->v_min = v; if (v > this->v_max) this->v_max = v; // if (graphics==true) // { // vpRect r(this->u_min, this->v_min, // this->u_max - this->u_min + 1, // this->v_max - this->v_min + 1); // vpDisplay::displayRectangle(I, r, vpColor::white) ; // } // Mean value of the dot intensities mean_value = (mean_value *(n-1) + I[v][u]) / n; if (compute_moment==true) { m00++ ; m10 += u ; m01 += v ; m11 += (u*v) ; m20 += u*u ; m02 += v*v ; } I[v][u] = this->gray_level_out ; } else { return vpDot::out ; } if ( u-1 >= 0) { if (I[v][u-1] >= gray_level_min && I[v][u-1] <= gray_level_max) connexe(I,u-1,v, gray_level_min, gray_level_max, mean_value, u_cog,v_cog, n) ; } if (u+1 < width) { if (I[v][u+1] >= gray_level_min && I[v][u+1] <= gray_level_max) connexe(I,u+1,v,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if (v-1 >= 0) { if (I[v-1][u] >=gray_level_min && I[v-1][u] <= gray_level_max) connexe(I,u, v-1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if (v+1 < height) { if (I[v+1][u] >=gray_level_min && I[v+1][u] <= gray_level_max) connexe(I,u,v+1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if (connexity == CONNEXITY_8) { if ( (u-1 >= 0) && (v-1 >= 0) ) { if (I[v-1][u-1] >=gray_level_min && I[v-1][u-1] <= gray_level_max) connexe(I,u-1,v-1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if ( (u+1 < width) && (v-1 >= 0 ) ) { if (I[v-1][u+1] >=gray_level_min && I[v-1][u+1] <= gray_level_max) connexe(I,u+1,v-1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if ( (v+1 < height) && (u-1 >= 0) ) { if (I[v+1][u-1] >=gray_level_min && I[v+1][u-1] <= gray_level_max) connexe(I,u-1,v+1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } if ( (v+1 < height) && (u+1 < width) ) { if (I[v+1][u+1] >=gray_level_min && I[v+1][u+1] <= gray_level_max) connexe(I,u+1,v+1,gray_level_min, gray_level_max, mean_value, u_cog, v_cog, n) ; } } return vpDot::in ; }