看看我做的非递归进行的连通性分析是不是有点创新性的东西
开始也是想用递归,总是堆栈溢出。后来网上找资料也没有找到很好用的办法。我自己在纸上分析了半天,弄了一个用循环做的连通区域分析算法,就是对二值的二维数据,同属一个连通区域的就分给同一个标号。我这样做出来的应该是8-连通的。我不知道这个方法以前有没有很规范的先例呢?大家有没有看到过有类似的算法呢?我这个方法是不是从逻辑上讲完全正确呢(虽然我自己对我的数据跑通了)?还有没有什么分析连通性更好、适用面广、方便编程、简单实用的算法可以介绍给我吗?
我是新手,多多指教!最近做毕业设计,才找到CSDN这个广阔的天地来!
/////////////////////////主函数中的主要部分:
for(i = 0; i < (m_ImgCol_Big * m_ImgRow_Big); i++)
{
if(pImgBig[i]>=70&&pImgBig[i]<=120) /////////////////////select the range for processing
pImgBig[i]=1; //currently,change them manually
else
pImgBig[i]=0;
}
////
GDALClose(m_pGDALDataset);
////
int label_count=1; //to count the number of labels
float *lpsrc; //point to the original image,whose regions of interest have been marked with 1
int *label; // a table with the same size of the image to store the labels of connected areas
int j; // i,j and k are loop variables
int k;
label=new int[m_ImgCol_Big * m_ImgRow_Big];
int *lpdest=label; //a pointer pointing to the element in the label table
int *label_change=label; //refresh some labels which are not the same
//with their neighbors in one connected domain
for(i=0;i<m_ImgRow_Big*m_ImgCol_Big;i++)//clear the label table
{
*lpdest=0;lpdest++;
}
for(i=1;i<=m_ImgRow_Big;i++) //scan all the elements
{
for(j=1;j<=m_ImgCol_Big;j++)
{
lpsrc=pImgBig+m_ImgCol_Big*(i-1)+j-1; //separately point to the same position
lpdest=label+m_ImgCol_Big*(i-1)+j-1; // in different tables
////////
if(*lpsrc==1) //element of regions of interest
{
if(i==1&&j==1) //the first element,special handling:if it's 1,give it a label directly
{
*lpdest=label_count;
label_count++;
}
else if(i==1) //the first row expect for the first one,special handling:
{ //only consider the element before it(to its left)
if(*(lpdest-1)!=0) //if its left one has a label,then give it the same label
*lpdest=*(lpdest-1);
else if(*(lpdest-1)==0) //if its left one is 0,give it a label directly
{
*lpdest=label_count;
label_count++;
}
}
else if(j==1) //the first column expect for the first one,special handling:
{
if(*(lpdest-m_ImgCol_Big)!=0) //only consider the element above it(on its top)
*lpdest=*(lpdest-m_ImgCol_Big);
else if(*(lpdest-m_ImgCol_Big)==0)
{
*lpdest=label_count;
label_count++;
}
}
else //the others who have 4 neighbors(left,topleft,top and topright)
{
if(*(lpdest-1)==0&&*(lpdest-m_ImgCol_Big-1)==0&&*(lpdest-m_ImgCol_Big)==0&&*(lpdest-m_ImgCol_Big+1)==0)
{
*lpdest=label_count; //its neighbors have no label,then give it a new label
label_count++;
}
else if(find_4neighbors_same((lpdest-1),(lpdest-m_ImgCol_Big-1),(lpdest-m_ImgCol_Big),(lpdest-m_ImgCol_Big+1)))
{
//its neighbors which have labels have a same label, then give it the same label
*lpdest=find_4neighbors_same((lpdest-1),(lpdest-m_ImgCol_Big-1),(lpdest-m_ImgCol_Big),(lpdest-m_ImgCol_Big+1));
}
else //its neighbors which have labels have different labels
{
int *p1=find_a_different_neighbor(lpdest-1,lpdest-m_ImgCol_Big-1); //left and(or) topleft
int *p2=find_a_different_neighbor(lpdest-m_ImgCol_Big,lpdest-m_ImgCol_Big+1);//top and(or) topright
int recordp1=*p1;
int recordp2=*p2;
//if an element's neighbors have different labels
//if its left or topleft has a label A,and its top or topright has a label B
//give it A as its label,and change all the label B to A.
//So its top or(and) topright neighbors have the same label with its left or(and) topleft neighbors
*lpdest=*p1;
for(k=0;k<=(i-1)*m_ImgCol_Big+j-1;k++)
{
label_change=label+k;
if(*label_change==recordp2)
*label_change=recordp1;
}
}
}
}
////////
}
}
printf("%d",label_count);
//change the discontinuous labels to continuous labels
//convenient for display and summary
//(the labels are not continuously used is a shortcoming of this algorithm, because we refresh
// some labels(and these labels are just used in the middle but not used actually in the end)
// to other values in order to ensure that connected areas have the same label)
int num[1000]; //an array to store different labels from small to large
int min_before=0; //the valid minimum label is 1
int min_current=1;
lpdest=label;
i=0; //ordinal number of num[]
while(1)
{
min_current=search_min(min_before,m_ImgCol_Big,m_ImgRow_Big,label); //find current minimum
num[i]=min_current;
i++;
if(min_before!=min_current) //termination of the loop
min_before=min_current;
else break;
}
for(j=0;j<i-1;j++) //change the original labels into new ones,refer to num[]
{
for(k=0;k<(m_ImgCol_Big * m_ImgRow_Big);k++)
{
if(*(lpdest+k)==num[j])
*(lpdest+k)=j+1;
}
}
int changed_label_count=j; //define a new label count to record the amount of used labels
// which means the amount of the connected regions
// printf("\n%d\n",i);
// for(j=0;j<i;j++)
// printf("%d\t",num[j]);
printf("\nchanged_label_count=%d",changed_label_count);
printf("\n");
//
// i=500;j=188;
// printf("center=%d",*(label+m_ImgCol_Big*(i-1)+j-1));
// printf("\nleft=%d",*(label+m_ImgCol_Big*(i-1)+j-1-1));
// printf("\ntopleft=%d",*(label+m_ImgCol_Big*(i-1)+j-1-m_ImgCol_Big-1));
// printf("\ntop=%d",*(label+m_ImgCol_Big*(i-1)+j-1-m_ImgCol_Big));
// printf("\ntopright=%d",*(label+m_ImgCol_Big*(i-1)+j-1-m_ImgCol_Big+1));
///////////////////////////