0% found this document useful (0 votes)
111 views

Image Processing Using Sobel Operator

This document describes using the Sobel operator to perform edge detection on an image. It defines Sobel filter masks to calculate gradients in the x and y directions. It convolves the masks with the input image to produce separate x and y gradient images. It then calculates the total gradient at each pixel from the x and y gradients and scales the results to output a final edge detected image.

Uploaded by

Khushboo Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Image Processing Using Sobel Operator

This document describes using the Sobel operator to perform edge detection on an image. It defines Sobel filter masks to calculate gradients in the x and y directions. It convolves the masks with the input image to produce separate x and y gradient images. It then calculates the total gradient at each pixel from the x and y gradients and scales the results to output a final edge detected image.

Uploaded by

Khushboo Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

IMAGE PROCESSING USING SOBEL OPERATOR

Void CWinTestDoc :: m_EdgeSobel(int height, int width)


{
int MaskSobelX[3][3]={{-1,0,1}, {-2,0,2}, {-1,0,1}};
int MaskSobelY[3][3]={{1,2,1}, {0,0,0}, {-1,-2,-1}};
int heightm1=height-1;
int widthm1=width-1;
int mr,mc;
int newValue;
int i,j;
int *pImgSobelX,*pImgSobelY;
int min,max,where;
float constVal1,constVal2;
pImgSobelX=new int[height*width];
pImgSobelY=new int[height*width];
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
m_OutImg[i][j]=0;
where=i*width+j;
pImgSobelX[where]=0;
pImgSobelY[where]=0;
}
for(i=1; i<heightm1; i++)

{
for(j=1; j<widthm1; j++)
{
newValue=0;
for(mr=0;mr<3;mr++)
for(mc=0;mc<3;mc++)
newValue += (MaskSobelX[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
pImgSobelX[i*width+j]=newValue;
}
}
for(i=1; i<heightm1; i++)
{
for(j=1; j<widthm1; j++)
{
newValue=0;
for(mr=0;mr<3;mr++)
for(mc=0;mc<3;mc++)
newValue += (MaskSobelY[mr][mc]*m_InImg[i+mr-1][j+mc-1]);
pImgSobelY[i*width+j]=newValue;
}
}
for(i=1;i<heightm1;i++)
for(j=1;j<widthm1;j++)
{
where=i*width+j;

constVal1=pImgSobelX[where];
constVal2=pImgSobelY[where];
if(constVal1<0)
constVal1=-constVal1;
if(constVal2<0)
constVal2=-constVal2;
pImgSobelX[where]=constVal1+constVal2;
}
min=(int)10e10;
max=(int)-10e10;
for(i=1; i<heightm1; i++)
{
for(j=1; j<widthm1; j++)
{
newValue=pImgSobelX[i*width+j];
if(newValue<min)
min=newValue;
if(newValue>max)
max=newValue;
}
}
constVal1=(float)(255.0/(max-min));
constVal2=(float)(-255.0*min/(max-min));
for(i=1; i<heightm1; i++)
{

for(j=1; j<widthm1; j++)


{
newValue=pImgSobelX[i*width+j];
newValue=constVal1*newValue+constVal2;
m_OutImg[i][j]=(BYTE)newValue;
}
}
delete [] pImgSobelX;
delete [] pImgSobelY;
}

You might also like