⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig2.cpp

📁 图像处理源代码VC
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	for(i=-M2;i<=M2;i++)
	{
		CMask[i+M2]=SK*exp(-i*i/(2*sigma*sigma));
		RMask[i+M2]=(1-i*i/sigma*sigma)*CMask[i+M2];
	}
	for(k=0;k<row;k++)
		for(l=0;l<col;l++)
		{
			sum=0;
			for(i=-M2;i<=M2;i++)
			{
				sum1=0;
				for(j=-M2;j<=M2;j++)
				{
					if(k+i>=0&&l+j>=0&&k+i<row&&l+j<col)
						sum1+=(long)img[k+i][l+j];
					else
					{
						if((ki=k+i)<0) ki=0;
						if((lj=l+j)<0) lj=0;
						if(ki>=row) ki=row-1;
						if(lj>=col) lj=col-1;
						sum1+=(long)img[ki][lj];
					}
				}
				sum+=(sum1/M)*(long)RMask[i+M2];
			}
			for(j=-M2;j<=M2;j++)
			{
				sum1=0;
				for(i=-M2;i<=M2;i++)
				{
					if(k+i>=0&&l+j>=0&&k+i<row&&l+j<col)
						sum1+=(long)img[k+i][l+j];
					else
					{
						if((ki=k+i)<0) ki=0;
						if((lj=l+j)<0) lj=0;
						if(ki>=row) ki=row-1;
						if(lj>=col) lj=col-1;
						sum1+=(long)img[ki][lj];
					}
				}
				sum+=(sum1/M)*(long)CMask[j+M2];
			}
			Ilog[k][l]=sum/M;
		    //  printf("\r%d  %d  %d",k,l,Ilog[k][l]);
		}
	free(CMask);
	free(RMask);
	return(Ilog);
}
/**************************************************************
Function:			detect zero_crossing in a Ilog_image.
Parameter:
					<Ilog>:the array of the Ilog image which is derived from
							the origin image by convovle;
					<row>:the row of Ilog image;
					<col>:the column of Ilog image.
Call by   :		main().
*******************************************************************/
unsigned char **ZeroDetect1(int **Ilog,int row,int col)
{
	int i,j;
	unsigned char **Iz;
	Iz=(unsigned char **)fspace_2d(row,col,sizeof(unsigned char));
	if(Iz==NULL)
	{
	 //	printf("Alloc error");
		return NULL;
	}
	for(i=0;i<row;i++)
	for(j=0;j<col;j++)
	{
		if(Ilog[i][j]==0)
		{
			Iz[i][j]=240;
			continue;
		}
		if(Ilog[i][j+1]>0&&Ilog[i][j]<0||Ilog[i][j+1]<0&&Ilog[i][j]>0)
			Iz[i][j]=240;
		if(Ilog[i+1][j]>0&&Ilog[i][j]<0||Ilog[i+1][j]<0&&Ilog[i][j]>0)
			Iz[i][j]=240;
	}
	return(Iz);
}

