Skip to content

Instantly share code, notes, and snippets.

@uugan
Last active December 16, 2015 13:39
Show Gist options
  • Save uugan/5443003 to your computer and use it in GitHub Desktop.
Save uugan/5443003 to your computer and use it in GitHub Desktop.
/* Edge detection by sobel */
float EdgeSobel(unsigned char* inbuf, unsigned char* outbuf, int height, int width){
int start = GetTickCount();
short filter_x[3][3] =
{ { -1, 0, 1},
{-2, 0, 2},
{ -1, 0, 1}};
short filter_y[3][3] =
{ { -1, -2, -1},
{0, 0, 0},
{ 1, 2, 1}};
int sum_x=0;
int sum_y=0;
int sum=0;
for(int i=1; i<height-1;i++)
for(int j=1; j<width-1; j++)
{
sum_x = 0;
sum_y = 0;
for(int a=-1; a<=1; a++)
for(int b=-1; b<=1; b++){
sum_x += inbuf[(i+a)*width+j+b]*filter_x[a+1][b+1];
sum_y += inbuf[(i+a)*width+j+b]*filter_y[a+1][b+1];
}
sum =(abs(sum_x)+abs(sum_y))/6;
if(sum < 0) sum = 0;
if(sum > 255) sum = 255;
outbuf[i*width+j]=sum;
}
return (float)(GetTickCount() - start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment