4方向或者8方向的sobel算子,怎么求边缘方向
发布网友
发布时间:2022-04-23 18:06
我来回答
共1个回答
热心网友
时间:2023-10-12 05:50
int t0,t1,t2,t3,t4,t5,t6,t7;/////sobel模板的八个邻域值
int ab1,ab2,ab3,ab4,ma,ma1,ma2;
int m_max_ab=0;
if(m_Img_sobel!=NULL)
{
delete []m_Img_sobel;
m_Img_sobel=NULL;
}
m_Img_sobel=new unsigned char[aLineByte*aHeight];
if(m_iBitCount!=8)
{
//AfxMessageBox("sobel只处理灰度图,请先转换成灰度图");
return false;
}
else
{
for(int h=2;h<aHeight-1;h++)
{
for(int w=2;w<aWidth-1;w++)
{
if(*(aImg_zone+h*aLineByte+w)>=150*(aImg_zone+h*aLineByte+w)<=200)
{
m_max_ab=0;
t0=*(aImgData+(h-1)*aLineByte+(w-1));
t1=*(aImgData+(h-1)*aLineByte+(w));
t2=*(aImgData+(h-1)*aLineByte+(w+1));
t3=*(aImgData+(h)*aLineByte+(w+1));
t4=*(aImgData+(h+1)*aLineByte+(w+1));
t5=*(aImgData+(h+1)*aLineByte+(w));
t6=*(aImgData+(h+1)*aLineByte+(w-1));
t7=*(aImgData+h*aLineByte+(w-1));
ab1=(t0+2*t1+t2)-(t4+2*t5+t6);
if(ab1<0)
{
ab1=-ab1;
}
ab2=(t0+2*t7+t6)-(t2+2*t3+t4);
if(ab2<0)
{
ab2=-ab2;
}
ab3=(t0*2+t1+t7)-(t4*2+t3+t5);
if(ab3<0)
{
ab3=-ab3;
}
ab4=abs((t1+t2*2+t3)-(t5+t6*2+t7));
ma2=max(ab3,ab4);
ma=max(ab1,ab2);
if(ma>0)
{
if(ma<=255)
{
*(m_Img_sobel+h*aLineByte+w)=ma;
}
else
{
*(m_Img_sobel+h*aLineByte+w)=255;
}
}
else
{
*(m_Img_sobel+h*aLineByte+w)=0;
}
}
}
}
}