简易分水岭分割

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include
#include
#define row 125
#define col 123
using namespace std;
struct POINT
{
int x;
int y;
};//定义POINT结构体
typedef int BOOL;//定义BOOL为int类型
void watershed(unsigned char originalimage[row][col],char seedimage[row][col],int labelimage[row][col])
{
vector seedcounts;//定义保存种子的数组
queue quetem;//临时种子队列
queue* pque;
vector*> vque;//保存所有标记区域种子队列的数组
// memset(labelimage,0,(row*col));
// memset(seedimage,0,(row*col));
BOOL up,down,left,right,upleft,upright,downleft,downright;
POINT temp;
int m,n;
int NUM=0;
int* array;
int i,j;
for(i=0;i{
for(j=0;j{
if(seedimage[i][j]==1)
{
NUM++;
array=new int[256];
seedcounts.push_back(array);
pque=new queue[256];
vque.push_back(pque);
temp.x=i;
temp.y=j;
quetem.push(temp);
labelimage[i][j]=NUM;
seedimage[i][j]=127;
while(!quetem.empty())//判断临时种子队列非空
{
up=down=right=left=false;
upleft=upright=downleft=downright=false;
temp=quetem.front();
m=temp.x;
n=temp.y;
quetem.pop();
if(m>0)
{
if(seedimage[m-1][n]==1)
{
temp.x=m-1;
temp.y=n;
quetem.push(temp);
labelimage[m-1][n]=NUM;
seedimage[m-1][n]=127;
}

else
{
up=true;
}
}
if(m>0&&n>0)
{
if(seedimage[m-1][n-1]==1)
{
temp.x=m-1;
temp.y=n-1;
quetem.push(temp);
labelimage[m-1][n-1]=NUM;
seedimage[m-1][n-1]=127;
}

else
{
upleft=true;
}
}
if(m{
if(seedimage[m+1][n]==1)
{
temp.x=m+1;
temp.y=n;
quetem.push(temp);
labelimage[m+1][n]=NUM;
seedimage[m+1][n]=127;
}

else
{
down=true;
}
}
if(m<(row-1)&&n<(col-1))
{
if(seedimage[m+1][n+1]==1)
{
temp.x=m+1;
temp.y=n+1;
quetem.push(temp);
labelimage[m+1][n+1]=NUM;
seedimage[m+1][n+1]=127;
}

else
{
downright=true;
}
}
if(m{
if(seedimage[m][n+1]==1)
{
temp.x=m;
temp.y=n+1;
quetem.push(temp);
labelimage[m][n+1]=NUM;
seedimage[m][n+1]=127;
}

else
{
right=true;
}
}
if(m>0&&n<(col-1))
{
if(seedimage[m-1][n+1]==1)
{
temp.x=m-1;
temp.y=n+1;
quetem.push(temp);
labelimage[m-1][n+1]=NUM;
seedimage[m-1][n+1]=127;
}

else
{
upright=true;
}
}
if(n>0)
{
if(seedimage[m][n-1]==1)
{
temp.x=m;
temp.y=n-1;
quetem.

push(temp);
labelimage[m][n-1]=NUM;
seedimage[m][n-1]=127;
}

else
{
left=true;
}
}
if(m<(row-1)&&n>0)
{
if(seedimage[m+1][n-1]==1)
{
temp.x=m+1;
temp.y=n-1;
quetem.push(temp);
labelimage[m+1][n-1]=NUM;
seedimage[m+1][n-1]=127;
}

else
{
downleft=true;
}
}
if(up||down||right||left||upleft||downleft||upright||downright)
{
temp.x=m;
temp.y=n;
vque[NUM-1][originalimage[m][n]].push(temp);

}
}//while结束
}//if结束
}//for结束
}//for结束/初始集水盆完成
//bool actives;
int waterlevel;
for(waterlevel=0;waterlevel<256;waterlevel++)
{
// actives=true;
// while(actives)
// actives=false;
for(i=0;i{
while(!vque[i][waterlevel].empty())
{
//actives=true;
//if(seedcounts[i][waterlevel]>0)
{
//seedcounts[i][waterlevel]--;
temp=vque[i][waterlevel].front();
m=temp.x;
n=temp.y;
vque[i][waterlevel].pop();
if(m>0)
{
if(!labelimage[m-1][n])
{
temp.x=m-1;
temp.y=n;
labelimage[m-1][n]=i+1;
if(originalimage[m-1][n]<=waterlevel)
{
vque[i][waterlevel].push(temp);
}
else
{
vque[i][originalimage[m-1][n]].push(temp);

}
}
}
if(m{
if(!labelimage[m+1][n])
{
temp.x=m+1;
temp.y=n;
labelimage[m+1][n]=i+1;
if(originalimage[m+1][n]<=waterlevel)
{
vque[i][waterlevel].push(temp);

}
else
{
vque[i][originalimage[m+1][n]].push(temp);

}
}
}
if(n{
if(!labelimage[m][n+1])
{
temp.x=m;
temp.y=n+1;
labelimage[m][n+1]=i+1;
if(originalimage[m][n+1]<=waterlevel)
{
vque[i][waterlevel].push(temp);
}
else
{
vque[i][originalimage[m][n+1]].push(temp);

}
}
}
if(n>0)
{
if(!labelimage[m][n-1])
{
temp.x=m;
temp.y=n-1;
labelimage[m][n-1]=i+1;
if(originalimage[m][n-1]<=waterlevel)
{
vque[i][waterlevel].push(temp);

}
else
{
vque[i][originalimage[m][n-1]].push(temp);

}
}
}
}//while

}//if
}//for
}//for
};//watershed结束
int main()
{
int i,j;
unsigned char p;
unsigned char ori_image[row][col];
FILE*fp;
fp=fopen("C:\\Users\\CM\\Desktop\\one","rb+");
if(fp==NULL)
{
printf("错误!");
exit(1);
}
for(i=0;i{
for(j=0;j{

fread(&p,sizeof(unsigned char),1,fp);
ori_image[i][j]=p;
}
}
fclose(fp);
char seed_image[row][col];
memset(seed_image,0,(row*col)*sizeof(char));

int label_image[row][col];
memset(label_image,0,(row*col)*sizeof(int));
for(i=0;i{
for(j=0;j{
if(ori_image[i][j]<35)
seed_image[i][j]=1;
}
}
watershed(ori_image,seed_image,label_image);
FILE *fpp;
fpp=fopen("C:\\Users\\CM\\Desktop\\data","wb");
for(i=0;i{
for(j=0;j{

fwrite(&label_image[i][j],sizeof(int),1,fpp);
}
}
fclose(fpp);
return 0;
}

相关文档
最新文档