Image Processing Using Sobel Operator

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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