// Identifies different connected components in the input image and modifies the input image such that, // it deletes any components with size lesser than the threshold size // Also returns ncc (number of connected components) and ntcc (number of connected components with size greater than threshold) void thr_Connected_Component( char *im, int thr, int nx, int ny, int nz, int *ncc, int *ntcc) { int *labeled_im; int N,*CClabel,*CCsize; int nv; nv= nx*ny*nz; labeled_im=(int *)calloc(nv,sizeof(int)); //Allocate memory for the labeled connected component image if(thr <= 0) { printf("\n Threshold has to be greater than zero\n"); exit(0); } // creates a labeled image 'labeled_im', such that all voxels in the binary image which are zero // will remain zero in labeled image and all voxels which are non zero will have a distinct label based on pixel connectivity // N: Number of cluster // CClable: Lable number of each cluster // CCsize: Size of each cluster Connected_Components(im,nx,ny,nz,labeled_im,&N,&CClabel,&CCsize); //Find the connected components with size greater than threshold for(int i=0; i<nv; i++) im[i]=0; *ntcc = 0; for(int i=0; i<N; i++) if(CCsize[i]>=thr) ++*ntcc; int k=0; for(int i=0; i<nv; i++) { if(labeled_im[i]) { if(labeled_im[i]==CClabel[k] && CCsize[k]>=thr) im[i]=1; else if(labeled_im[i]<CClabel[N/2]) for(int j=0; j<N/2; j++) { if (labeled_im[i]==CClabel[j] && CCsize[j]>=thr) { im[i]=1; k=j; break; } } else for(int j=N/2; j<N; j++) { if (labeled_im[i]==CClabel[j] && CCsize[j]>=thr) { im[i]=1; k=j; break; } } } } *ncc = N; free(labeled_im); free(CClabel); free(CCsize); }
// Identifies connected components in input image and stores the location of voxels in each connected component in array L. // It also computes array S, where S[i] points to the location in L from where the index of voxels in ith connected component are stored // Also returns ncc (number of connected components) void Connected_Component_location(char *im, int nx, int ny, int nz, int *ncc, int **S, int **L, int **CCsize) { int *labeled_im; int *CClabel; int nv,np; int N; int nbv; // Number of non-zero voxels in input image char *imtemp; nv = nx*ny*nz; np = nx*ny; labeled_im=(int *)calloc(nv,sizeof(int)); // Allocate memory for the labeled connected component image // creates a labeled image 'labeled_im', such that all voxels in the binary image which are zero // will remain zero in labeled image and all voxels which are non zero will have a distinct label based on pixel connectivity // N: Number of cluster // CClable: Lable number of each cluster // CCsize: Size of each cluster Connected_Components(im,nx,ny,nz,labeled_im,&N,&CClabel,CCsize); nbv=0; for(int i=0; i<N; i++) nbv += (*CCsize)[i]; (*L)=(int *)calloc(nbv,sizeof(int)); // Allocate memory to store the voxel index (*S)=(int *)calloc(N,sizeof(int)); imtemp=(char *)calloc(nv,sizeof(char)); for(int i=0; i<nv; i++) imtemp[i]=im[i]; (*S)[0]=0; for(int i=1; i<N; i++) (*S)[i] = (*S)[i-1] + (*CCsize)[i-1]; // Store the location of voxels in each connected component int ii=0; for(int i=0; i<N; i++) { for(int j=0; j<nv; j++) { if(imtemp[j]) { if(CClabel[i]==labeled_im[j]) { (*L)[ii++]=j; imtemp[j]=0; } } } } *ncc = N; free(labeled_im); free(CClabel); free(imtemp); }
int main(void) { Build_Graph(Adj, N); Print_Adjacency_Matrix(Adj, N); Connected_Components(); Print_Disjoint_Sets(); if ( Same_Component(5, 7) ) printf("Yes\n"); else printf("No\n"); system("pause"); return 0; }
// Identifies different connected components in the input image and modifies the input image such that, // all the components are removed from the image except for one component, which has the maximum size // Also returns ncc (number of connected components) and maxsize (maximum component size) void max_Connected_Component( char *im, int nx, int ny, int nz, int *ncc, int *maxsize) { int *labeled_im; int N,L,*CClabel,*CCsize; int nv; nv=nx*ny*nz; labeled_im=(int *)calloc(nv,sizeof(int)); // Allocate memory for the labeled connected component image // creates a labeled image 'labeled_im', such that all voxels in the binary image which are zero // will remain zero in labeled image and all voxels which are non zero will have a distinct label based on pixel connectivity // N: Number of cluster // CClable: Lable number of each cluster // CCsize: Size of each cluster Connected_Components(im,nx,ny,nz,labeled_im,&N,&CClabel,&CCsize); *maxsize = 0; // Find max cluster size for(int i=0; i<N; i++) if(CCsize[i] > *maxsize) { *maxsize = CCsize[i]; L = CClabel[i]; } // Find the connected component with max size for(int i=0; i<nv; i++) if(im[i]) { if(labeled_im[i] == L) im[i] = 1; else im[i] = 0; } *ncc = N; free(labeled_im); free(CClabel); free(CCsize); }
// Identifies different connected components in the input image and modifies the input image such that, // it deletes any components with height lesser than the threshold height // Also returns ncc (number of connected components) and ntcc (number of connected components with height greater than threshold) void heightthr_Connected_Component(float *im, float thr, int nx, int ny, int nz, int *ncc, int *ntcc) { int *labeled_im; char *imtemp; int N,*CClabel,*CCsize; float *CCheight; int nv,np; nv = nx*ny*nz; np = nx*ny; labeled_im=(int *)calloc(nv,sizeof(int)); // Allocate memory for the labeled connected component image imtemp=(char *)calloc(nv,sizeof(char)); for(int i=0; i<nv; i++) if(im[i]) imtemp[i]=1; // creates a labeled image 'labeled_im', such that all voxels in the binary image which are zero // will remain zero in labeled image and all voxels which are non zero will have a distinct label based on pixel connectivity // N: Number of cluster // CClable: Lable number of each cluster // CCsize: Size of each cluster Connected_Components(imtemp,nx,ny,nz,labeled_im,&N,&CClabel,&CCsize); // Find the height of each connected component CCheight = (float *)calloc(N,sizeof(float)); for(int i=0; i<N; i++) CCheight[i]=0.0; int k=0; for(int i=0; i<nv; i++) { if(labeled_im[i]) { if(labeled_im[i]==CClabel[k] && im[i]>CCheight[k]) CCheight[k]=im[i]; else if(labeled_im[i] < CClabel[N/2]) for(int j=0; j<N/2; j++) { if (labeled_im[i]==CClabel[j] && im[i]>CCheight[j]) { CCheight[j]=im[i]; k=j; break; } } else for(int j=N/2; j<N; j++) { if (labeled_im[i]==CClabel[j] && im[i]>CCheight[j]) { CCheight[j]=im[i]; k=j; break; } } } } // Find the connected components with height greater than threshold for(int i=0; i<nv; i++) im[i]=0.0; *ntcc = 0; for(int i=0; i<N; i++) if(CCheight[i]>=thr) ++*ntcc; k=0; for(int i=0; i<nv; i++) { if(labeled_im[i]) { if(labeled_im[i]==CClabel[k] && CCheight[k]>=thr) im[i]=1.0; else if(labeled_im[i] < CClabel[N/2]) for(int j=0; j<N/2; j++) { if (labeled_im[i]==CClabel[j] && CCheight[j]>=thr) { im[i]=1.0; k=j; break; } } else for(int j=N/2; j<N; j++) { if (labeled_im[i]==CClabel[j] && CCheight[j]>=thr) { im[i]=1.0; k=j; break; } } } } *ncc = N; free(labeled_im); free(CClabel); free(CCsize); free(CCheight); free(imtemp); }