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

📄 处理..c

📁 matlab 例程
💻 C
📖 第 1 页 / 共 4 页
字号:
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);
   
prewittD0(Image[1],0,0,Image[1]->wid,Image[1]->hei,'M');

   WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}






//梯度函数//
void Gradiant0(BYTE **list1,BYTE **list0,int Dx,int Dy,char ch)
{
  int  i,j,A,B,C;
  
  for (i=0;i<Dy-1;i++) {
    for (j=0;j<Dx-1;j++) {
      if (ch=='R') {
	    A = abs(list1[i][j]-list1[i+1][j+1]);
	    B = abs(list1[i+1][j]-list1[i][j+1]);
        C = A + B;
	  }
      else {
	    A = abs(list1[i][j]-list1[i][j+1]);
	    B = abs(list1[i][j]-list1[i+1][j]);
 	    switch(ch) {
	      case 'M' :
            C = A + B;
            break;
	      case 'H' :
	        C = A;
	        break;
	      case 'V' :
	        C = B;
	        break;
	      default :
	        break;
		}
	  }
      C *= Scale;
	  if (C>255) C=255;
  	  list0[i][j] = C;
	}
  }
}
void GradiantD0(struct IMAGE *image,int x,int y,int Dx,int Dy,char ch)
{
  BYTE *buf1,*buf0;
  BYTE **list1,**list0;
  int  i;
  
  buf1 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  buf0 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
 getimage(image,x,y,Dx,Dy,buf1);                    
  list1=(BYTE**) malloc(Dy*sizeof(BYTE*));       
  list0=(BYTE**) malloc(Dy*sizeof(BYTE*));       
  for(i=0;i<Dy;i++) {
    list1[i]=buf1+i*Dx;
	list0[i]=buf0+i*Dx;
  }

  Gradiant0(list1,list0,Dx,Dy,ch);

  for(i=0;i<Dy-1;i++) 
   setimage(image,x+1,y+i,Dx-2,1,&list0[i][1]);   
   
  free(buf1);  
  free(buf0);  
  free(list1);  
  free(list0);  
}
//梯度边缘检测................................//
void EX8(HWND hWnd)
{   if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);                                                        
   Scale=2;
   GradiantD0(Image[1],0,0,Image[1]->wid,Image[1]->hei,'M');
WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}
//Roberts边缘检测.......................//
void EX9(HWND hWnd)
{   if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);                                                      
   Scale=2;
   GradiantD0(Image[1],0,0,Image[1]->wid,Image[1]->hei,'R');
WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}







//方向检测算子

int  Tkirsch[72]={
     5,5,5,-3,0,-3,-3,-3,-3,
     5,5,-3,5,0,-3,-3,-3,-3,
     5,-3,-3,5,0,-3,5,-3,-3,
     -3,-3,-3,5,0,-3,5,5,-3,
     -3,-3,-3,-3,0,-3,5,5,5,
     -3,-3,-3,-3,0,5,-3,5,5,
     -3,-3,5,-3,0,5,-3,-3,5,
     -3,5,5,-3,0,5,-3,-3,-3 };
 
//   Robinson 算子 
int  Trobinson[72]={
     1,2,1,0,0,0,-1,-2,-1,
     2,1,0,1,0,-1,0,-1,-2,
     1,0,-1,2,0,-2,1,0,-1,
     0,-1,-2,1,0,-1,2,1,0,
     -1,-2,-1,0,0,0,1,2,1,
     -2,-1,0,-1,0,1,0,1,2,
     -1,0,1,-2,0,2,-1,0,1,
     0,1,2,-1,0,1,-2,-1,0 };

//   Prewitt 算子 
int  Tprewitt[72]={
     1,1,1,1,-2,1,-1,-1,-1,
     1,1,1,1,-2,-1,1,-1,-1,
     1,1,-1,1,-2,-1,1,1,-1,
     1,-1,-1,1,-2,-1,1,1,1,
     -1,-1,-1,1,-2,1,1,1,1,
     -1,-1,1,-1,-2,1,1,1,1,
     -1,1,1,-1,-2,1,-1,1,1,
     1,1,1,-1,-2,1,-1,-1,1 };
 //  带方向的边缘检测共用部分 (Kirsch,Robinson,Prewitt)函数//
void KRP_op(BYTE **list1,BYTE **list0,int Dx,int Dy,int *pp,int bb)
{         
  BYTE t[8];
  int  i,j,k,p,g,m,tab[8][3][3];
   
  for (k=0;k<8;k++)
    for (i=0;i<3;i++)
	  for (j=0;j<3;j++)
		tab[k][i][j]=*pp++;

  for (i=1;i<Dy-1;i++) {
    for (j=1;j<Dx-1;j++) {
	  for (p=0;p<8;p++) {	              //  每个方向处理一遍
	    g = (tab[p][0][0]*list1[i-1][j-1]+tab[p][0][1]*list1[i-1][j]
		    +tab[p][0][2]*list1[i-1][j+1]+tab[p][1][0]*list1[i][j-1]
		    +tab[p][1][1]*list1[i][j]    +tab[p][1][2]*list1[i][j+1]
		    +tab[p][2][0]*list1[i+1][j-1]+tab[p][2][1]*list1[i+1][j]
		    +tab[p][2][2]*list1[i+1][j+1])/Scale;    //  卷积运算
		if (g<0)  g=0;	                      //  超限处理
		else if (g>0xff)  g=0xff;
		t[p]= (BYTE) g;	      //  存入相应方向单元
	  }
	  m=0;
	  for (p=0;p<8;p++) {	  //  八方向中找差值最大者
	    if (t[p]>m)  m=t[p];
	  }
      if (bb==0)
	    list0[i][j] = (BYTE) m;        //  buf放汇总值
	  else if (m==t[bb-1]) 
		list0[i][j] = (BYTE) m;        //  放入最大方向
	  else  list0[i][j] = 0;
	}
  }
}

void KRPD_op(struct IMAGE *image,int x,int y,int Dx,int Dy,int *pp,int bb)
{          
  BYTE *buf1,*buf0;
  BYTE **list1,**list0;
  int  i;
   
  buf1 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  buf0 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
 getimage(image,x,y,Dx,Dy,buf1);                      
  list1=(BYTE**) malloc(Dy*sizeof(BYTE*));       
  list0=(BYTE**) malloc(Dy*sizeof(BYTE*));       
  for(i=0;i<Dy;i++) {
    list1[i]=buf1+i*Dx;
	list0[i]=buf0+i*Dx;
  }

  KRP_op(list1,list0,Dx,Dy,pp,bb);

  for(i=1;i<Dy-1;i++) 
    setimage(image,x+1,y+i,Dx-2,1,&list0[i][1]);       
   
  free(buf1);  
  free(buf0);  
  free(list1);  
  free(list0);  
}
//Tkirsch方向边缘检测.............................//
void EX10(HWND hWnd)
{   
	if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);                                       
   Scale=8;                                       
   KRPD_op(Image[1],0,0,Image[1]->wid,Image[1]->hei,Tkirsch,0);
   WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}
//Trobinson方向边缘检测.............................//
void EX11(HWND hWnd)
{   
	if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);                                       
   Scale=2;                                       
   KRPD_op(Image[1],0,0,Image[1]->wid,Image[1]->hei,Trobinson,0);       
WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}
//Tprewitt方向边缘检测........................//
void EX12(HWND hWnd)
{   
	if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);

   ColorToGray(0,1);                                      
   Scale=2;                                       
   KRPD_op(Image[1],0,0,Image[1]->wid,Image[1]->hei,Tprewitt,0);         
WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}
//LOG边缘检测...........................//
void EX16(HWND hWnd)
{                                                       
   if ((Image[0]->hdib==NULL)||(Image[0]->bitcount<8)) return; 
 
   ClearDDB();
   WriteDDB(Image[0],  0,  0, 0, 0,512,512);
  
   
   ColorToGray(0,1);
   loG(Image[1],0,0,Image[1]->wid,Image[1]->hei);

   WriteDDB(Image[1],512,  0, 0, 0,512,512);
   InvalidateRect(hWnd,NULL,FALSE);
   Offset=0;
}
//Log函数//
void loG(struct IMAGE *image,int x,int y,int Dx,int Dy)
{  BYTE *buf1,*buf0,*buf2;
  BYTE **list1,**list0,**list2;
  int width;
	float **smx,**smy,x1;
	float **dx,**dy;
	int i,j,n;
	float gau[MAX_MASK_SIZE], dgau[MAX_MASK_SIZE], z;
    int low= 0,high=-1;

  buf1 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  buf0 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
buf2 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  getimage(image,x,y,Dx,Dy,buf1); 
  list1=(BYTE**) malloc(Dy*sizeof(BYTE*));
  list0=(BYTE**) malloc(Dy*sizeof(BYTE*));
   list2=(BYTE**) malloc(Dy*sizeof(BYTE*));
  for(i=0;i<Dy;i++) {
    list1[i]=buf1+i*Dx;
	list0[i]=buf0+i*Dx;
   list2[i]=buf2+i*Dx; 
  }
 
	for(i=0; i<MAX_MASK_SIZE; i++)
	{	x1=(float)i;
		z = (gauss(x1)+gauss(x1+0.5)+gauss(x1-0.5))/3.0;
     	z = z/(PI*2.0*sigma*sigma);
	    gau[i] = z;
       
	  if (gau[i] < 0.005)
	  {
		width = i;
		break;
	  }
	  dgau[i] = dGauss ((float)i);
	}
                  
  	n = width+width + 1;
	WIDTH = width/2;
 smx=allocate_2d_float(Dx,Dy,0);
 smy=allocate_2d_float(Dx,Dy,0);

seperable_convolution (Image[1],list1,Image[1]->wid,Image[1]->hei, gau, width, smx, smy);	
	dx = allocate_2d_float(Dx,Dy,0);
	dxy_seperable_convolution (smx, Image[1]->wid,Image[1]->hei,
		 dgau, width, dx, 1);
	free(smx[0]); free(smx);

	dy =  allocate_2d_float(Dx,Dy,0);
	dxy_seperable_convolution (smy, Image[1]->wid,Image[1]->hei,
		 dgau, width, dy, 0);
	free(smy[0]); free(smy); 

	for (i=0; i<Dx; i++)
	  for (j=0; j<Dy; j++)
	  {
	      z = norm (dx[i][j], dy[i][j]);
	     list0[i][j] = (unsigned char)(z*MAG_SCALE);
	  }  

	for(i=1;i<Dy-1;i++) 
    setimage(image,x+1,y+i,Dx-2,1,&list0[i][1]); 
  	free(dx[0]); free(dx);
	free(dy[0]); free(dy); 
  free(buf1);  
  free(buf0);  
  free(list1);  
  free(list0);
  free(buf2);  
  free(list2);
 }



//canny函数//
void canny(struct IMAGE *image,int x,int y,int Dx,int Dy)
{  BYTE *buf1,*buf0,*buf2;
  BYTE **list1,**list0,**list2;
  int width;
	float **smx,**smy,x1;
	float **dx,**dy;
	int i,j,n;
	float gau[MAX_MASK_SIZE], dgau[MAX_MASK_SIZE], z;
    int low= 0,high=-1;

  buf1 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  buf0 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  buf2 =(BYTE*)  malloc(Dx*Dy*sizeof(BYTE));
  getimage(image,x,y,Dx,Dy,buf1); 
  list1=(BYTE**) malloc(Dy*sizeof(BYTE*));
  list0=(BYTE**) malloc(Dy*sizeof(BYTE*));
   list2=(BYTE**) malloc(Dy*sizeof(BYTE*));
  for(i=0;i<Dy;i++) {
    list1[i]=buf1+i*Dx;
	list0[i]=buf0+i*Dx;
   list2[i]=buf2+i*Dx; 
  }

	for(i=0; i<MAX_MASK_SIZE; i++)
	{	x1=(float)i;
		z = (gauss(x1)+gauss(x1+0.5)+gauss(x1-0.5))/3.0;
     	z = z/(PI*2.0*sigma*sigma);
	    gau[i] = z;
       
	  if (gau[i] < 0.005)
	  {
		width = i;
		break;
	  }
	  dgau[i] = dGauss ((float)i);
	}
                  
  	n = width+width + 1;
	WIDTH = width/2;
    smx=allocate_2d_float(Dx,Dy,0);
    smy=allocate_2d_float(Dx,Dy,0);
    seperable_convolution (Image[1],list1,Image[1]->wid,Image[1]->hei, gau, width, smx, smy);//平滑卷积	
	dx = allocate_2d_float(Dx,Dy,0);
	dxy_seperable_convolution (smx, Image[1]->wid,Image[1]->hei,
		 dgau, width, dx, 1);   //x方向求导
	free(smx[0]); free(smx);

	dy =  allocate_2d_float(Dx,Dy,0);
	dxy_seperable_convolution (smy, Image[1]->wid,Image[1]->hei,
		 dgau, width, dy, 0);  //y方向求导
	free(smy[0]); free(smy); 
	for (i=0; i<Dx; i++)
	  for (j=0; j<Dy; j++)
	  {
	      z = norm (dx[i][j], dy[i][j]);
	     list0[i][j] = (unsigned char)(z*MAG_SCALE);
	  }  

	nonmax_suppress (dx, dy, Image[1]->wid,Image[1]->hei, list0, list2);

	hysteresis (high, low, Image[1], list1,list0, list2);
    for (i=0; i<WIDTH; i++)
	  for (j=0; j<Image[1]->hei; j++)
	    list1[i][j] = 255;

	for (i=Image[1]->wid-1; i>Image[1]->wid-1-WIDTH; i--)
	  for (j=0; j<Image[1]->hei; j++)
	    list1[i][j] = 255;

	for (i=0; i<Image[1]->wid; i++)
	  for (j=0; j<WIDTH; j++)
	    list1[i][j] = 255;

	for (i=0; i<Image[1]->wid; i++)
	  for (j=Image[1]->hei-WIDTH-1; j<Image[1]->hei; j++)
	    list1[i][j] =255;
	for(i=1;i<Dy-1;i++) 
    setimage(image,x+1,y+i,Dx-2,1,&list1[i][1]); 
  	free(dx[0]); free(dx);
	free(dy[0]); free(dy); 
    free(buf1);  
    free(buf0);  
    free(list1);  
    free(list0);
    free(buf2);  
    free(list2);
 }
//阈值选取及边缘链接//
void hysteresis (int high, int low, struct IMAGE *image,BYTE **list1,BYTE **list0, BYTE **list2)
{
	int i,j;


	for (i=0; i<Image[1]->wid; i++)
	  for (j=0; j<Image[1]->hei; j++)
	    list1[i][j] = 0;

	if (high<low)
	{
	  estimate_thresh (list0, &high, &low);
	  
	}
   
	for (i=0; i<Image[1]->wid; i++)
	  for (j=0; j<Image[1]->hei; j++)
	    if (list0[i][j] >= high)
	      trace (i, j, low, Image[1], list1, list0, list2);

	for (i=0; i<Image[1]->wid; i++)
	  for (j=0; j<Image[1]->hei; j++)
	    if (list1[i][j] == 0) list1[i][j] = 0;
	    else list1[i][j] = 255;
}
//估计阈值//
void estimate_thresh (BYTE **list0, int *hi, int *low)
{
	int i,j,k, hist[256], count;

	for (k=0; k<256; k++) hist[k] = 0;

	for (i=WIDTH; i<Image[1]->wid-WIDTH; i++)
	  for (j=WIDTH; j<Image[1]->hei-WIDTH; j++)
	    hist[list0[i][j]]++;

	
	j = Image[1]->wid;
	if (j<Image[1]->hei) j = Image[1]->hei;
	j = (int)(0.9*j);
	k = 255;

	count = hist[255];
	while (count < j)
	{
	  k--;
	  if (k<0) break;
	  count += hist[k];
	}
	*hi = k;

	i=0;
	while (hist[i]==0) i++;

	*low = (*hi+i)/2.0;
}
//边缘链接//
int trace (int i, int j, int low, struct IMAGE *image,BYTE **list1,BYTE **list0, BYTE **list2)
{
	int n,m;
	char flag = 0;

	if (list1[i][j] == 0)
	{
	  list1[i][j] = 255;
	  flag=0;
	  for (n= -1; n<=1; n++)
	  {
	    for(m= -1; m<=1; m++)
	    {
	      if (i==0 && m==0) continue;
	      if (range(Image[1],list0, i+n, j+m) && list0[i+n][j+m] >= low)
		if (trace(i+n, j+m, low, Image[1], list1, list0, list2))
		{
		    flag=1;
		    break;
		}
	    }
	    if (flag) break;
	  }
	  return(1);
	}
	return(0);
}
int range (struct IMAGE *image,BYTE **list0, int i, int j)
{
	if ((i<0) || (i>=Image[1]->wid)) return 0;
	if ((j<0) || (j>=Image[1]->hei)) return 0;
	return 1;
}
//非极大值抑制//
void nonmax_suppress (float **dx, float **dy, int nr, int nc, 
	BYTE **list0, BYTE **list2)
{
	int i,j;
	
	float xx, yy, g2, g1, g3, g4, g, xc, yc;

	for (i=1; i<(Image[1]->wid-1); i++)
	{
	  for (j=1; j<(Image[1]->hei-1); j++)
	  {
	    list0[i][j] = 0;

	    xc = dx[i][j];
	    yc = dy[i][j];
	    if (fabs(xc)<0.01 && fabs(yc)<0.01) continue;

	    g  = norm (xc, yc);


	    if (fabs(yc) > fabs(xc))
	    {

	      xx = fabs(xc)/fabs(yc);
	      yy = 1.0;

	      g2 = norm (dx[i-1][j], dy[i-1][j]);
	      g4 = norm (dx[i+1][j], dy[i+1][j]);
	      if (xc*yc > 0.0)
	      {
		g3 = norm (dx[i+1][j+1], dy[i+1][j+1]);

⌨️ 快捷键说明

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