自己coding的“种子填充算法”,能优化的给100分!!!tks

pauper 北京艾科诺斯科技有限责任公司 图像处理工程师  2014-10-30 08:06:35
//扫描线种子填充算法
bool AbdomenRecognition::imfill(int* pDst, const int area_thv,const int* pSrc,
const int nW, const int nH)
{
bool hole_flag;
bool rtn_flag=true;
int fill_num;
int m,n,space0;
int x_left,x_right;
int i,j,space;
int *pComplement=NULL;
int *preData=NULL;
A_GEPOINT_INT start_seed,seed,new_seed;
STACK stk;

pComplement=new int[nW*nH];
memset(pComplement,0,nW*nH*sizeof(int));
for(i=0;i<nH;i++)
{
space=i*nW;
for(j=0;j<nW;j++)
{
if(pSrc[space+j]==0){pComplement[space+j]=1;}
else{pComplement[space+j]=0;}
}
}

preData=new int[nW*nH];
memcpy(preData,pComplement,nW*nH*sizeof(int));
start_seed.x=0;
start_seed.y=0;
for(i=1;i<nH;i++)
{
space=i*nW;
for(j=1;j<nW;j++)
{
if(preData[space+j]==1 && preData[space+j-1]==0 && preData[space-nW+j]==0)
{
//1.get original seed.
fill_num=0;
start_seed.x=j;
start_seed.y=i;
hole_flag=true;
stackInit(&stk);
rtn_flag=push(&stk,start_seed);
if(rtn_flag!=true)
{
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
while(!empty(&stk))
{
//2.ext left and right by seed.
seed=stk.data[stk.top];
if(seed.y==0 || seed.y==nH-1)
{
hole_flag=false;
break;
}
pop(&stk);
for(m=seed.x;m>=0;m--)
{
if(preData[seed.y*nW+m]==1 && m!=0)
{
preData[seed.y*nW+m]=2;
fill_num++;
}
else if(preData[seed.y*nW+m]==0 && m!=0)
{
x_left=m+1;
break;
}
else if(m==0)
{
hole_flag=false;
break;
}
else
{continue;}
}
for(m=seed.x;m<nW;m++)
{
if(preData[seed.y*nW+m]==1 && m!=nW-1)
{
preData[seed.y*nW+m]=2;
fill_num++;
}
else if(preData[seed.y*nW+m]==0 && m!=nW-1)
{
x_right=m-1;
break;
}
else if(m==nW-1)
{
hole_flag=false;
break;
}
else
{continue;}
}

//3.processing up and down lines.
if(hole_flag==false)
{break;}
if(preData[(seed.y-1)*nW+x_right+1]==1)
{
new_seed.x=x_right+1;
new_seed.y=seed.y-1;
rtn_flag=push(&stk, new_seed);
if(rtn_flag!=true)
{
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
}
for(m=x_right;m>=x_left-1;m--)
{

if(preData[(seed.y-1)*nW+m+1]==0 && preData[(seed.y-1)*nW+m]==1)
{
new_seed.x=m;
new_seed.y=seed.y-1;
rtn_flag=push(&stk, new_seed);
if(rtn_flag!=true)
{
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
}
}
if(preData[(seed.y+1)*nW+x_right+1]==1)
{
new_seed.x=x_right+1;
new_seed.y=seed.y+1;
rtn_flag=push(&stk, new_seed);
if(rtn_flag!=true)
{
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
}
for(m=x_right;m>=x_left-1;m--)
{
if(preData[(seed.y+1)*nW+m+1]==0 && preData[(seed.y+1)*nW+m]==1)
{
new_seed.x=m;
new_seed.y=seed.y+1;
rtn_flag=push(&stk, new_seed);
if(rtn_flag!=true)
{
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
}
}
}

//4.fill 0s to holes.
if(hole_flag==false || fill_num>area_thv)
{
for(m=0;m<nH;m++)
{
space0=m*nW;
for(n=0;n<nW;n++)
{
if(preData[space0+n]==2)
{preData[space0+n]=1;}
}
}
}
else
{
for(m=0;m<nH;m++)
{
space0=m*nW;
for(n=0;n<nW;n++)
{
if(preData[space0+n]==2)
{preData[space0+n]=0;}
}
}
}
}
}
}

for(i=0;i<nH;i++)
{
space=i*nW;
for(j=0;j<nW;j++)
{
if(preData[space+j]==0){pDst[space+j]=1;}
else{pDst[space+j]=0;}
}
}
delete[] pComplement;
pComplement=NULL;
delete[] preData;
preData=NULL;
return rtn_flag;
}
...全文
288 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wjw2586121 2014-11-21
opencv的cvFloodFill 速度也是非常快,我测试过 http://bbs.rongkevision.com/forum.php?mod=viewthread&tid=40&extra=
回复
wjw2586121 2014-11-21
使用广度优先算法不知道有没有比较快,这是我网上看到的例子 http://bbs.rongkevision.com/forum.php?mod=viewthread&tid=39&extra=
回复
pauper 2014-10-30
先顶一下!
回复
相关推荐
发帖
机器视觉
创建于2007-08-27

4020

社区成员

图形图像/机器视觉
申请成为版主
帖子事件
创建了帖子
2014-10-30 08:06
社区公告
暂无公告