图像周长

writen by [email protected]

void   Cbiaoji1::zhouchang(IplImage  * src)
{
    IplImage 
*imgZhouchang=NULL;
 biaoji(src);
 imgZhouchang
=cvCreateImage(cvSize(src->width,src->height),8,1);
 
for(int j=0;j<imgZhouchang->height;j++)
        
for(int i=0;i<imgZhouchang->width;i++)
        
{
            CV_IMAGE_ELEM(imgZhouchang,uchar,j,i)
=255;
        }

 
long pixel;
 
bool bFindStartPoint;
 
bool bFindPoint;
 CvPoint StartPoint,CurrentPoint;
 
int Direction[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
 
int BeginDirect;
 
for(int t=1;t<=x_sign;t++)
 
{
     
if(flag[t]!=0)
     
{
         bFindStartPoint
=false;
         
for(int j=0;j<src->height && !bFindStartPoint;j++)
         
{
             
for(int i=0;i<src->width && !bFindStartPoint;i++)
                
             
{
                 
if(CV_IMAGE_ELEM(temp,uchar,j,i)==t)
                 
{
                     bFindStartPoint
=TRUE;
                     StartPoint.y
=j;
                     StartPoint.x
=i;
                     CV_IMAGE_ELEM(imgZhouchang,uchar,j,i)
=t;
                 }

             }

         }

         BeginDirect
=0;
         bFindStartPoint
=false;
         CurrentPoint.y
=StartPoint.y;
         CurrentPoint.x
=StartPoint.x;
         
while(!bFindStartPoint)
         
{
             bFindPoint
=false;
             
while(!bFindPoint)
             
{
                 pixel
=CV_IMAGE_ELEM(temp,uchar,CurrentPoint.y+Direction[BeginDirect][1],CurrentPoint.x+Direction[BeginDirect][0]);
                
                 
if(pixel==t)
                 
{
                     bFindPoint
=true;
                     CurrentPoint.y
=CurrentPoint.y+Direction[BeginDirect][1];
                     CurrentPoint.x
=CurrentPoint.x+Direction[BeginDirect][0];
                     
if(CurrentPoint.y==StartPoint.y && CurrentPoint.x==StartPoint.x)
                     
{
                         bFindStartPoint
=true;
                     }

                     CV_IMAGE_ELEM(imgZhouchang,uchar,CurrentPoint.y,CurrentPoint.x)
=t;
                     BeginDirect
--;
                     
if(BeginDirect==-1)
                         BeginDirect
=7;
                     BeginDirect
--;
                     
if(BeginDirect==-1)
                         BeginDirect
=7;
                 }

                 
else
                 
{
                     BeginDirect
++;
                     
if (BeginDirect==8)
                         BeginDirect
=0;
                 }


             }

         }


     }

     
 }

 
int x_line=0;
int fm[255]={0};
memset(fm,
0,255);
for(j=0;j< imgZhouchang->height;j++)
for(int i=0;i< imgZhouchang->width;i++)
{
    
if(CV_IMAGE_ELEM( imgZhouchang,uchar,j,i)!=0)
    
{
        x_line
=CV_IMAGE_ELEM( imgZhouchang,uchar,j,i);
        
++fm[x_line];
    }

}

memset(fn,
0,255);
int y_line=0;
int m_line=0;
for(int i=0;i<=x_sign;i++)
{
    
if(fm[i]!=0)
    
{
        
if(fn[y_line]==0)
        
{
            fn[y_line]
=fm[i];
            
++y_line;
        }

    }

    m_line
+=fm[i];
}

cout
<<"连通区域个数"<<y_line<<endl;
cout
<<"连通区域的总积"<<m_line<<endl;
for(i=0;i<y_line;i++)
{
    cout
<<"连通区域:"<<i+1<<"该区域周长:"<<fn[i]<<endl;
}

cvCopy(imgZhouchang,src);
    cvReleaseImage(
&temp);
        cvReleaseImage(
&imgZhouchang);
 
}

你可能感兴趣的:(图像)