Usenet.com

www.Usenet.com

Group Index

Sci Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: Edge Detection in C++



I tried to apply a smoothing mask to the image by using your function
in the following manner:

int Smooth()
{
 double Result;
 int i, j, x, y, Value;
 int Sum, NeighX, NeighY;

 double Scale = 5.0;
 int smooth[][3] = {{1, 3, 1}, {3, 16, 3}, {1, 3, 1}};
 

 // allocate working array
 double* pWork = new double [my_image.width * my_image.length];

 // apply over whole image
 for (y = 0; y < my_image.length; y++)
 {
  for (x = 0; x < my_image.width; x++)
  {
   // apply filter at this xy
   Sum = 0; 
   for (i = -1; i <= 1; i++)
   {
    for (j = -1; j <= 1; j++)
    {
     // get neighbour
     NeighX = x + i;
     NeighY = y + j;

     // "extend" image at extremeties
     if (NeighX < 0) 
         {
                 NeighX = 0;
         }
     if (NeighX >= my_image.width)
         {
                 NeighX = my_image.width - 1;
         }
     if (NeighY < 0)
         {
                 NeighY = 0;
         }
     if (NeighY >= my_image.length)
         {
                 NeighY = my_image.length - 1;
         }
        
     // accumulate
     Value = my_image.raw_image[(NeighY * my_image.width) + NeighX];
     Sum = Sum + (Value * smooth[j + 1][i + 1]);
     
    }
   }

   // calculate result
   Result = Sum;
   if (Result > 0) Result = sqrt(Result);
   pWork[(y * my_image.width) + x] = Result / Scale;
  }
 }

 // copy result back into source array
 for (i = 0; i < my_image.width * my_image.length; i++)
 {
  // clamp to 255
  if (pWork[i] > 255)
   my_image.raw_image[i] = 255;
  else
   my_image.raw_image[i] = (unsigned char)pWork[i];
 }

return 0;
}

I thought that by passing the filter (1 3 1) (3 16 3) (1 3 1) over the
image
in the same way as you passed the 2 sobel filters woulds create the
smoothing effect but.....



[EMAIL PROTECTED] (DaveWilson) wrote in message news:<[EMAIL PROTECTED]>...
> Sorry to keep pestering you :p
> 
> I was wondering could i use that function for noise removal and smoothing?
> 
> 
> 
> "Roger Rowland" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> > "DaveWilson" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> > > Well that got it working :) thankyou for the help. but your right I
> > > would like to understand this a little better. I now understand the
> > > need for a second buffer. But am still a little in the dark about the
> > > actual operation and how it aplies to the code?
> > > Most of the code i undrstood but was a little unclear on the following
> > > sections:
> > >
> > 
> > Just taking these in a different order from your message. The following
> > loop -
> > 
> > >      // apply filter at this xy
> > >      nXSum = nYSum = 0;
> > >      for (i = -1; i <= 1; i++)
> > >      {
> > >       for (j = -1; j <= 1; j++)
> > >       {
> > >        ........
> > 
> > is looking at the 3x3 pixels around the current (x,y) - so the variable "i"
> > scans over the x dimension from one column left to one column right, and the
> > variable "j" scans over the y dimension from one row up to one row down.
> > Within these loops, nNeighX and nNeighY contain the coordinates of each of
> > the 3x3 pixels in turn -
> > 
> > >      nValue = my_image.raw_image[(nNeighY * my_image.width) +
> > > nNeighX];
> > 
> > Then the sobel operator is applied by taking the matching coefficient from
> > the static arrays nSobelX and nSobelY, which contain the h1 and h3 masks.
> > These are both 3x3 arrays and are accessed by zero-based index (like C
> > always does), so I add 1 to "i" and "j" (which range from -1 to +1) to get
> > the correct entry (in the range 0 to 2) and then accumulate the result for
> > both x and y -
> > 
> > >      nXSum += nValue * nSobelX[j + 1][i + 1];
> > >      nYSum += nValue * nSobelY[j + 1][i + 1];
> > >
> > 
> > After applying the masks over the whole 3x3 area, nXSum and nYSum will hold
> > the (approximated) gradient in the x and y directions respectively. So the
> > magnitude of this gradient vector is used to derive the edge strength -
> > 
> > >      dResult = (double)((nXSum * nXSum) + (nYSum * nYSum));
> > >      if (dResult > 0.0) dResult = sqrt(dResult);
> > 
> > The edge strength is scaled and stored in my work buffer before proceeding
> > to the next (x,y) -
> > 
> > >      pWork[(y * my_image.width) + x] = dResult / dScale;
> > 
> > Does this make it any clearer?
> > 
> > Regards,
> > Roger
> > 
> > ----------------------------------------------------------------------------
> > -----------
> > EMail: roger dot rowland at rmrsystems dot co dot uk



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.