/**************************************************************
Function:			detect zero_crossing in a Ilog_image.
Parameter:
					<Ilog>:the array of the Ilog image which is derived from
							the origin image by convovle;
					<row>:the row of Ilog image;
					<col>:the column of Ilog image.
Call by :		main().
*******************************************************************/
unsigned char **ZeroDetect(int **Ilog,int row,int col)
{
	int i,j,k,b,c,d,e,f,g,h;
	unsigned char *bz,*dz,*ez,*fz,*hz;
	unsigned char **Iz;
	Iz=(unsigned char **)fspace_2d(row,col,sizeof(unsigned char));
	if(Iz==NULL)
	{
	  //	printf("Alloc error");
		return NULL;
	}
	for(i=0;i<row;i++)
	for(j=0;j<col;j++)
	{
		if(i==0||j==0||i==row-1||j==col-1)
			Iz[i][j]=0;
		else
		{
			b=Ilog[i][j-1];
			c=Ilog[i+1][j-1];
			d=Ilog[i-1][j];
			e=Ilog[i][j];
			f=Ilog[i+1][j];
	 g=Ilog[i-1][j+1];
			h=Ilog[i][j+1];
			bz=&Iz[i][j-1];
			dz=&Iz[i-1][j];
			ez=&Iz[i][j];
			fz=&Iz[i+1][j];
			hz=&Iz[i][j+1];

			if(e==0)
			{
				if(d>0&&f<0)
				{
					if(b>0&&h<0)
						*ez=5;
					else if(b<0&&h>0)
						*ez=7;
					else *ez=6;
					continue;
				}
				else if(d<0&&f>0)
				{
					if(b>0&&h<0)
						*ez=3;
					else if(b<0&&h>0)
						*ez=1;
					else *ez=6;
					continue;
				}
				else if(b>0&&h<0)
//					if(d>0&&f>0||d<0&&f<0)
				{
					*ez=4;
					continue;
				}
				else
				{
					*ez=8;
					continue;
				}
			}
			else
			{
				if(e>0&&d<0&&f>0)
				{
					if(b>0&&h<0)
					{
						if(abs(h)<e)
							*hz=3;
						if(abs(d)<e)
							*dz=3;
						else *ez=3;
					}
					else if(b<0&&h)
					{
						if(abs(b)<e)
							*bz=1;
						if(abs(d)<e)
							*dz=1;
						else *ez=1;
					}
					else if(b>0&&h)
					{
						if(abs(d)<e)
							*dz=2;
					}
					else *ez=2;
				}
				else if(e>0&&d<0&&f<0)
				{
					if(b>=0&&h<0)
					{
						if(abs(b)<e)
						{	*hz=3;continue;}
						else
						{
							*ez=4;
							*fz=4;
							continue;
						}
					}
					else if(b<0&&h>=0)
					{
							*ez=8;
							*fz=8;
							continue;
					}
					else if(abs(d)<e)
					{
						*dz=2;
						continue;
					}
					else
					{
						*ez=2;
						*fz=6;
						continue;
					}
				}
				else if(b<0&&e>0&&h>0)
				{
					if(d>0&&abs(b)<e)
					{
						if(f>0) *bz=8;
						else if(f<0) *bz=7;
						continue;
					}
					else if(d>0)
					{
						if(f<0&&abs(f)<e) *fz=7;
						else if(f<0&&abs(f)>e) *ez=7;
						continue;
					}
					else *ez=8;
					continue;
				}
				else if(b<0&&e>0&&h<0)
				{
					if(f<0)
					{
						if(g==0) *hz=5;
						else if(g>0) *hz=6;
						else
						{
							*ez=6;
							*hz=8;
						}
						continue;
					}
					else
					{
						*hz=4;
						continue;
					}
				}
				else if(e>0&&f<0&&h>0)
				{
					if(abs(f)<e) *fz=6;
					else *ez=6;
					continue;
				}
				else if(e>0&&f<0&&h<0)
				{
					if(abs(h)<e) *hz=4;
					else if(abs(h)>=e) *ez=5;
					else if(c>0&&abs(f)<e) *fz=5;
					else if(c>0&&abs(f)>=e) *ez=5;
					else if(g>0) *hz=5;
					continue;
				}
				else if(b>0&&d>0&&e>0&&f>0&&h<0)
				{
					if(abs(h)<e) *hz=4;
					else *ez=4;
				}
			}
		}
		if(Iz[i][j]) Iz[i][j]=240;
	}
	return(Iz);
}
/***************************************************************************
		This program can be used to extract the edge of	a grey image,

Usage:logm <source image file > row col param1 param2  sigma
		param1=1:convovling with function Convovle1D;
		param1=2:convovling with function Convovle2D;
		param2=1:detect zero_crossing with function ZeroDetect;
		param2=2:detect zero_crossing with function ZeroDetect1;
Programer:Liu Hongbo

************************************************************************/
int logm(unsigned char **Iorig,unsigned char **Izero,
          int row,int col,int a,int b,float sigma)
{
	int k;
	int **Ilog;

	if(a==1)
		Ilog=Convolve1D((unsigned char **)Iorig,row,col,sigma);
	else
		Ilog=Convolve2D((unsigned char **)Iorig,row,col,sigma);
	if(Ilog==NULL)
	{
		return 0;
	}
	if(b==1)
	{
		Izero=(unsigned char **)ZeroDetect(Ilog,row,col);
		if(Izero==NULL)
		{
			return 0;
		}
	}
	else
	{
		Izero=(unsigned char **)ZeroDetect1(Ilog,row,col);
		if(Izero==NULL)
		{
			return 0;
		}
	}
	ffree_2d((void **)Ilog,row);
	return 1;
}


void LoGMask(int *ha,int *hb,int HalfWidth,float sigma,int K)
{
    float fun,fun2;
    int i;
    for(i=0;i<HalfWidth;i++)
    {
      fun=i*i;
      fun2=fun;
      fun=(1-fun/(sigma*sigma))*exp(-fun/(2.*sigma*sigma));
      ha[i]=(int)(fun*sqrt(K));
      fun2=exp(-fun2/sigma);
      hb[i]=(int)(sqrt(K)*fun2);
      //printf("a[%d]:%d,b[%d]:%d  ",i,ha[i],i,hb[i]);
    }//end for(i)
}


/* return:
	  0---No enough memory to allocate!
	  1---sucess
*/
int GaussFilter(unsigned char ** imgptr,int nHeight,int nWidth,float
				 sigma,int K,/*int huge*/long ** s2)
{
   int i,j,k;
//   void LoGMask(int *ha,int *hb,int HalfWidth,float sqrsigma,int K);
   long sum;
   int DIM=(int)(3*sqrt(2)*sigma+0.5);
   int * a=new int[DIM];
   int * b=new int[DIM];
   /*int  huge*/long ** s1=(/*int huge*/long **)fspace_2d(nHeight,nWidth,sizeof(int));
   int  * rowline=new int[nWidth+2*DIM];
   int  * colline=new int[nHeight+2*DIM];

   if(s1==NULL||a==NULL||b==NULL||rowline==NULL||colline==NULL)
   {
      ffree_2d((void**)s1,nHeight);
      delete a;
      delete b;
      delete rowline;
      delete colline;
		return 0;
   }

   LoGMask(a,b,DIM,sigma,K);

//Initilizing...
   for(i=0;i<DIM;i++){
	  rowline[i]=0;rowline[nWidth+i]=0;
     colline[i]=0;colline[nHeight+i]=0;
   }//end for

//printf(" The first translation...\n");
//printf("    Row Translation...\n");
  for(i=0;i<nHeight;i++)
  {
    for(k=DIM;k<DIM+nWidth;k++) rowline[k]=imgptr[i][k-DIM];
    for(j=0;j<nWidth;j++)
    {
       sum=a[0]*rowline[j+DIM];
       for(k=1;k<DIM;k++)
    	  sum+=(long)(a[k]*(rowline[j+DIM-k]+rowline[j+DIM+k]));
       s1[i][j]=sum;
    }//end for(j);
  }//end for(i)

//printf("    Column Translation...\n");
  for(j=0;j<nWidth;j++)
  {
     for(k=DIM;k<DIM+nHeight;k++) colline[k]=s1[k-DIM][j];
     for(i=0;i<nHeight;i++)
     {
	    sum=b[0]*colline[i+DIM];
    	 for(k=1;k<DIM;k++)
    	  sum+=(long)(b[k]*(colline[i+DIM+k]+colline[i+DIM-k]));
	    s2[i][j]=sum;
     }//end for(i)
  }//end for(j);

//printf("The second translation...\n");
//Initilizing...
  for(i=0;i<DIM;i++){
   	rowline[i]=0;rowline[nWidth+i]=0;
	   colline[i]=0;colline[nHeight+i]=0;
  }//end for

// printf("    Row translation...\n");
// Row translation...
  for(i=0;i<nHeight;i++)
  {
      for(k=DIM;k<DIM+nWidth;k++) rowline[k]=imgptr[i][k-DIM];
      for(j=0;j<nWidth;j++)
      {
    	 sum=b[0]*rowline[j+DIM];
	    for(k=1;k<DIM;k++)
    	  sum+=(long)(b[k]*(rowline[j+DIM-k]+rowline[j+DIM+k]));
   	 s1[i][j]=sum;
      }//end for(j);
  }//end for(i)

//printf("    column translation...\n");
  for(j=0;j<nWidth;j++)
  {
     for(k=DIM;k<DIM+nHeight;k++) colline[k]=s1[k-DIM][j];
     for(i=0;i<nHeight;i++)
     {
   	 sum=a[0]*colline[i+DIM];
	    for(k=1;k<DIM;k++)
   	  sum+=(long)(a[k]*(colline[i+DIM+k]+colline[i+DIM-k]));
   	 s2[i][j]+=sum;
     }//end for(i)
  }//end for(j);

//printf("Done!\n");

 ffree_2d((void**)s1,nHeight);
 delete a;
 delete b;
 delete rowline;
 delete colline;

 return 1;
}

/* return:
	  0---No enough memory to allocate!
	  1---sucess
*/
int LogAlg(unsigned char ** Inimg,
			  unsigned char ** img,
			  int row,int col,
			  float sigma,int k)
{
   int i,j;
   int max=-10000,min=10000;
   /*int  huge*/long ** edgedimg;
	 int ret;

	 edgedimg=(/*int huge*/long **)fspace_2d(row,col,sizeof(int));
   if(edgedimg==NULL||img==NULL)
   {
      ffree_2d((void**)edgedimg,row);
			fatal_error("No enough space!");
      return 0;
	 }

	 ret=GaussFilter(Inimg,row,col,sigma,k,edgedimg);
	 if(ret==0)
	 {
      ffree_2d((void**)edgedimg,row);
			fatal_error("GaussFilter Error!");
      return 0;
	 }

	 for(i=0;i<row;i++)
	 for(j=0;j<col;j++)
   {
   	 max=edgedimg[i][j]>max?edgedimg[i][j]:max;
	   min=edgedimg[i][j]<min?edgedimg[i][j]:min;
   }

/*
   cout<<endl<<"Do you want filted Image without edge tracing?(Y/N)";
   cin>>YesNo;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -