Monday, May 27, 2013

edge detection based on sobel operator in MFC

void CChildView::OnEdge() // edge detection based on Sobel operators
{

 {
 int operx[3][3]={-1,0,1,-2,0,2,-1,0,1};//Sobel operators
 int opery[3][3]={1,2,1,0,0,0,-1,-2,-1};//Sobel operators
 int i,j;
 int p=0;
 int smx=0,smy=0;
 //CImage img_tem;
 //img_tem.Create(img_width,img_hight,24,0);
 for(i=0;i<img_hight;i++)
 {
  for(j=0;j<img_width;j++)
  {
   COLORREF cf = MyRGB(0,0,0);
   img_final.SetPixel(j,i,cf); //img_final is a member of this class, this is to reset it to zeros
         
  }
 }

 for(i=0;i<img_hight-2;i++)
 {
  for(j=0;j<img_width-2;j++)
  {
            smx=0;
   smy=0;
   for (int m=0; m<3; m++)
   {
      for (int n=0; n<3; n++)
      {  
      COLORREF rgb=img_new.GetPixel(j+n,i+m);
         int p=MyGetRValue(rgb);
                  smx=smx+p*operx[m][n];
                  smy=smy+p*opery[m][n];
      }
   }
   float G=smx*smx+smy*smy;
   int GD=(int)sqrt(G);
           
   if(GD>100) // threshold to determine whether it is an edge
   {
         COLORREF cf = MyRGB(255,255,255);
         img_final.SetPixel(j+1,i+1,cf);
 
   }
  }
 }
  
   Invalidate(); //call the OnPaint to show the result

}


No comments:

Post a Comment